我想用pygame做一个小游戏。我不知道如何使球随着时间的推移而增加,并且使球的繁殖速度增加。 它将是这样的: https://www.youtube.com/watch?v=DCQp1Q8ANCM (0:31)
#ball.py
import pygame
import random
YELLOW = (225, 225, 0)
class balls:
def draw_ball(screen, tickrate, i):
x = random.randint(0,500)
y = random.randint(0,500)
first_range = 10
range = 10
print(i)
if i >= 100 :
ball = pygame.draw.circle(screen, YELLOW, (x, y), range)
#Main.py
import pygame
import os
import random
import ball
import threading
import sys
from timeit import Timer
pygame.init()
game_screen = pygame.display.set_mode((800, 600))
x = 100
y = 100
os.environ['Sp_VIDEO_WINDOW_POS'] = "%d,%d" % (x,y)
size = [500, 500]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Reaction")
background = pygame.image.load("images\\background.jpg")
background_rect = background.get_rect(bottomright = (500,500))
background.set_colorkey((255,255,255)) #прозрачный слой фона
screen.blit(background,background_rect)
pygame.display.update()
run_game = True #флаг игрового цикла
clock = pygame.time.Clock()
FPS = 60
starttime=pygame.time.get_ticks()
#timer = Timer(0.05, ball.balls.draw_ball(screen)) # 50 миллисекунд
i = 1
def quit():
rungame = False
pygame.quit()
sys.exit()
while run_game: #игровой цикл
#timer.start()
tickrate = clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
ball.balls.draw_ball(screen,tickrate,i)
pygame.display.update()
if i == 100:
i = 0
i+=1
pygame.display.flip()
您没有提供任何代码,因此对任何想要回答您的问题的人几乎没有帮助
我假设你有一个像这样的球对象:
您的pygame while循环可以如下所示
当然,这是假设您对某些半径、某些编号、屏幕、颜色和位置具有适当的值
下面是我要做的:
首先,使用变量x、y和size定义一个类:
每次要创建新的绿球时,都可以调用如下函数:
现在,要绘制每个刻度的圆,我们需要让它们自己渲染
希望这些代码块能有所帮助
编辑: 在胡闹了一会儿之后,我想出了这个计划:
我不想破坏你编写有趣游戏的乐趣,但这里是我创建的一个基本设置。请随时向我提问,这意味着什么:D
创建一个类
Ball
。该类有3个属性。位置(self.x
,self.y
)和当前大小(self.size
)。该调用必须调用方法change_size
更改球的大小并draw_ball
绘制球:创建
Ball
的实例,更改球的大小并在应用程序循环中绘制球:相关问题 更多 >
编程相关推荐