问题描述
好的,所以我是 Stack Overflow 和 Python 的新手,我正在使用 Pygame 制作一个游戏,其中产生一堆随机颜色的球,它们有望随机移动,当它们碰撞时会变得更大并占据“通过使用 rg 和 b 值的整数来计算颜色的平均值。我目前的问题是我产生了球,但我无法让球移动。我试图让他们在我的球类中移动更新功能。我希望它们在没有用户输入的情况下随机移动。
这是我的主要内容
import pygame
import random
import ballClass
WORLD_WIDTH = 800
WORLD_HEIGHT = 600
WIN = pygame.display.set_mode((WORLD_WIDTH,WORLD_HEIGHT))
paused = False
def main():
global numberOfBalls,WIN,paused
pygame.init()
clock = pygame.time.Clock()
running = True
WHITE = (255,255,255)
#ballClass.Ball().initialize(10)
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_1:
paused = not paused
if not paused:
WIN.fill(WHITE)
ballClass.Ball().draw(WIN)
ballClass.Ball().update(WIN)
#drawWorld(WIN)
clock.tick(1)
pygame.display.update()
def setinitialBallAttributes():
pass
#def checkForCollision(self): #iterate through list of objects and check each one against,tell it to do averaging
# ball1 = self.ball
# ball2 = self.ball
# ball3 = self.ball
# ball4 = self.ball
# ball5 = self.ball
# balls = [ball1,ball2,ball3,ball4,ball5]
# #from Mr. Sharick
# for i in range(len(balls)):
# for j in range(i,len(balls)): # start this loop at i+1 so you don't check collisions twice
# balls[i].collision(balls[j])
#def drawWorld(WIN,self):
# ball1 = self.ball
# ball2 = self.ball
# ball3 = self.ball
# ball4 = self.ball
# ball5 = self.ball
#pygame.draw.rect(WIN,'black',width=0)
#pass
if __name__ == "__main__":
main()
这是我的球类
import pygame
import random
import math
balls = []
center1 = random.randint(0,600)
center2 = random.randint(0,600)
balls.append((center1,center2))
class Ball():
WORLD_WIDTH = 600
WORLD_HEIGHT = 600
def __init__(self,centerX=None,centerY=None,direction=None,r=None,g=None,b=None,radius= None,speed= None):
self.centerX = 10
self.centerY = 20
self.r = r
self.g = g
self.b = b
self.direction = direction
self.radius = radius
self.speed = 3 #Set as default 3; change later
def initialize(self,num):
global balls
for i in range(num):
self.centerX = random.randint(0,600)
self.centerY = random.randint(0,600)
balls.append((self.centerX,self.centerY))
def draw(self,WIN):
global ball,radius,centerX,centerY,r,g,b,balls
self.radius = 10
self.direction = random.randint(0,600),random.randint(0,600)
self.speed = 3
#self.centerX = random.randint(self.radius,600 - self.radius)
#self.centerY = random.randint(self.radius,600 - self.radius)
r = random.randint(0,255)
g = random.randint(0,255)
b = random.randint(0,255)
#self.ball = Ball(centerX,WIN)
# needs all 8
for ball in balls:
pygame.draw.circle(WIN,((r,b)),(ball[0],ball[1]),self.radius)
print(ball)
#balls.append((self.centerX,self.centerY))
# def collision(self,other): #pass in another ball object as another parameter
# distance = math.sqrt((self.centerX - self.centerY)^2 + (other.centerX - other.centerY)^2)
# if distance < self.radius + other.radius:
# (self.r + other.r) / 2 == self.r
# (self.g + other.g) / 2 == self.g
# (self.b + other.b) / 2 == self.b
# if self.radius > other.radius:
# self.radius = other.radius + self.radius
# other.radius = 0
# if other.radius > self.radius:
# other.radius = other.radius + self.radius
# self.radius = 0
# else:
# #self.radius = other.radius + self.radius
# self.centerX = (other.centerX + self.centerX) / 2
# self.centerY = (other.centerY + self.centerY) / 2
# other.radius = 0
def update(self,win):
WORLD_WIDTH = 600
WORLD_HEIGHT = 600
# if len(balls):
# for i in range(len(balls)):
# balls[i] = (center1+10,center2 +10)
newBallCoords
# if self.right >= WORLD_WIDTH or ball.left <= 0:
# self.direction = -self.centerX,self.centerX
# if self.bottom >= WORLD_HEIGHT or ball.top <= 0:
#self.direction = self.centerX,-self.centerX
我注释掉了碰撞部分的内容......我还没有广告希望让球移动。如果我是新手,使用此页面有误,我提前道歉,但非常感谢您的帮助。
解决方法
如果你想让球以自己独立的方向移动,每个球都应该有自己的 X 和 Y 速度,这些速度应该在初始化时随机生成。
查看您包含的程序,我建议您考虑将 Ball 类与控制所有球的逻辑分开。
我已经包含了包含此想法的程序的修改版本。
主要内容:
import pygame
import random
import math
import ball as ballClass
WORLD_WIDTH = 800
WORLD_HEIGHT = 600
def main():
paused = False
WIN = pygame.display.set_mode((WORLD_WIDTH,WORLD_HEIGHT))
pygame.init()
clock = pygame.time.Clock()
running = True
WHITE = (255,255,255)
balls = [ballClass.Ball() for _ in range(8)]
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_1:
paused = not paused
if not paused:
WIN.fill(WHITE)
for ball in balls:
ball.draw(WIN)
ball.update(WIN)
clock.tick(1)
pygame.display.update()
if __name__ == "__main__":
main()
球
import random,pygame
class Ball():
WORLD_WIDTH = 600
WORLD_HEIGHT = 600
def __init__(self,centerX=None,centerY=None,direction=None,r=None,g=None,b=None,radius= None,speed= None):
self.centerX = random.randint(0,600)
self.centerY = random.randint(0,600)
self.r = random.randint(0,255)
self.g = random.randint(0,255)
self.b = random.randint(0,255)
self.velocityX = random.randint(-10,10)
self.velocityY = random.randint(-10,10)
self.radius = 10
def draw(self,WIN):
pygame.draw.circle(WIN,((self.r,self.g,self.b)),(self.centerX,self.centerY),self.radius)
def update(self,win):
self.centerX += self.velocityX
self.centerY += self.velocityY
在我提供的代码中,Ball 的 update 方法只更新一个球,它有各自的速度,主程序告诉所有球它们需要更新。