创建反弹球pygam的多个实例

2024-10-01 07:47:27 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我是pygame的新手,我做了一个程序,可以让一个球根据重力反弹,它工作得很好,但是现在我想通过在任何地方创建一个,来让它们无限地发挥作用。我知道如何获得点击的坐标,但我不知道如何创建多个圆。我会使用函数或类吗?这是我的代码,如果有点乱,很抱歉。在

import pygame,random,time
pygame.init()
infoObject = pygame.display.Info()
side = pygame.display.Info().current_h
side = side - ((1.0/12)*(pygame.display.Info().current_h))
side = int(side)
screen = pygame.display.set_mode([side, side])
pygame.display.set_caption("")
clock = pygame.time.Clock()
done = False
BLACK = (0,0,0)
WHITE = (255,255,255)
RED = (250,30,25)
GREEN = (25, 240, 25)
YELLOW = (250,240,25)
FPS = 60
x=100
y=100
t=0
r=10
direction=0
count = 0
def faller(x,y,color,size):
    pygame.draw.circle(screen,color,(x,y),size,0)
def reverse():
    global t,direction
    if direction == 0:
        direction = 1
        t=t*4/5
    elif direction == 1:
        direction = 0
        t=1
while not done:
        clock.tick(FPS)
        events = pygame.event.get()
        screen.fill(BLACK)
        for event in events:
            if event.type == pygame.QUIT or  (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
                done = True
        if direction==0:
            t+=1
            y+=int((9.8/FPS)*t)
        elif direction==1:
            t-=1
            y-=int((9.8/FPS)*t)
        if t==0:
            reverse()
        if y+r>side:
            y=side-r
            faller(x,y,RED,r)
            reverse()
        else:
            faller(x,y,RED,r)
        if y==side-r:
            count+=1
        else:
            count=0
        if count>=3:
            done = True
        pygame.display.flip()
pygame.quit()

Tags: infoeventifcountdisplayredscreenpygame
1条回答
网友
1楼 · 发布于 2024-10-01 07:47:27

我鼓励你尝试用类和方法。一旦你有了一个类Ball,你就可以列一个Ball对象的列表,然后一次处理它们。然后可以使用.append()list方法在鼠标单击时添加一个新的球。在

我建议您使用以下变量: 位置x, 是的, 维克斯, 维尤

如果您想要实现碰撞,这将使事情更容易理解。别忘了评论你的代码:)

相关问题 更多 >