Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pygame球反弹模拟中的变量赋值_Python_Pygame - Fatal编程技术网

Python Pygame球反弹模拟中的变量赋值

Python Pygame球反弹模拟中的变量赋值,python,pygame,Python,Pygame,我正在尝试使用Python 3.7.3中的Pygame模块进行球反弹模拟。我的班级展示了球,但没有动作。错误是赋值前引用的局部变量x。我认为这意味着它是局部的,但需要是全局的,但是要把球的数量作为一个变量,所以我可以说要生成多少球,我不知道如何解决这个问题 我试过阅读其他问题,但没有一个能解决我的问题。我可以让一个球在屏幕上弹跳,边界碰撞也可以,但当我使它面向对象时就不行了。我也使用了自变量来表示每个球,但这不起作用 class BallEntity(): def __init__(se

我正在尝试使用Python 3.7.3中的Pygame模块进行球反弹模拟。我的班级展示了球,但没有动作。错误是赋值前引用的局部变量x。我认为这意味着它是局部的,但需要是全局的,但是要把球的数量作为一个变量,所以我可以说要生成多少球,我不知道如何解决这个问题

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

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

全局胜利,x,y让我们删除x未定义` 此外,x,y在内部类中使用,而不是指向各个圆点的全局变量。 全局胜利,x,y让我们删除x未定义` 此外,x,y在内部类中使用,而不是指向各个圆点的全局变量。
始终将完整的错误消息回溯作为文本,而不是屏幕截图。还有其他有用的信息。代替ballenty.movementself,您可以执行self.movement,这是首选的方法。您的变量是局部变量-您必须使用self。要访问类中所有方法中的变量-self.x、self.y、self.dx、self.dy,而不是ballenty.\uu init\uu i、r1但ballentyr1始终以文本形式而不是屏幕截图形式对完整的错误消息进行回溯。还有其他有用的信息。代替ballenty.movementself,您可以执行self.movement,这是首选的方法。您的变量是局部变量-您必须使用self。要访问类中所有方法中的变量-self.x、self.y、self.dx、self.dy,而不是ballenty.\uu init\uu i、r1,但不带解释的ballentyr1代码不是首选答案-1@furas“现在怎么样?”韦哈雷解释道,什么也解释不了。代码中没有全局x,y,那么为什么要提到它呢?代码使用self.x,self.y,但对此没有解释。@furas-hmm:有效点。我点击错误,找到了这个答案。看到您的评论后应用了编辑,认为这可能足以证明删除您的否决票是合理的。我不在乎任何一种方式,但我想你可能想要你的1代表回来。没有解释的代码不是首选答案-1@furas“现在怎么样?”韦哈雷解释道,什么也解释不了。代码中没有全局x,y,那么为什么要提到它呢?代码使用self.x,self.y,但对此没有解释。@furas-hmm:有效点。我点击错误,找到了这个答案。看到您的评论后应用了编辑,认为这可能足以证明删除您的否决票是合理的。我不管怎样都不在乎,但我想你可能想要回你的1号代表。
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()