Python:如何在反弹球代码中正确应用OOP
我试图在这个弹跳球代码中应用OOP,以便让多个球在屏幕上弹跳。这段代码的目标是创建一个名为settings的方法,该方法控制屏幕的所有设置以及Ball类下多个球的反弹行为。但是,我不断得到与属性(例如速度)相关的错误。我是Python中OOP的新手,非常感谢您的帮助。提前谢谢Python:如何在反弹球代码中正确应用OOP,python,oop,attributes,Python,Oop,Attributes,我试图在这个弹跳球代码中应用OOP,以便让多个球在屏幕上弹跳。这段代码的目标是创建一个名为settings的方法,该方法控制屏幕的所有设置以及Ball类下多个球的反弹行为。但是,我不断得到与属性(例如速度)相关的错误。我是Python中OOP的新手,非常感谢您的帮助。提前谢谢 import pygame import random import sys pygame.init() class Ball: def __init__(self, X, Y): self.v
import pygame
import random
import sys
pygame.init()
class Ball:
def __init__(self, X, Y):
self.velocity = [1,1]
self.ball_image = pygame.image.load ("ball.jpg")
self.ball_boundary = self.ball_image.get_rect ()
self.black = [0,0,0]
self.width = 800
self.height = 600
self.num = 8
self.X = random.randint(0, self.width)
self.Y = random.randint(0, self.height)
def settings(self):
#X = random.randint(0, self.width)
#Y = random.randint(0, self.height)
clock = pygame.time.Clock()
size = self.width, self.height
screen = pygame.display.set_mode(size)
ball_boundary = self.ball_image.get_rect()
speed = self.velocity
pic = self.ball_image
pygame.display.set_caption("Balls")
num_balls = self.num
ball_list = []
for i in range(num_balls):
ball_list.append( Ball(random.randint(10, self.width-10),random.randint(10, self.height-10)) )
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0)
screen.fill(self.black)
for balls in ball_list:
if balls.ball_boundary.left < 0 or balls.ball_boundary.right > self.width:
balls.speed[0] = -balls.speed[0]
if balls.ball_boundary.top < 0 or balls.ball_boundary.bottom > self.height:
balls.speed[1] = -balls.speed[1]
balls.ball_boundary = balls.ball_boundary.move (self.velocity)
screen.blit (balls.ball_image, balls.ball_boundary)
pygame.display.flip()
play = Ball(random.randint(0, 800), random.randint(0, 600))
play.settings()
导入pygame
随机输入
导入系统
pygame.init()
班级舞会:
定义初始化(self,X,Y):
self.velocity=[1,1]
self.ball\u image=pygame.image.load(“ball.jpg”)
self.ball\u boundary=self.ball\u image.get\u rect()
self.black=[0,0,0]
自宽=800
自身高度=600
self.num=8
self.X=random.randint(0,self.width)
self.Y=random.randint(0,self.height)
def设置(自):
#X=random.randint(0,self.width)
#Y=random.randint(0,自身高度)
clock=pygame.time.clock()
尺寸=自宽、自高
screen=pygame.display.set_模式(大小)
ball\u boundary=self.ball\u image.get\u rect()
速度=自身速度
pic=self.ball\u图像
pygame.display.set_标题(“球”)
num\u balls=self.num
ball_list=[]
对于范围内的i(球数):
ball_list.append(ball(random.randint(10,self.width-10),random.randint(10,self.height-10)))
而1:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
系统出口(0)
屏幕填充(自身黑色)
对于ball_列表中的球:
如果balls.ball_boundary.left<0或balls.ball_boundary.right>self.width:
球.速度[0]=-球.速度[0]
如果balls.ball_boundary.top<0或balls.ball_boundary.bottom>self.height:
balls.speed[1]=-balls.speed[1]
balls.ball\u boundary=balls.ball\u boundary.move(自速度)
screen.blit(balls.ball\u图像,balls.ball\u边界)
pygame.display.flip()
play=球(random.randint(0800),random.randint(0600))
play.settings()
如果您有错误,您应该说出它们是什么,并显示完整的回溯。回溯:AttributeError:Ball实例没有属性“speed”,这不是完整的回溯。但至少很明显,球没有速度
属性,它们有速度
属性。