首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >热对策球弹跳仿真中的变量分配

热对策球弹跳仿真中的变量分配
EN

Stack Overflow用户
提问于 2019-04-20 03:28:40
回答 1查看 132关注 0票数 2

我正在尝试使用Python 3.7.3中的Pygame模块来进行弹跳模拟。我上的课展示的是球,但不适合运动。错误是“赋值前引用的局部变量x”。我认为这意味着它是本地的,但需要是全局的,但是要将球的数量作为变量(所以我可以说生成多少),我不知道如何解决这个问题。

我试过阅读其他的问题,但没有一个问题解决了我的问题。我能够让一个球在屏幕上弹跳,边界碰撞起作用,但当我使它面向对象时就不行了。我也玩过自变量来指每一个球,但那是行不通的。

代码语言:javascript
复制
class BallEntity():
    def __init__(self, radius):
        x = random.randint(radius, win_width - radius)
        y = random.randint(radius, win_height - radius)
        pos = x, y
        pygame.draw.circle(win, (248, 24, 148), pos, radius)
        dx = random.randint(-5, 5)
        dy = random.randint(-5, 5)
        BallEntity.movement(self)

    def movement(self):
        if x <= r1 or x >= win_width - r1:
            dx = -dx
        elif x > r1 and x < win_width -r1:
            x += dx

        if y <= r1 or y >= win_height - r1:
            dy = -dy
        elif self.y > r1 and self.y < win_height -r1:
            y += dy


numbBalls = 8
r1 = 8
for i in range(numbBalls):
    BallEntity.__init__(i, r1)

我希望球在碰撞工作时打印和移动,但是我得到了“赋值前引用的局部变量x”的错误。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-20 03:53:52

  • global win,x,y让我们删除未定义的x
  • 另外,xy被用于内部class而不是global variable到各个圆点。
代码语言:javascript
复制
import random

import pygame, sys
import time
from pygame.locals import *

pygame.init()

win_width = 500
win_height = 400
radius = 90

win = pygame.display.set_mode((win_width, win_height), 0, 32)

class BallEntity():
    win = None

    x = 0
    y = 0

    dx = 1
    dy = 1

    radius = 0
    # preious position(used for delete previous circle after movement)
    prePos = 0, 0
    pos = 0, 0
    def __init__(self, i, radius):
        self.radius = radius

        # random position (x, y)
        self.x = random.randint(radius, win_width - radius)
        self.y = random.randint(radius, win_height - radius)

        self.dx = random.randint(-5, 5)
        self.dy = random.randint(-5, 5)

    def movement(self):
        global win

        if self.x <= r1 or self.x >= win_width - r1:
            self.dx = -self.dx
        elif self.x > r1 and self.x < win_width -r1:
            self.x += self.dx

        if self.y <= r1 or self.y >= win_height - r1:
            self.dy = -self.dy
        elif self.y > r1 and self.y < win_height -r1:
            self.y += self.dy

        self.pos = self.x, self.y
        # draw background color to delete previous position
        pygame.draw.circle(win, (0, 0, 0), self.prePos, self.radius)
        # draw circle
        pygame.draw.circle(win, (248, 24, 148), self.pos, self.radius)

        self.prePos = self.pos

numbBalls = 5
r1 = 8


balls = []



# Create balls and store list
for i in range(numbBalls):
    e = BallEntity(i, r1)
    balls.append(e)

while True:
    # update circle position via movement()
    for i in range(numbBalls):
        balls[i].movement()

    pygame.display.update()
    # delay for animation
    time.sleep(0.1)
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55769989

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档