我一直在研究这个pygame应用程序,这是一个基本上是一个硬币收集游戏。它有一个非常简单的想法,你有一个自由移动的玩家(在这种情况下是一个蓝色的球,移动它“WASD”)它应该收集一定时间后出现在屏幕上的硬币。代码很长,对于SO标准,我会尽力解释我的问题。
from pygame.locals import *
import pygame
import sys
import time
import random
image_resources = "C:/Users/user/Desktop/Pygame App/app_resources/image_resources/"
sound_resources = "C:/Users/user/Desktop/Pygame App/app_resources/sound_resources/"
width,height = 400,400
size = (width,height)
elapsed_seconds = 0
def quit_game():
pygame.quit()
sys.exit("System exit.")
class GetSource:
def background(self,image):
return pygame.image.load(image_resources + image).convert()
def player(self,image):
return pygame.image.load(image_resources + image).convert_alpha()
class Wall(pygame.sprite.Sprite):
def __init__(self,color,x,y,width,height):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((width,height))
self.image.fill(pygame.color.Color(color))
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
class Player(pygame.sprite.Sprite):
def __init__(self,image):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image_resources + image).convert_alpha()
self.rect = self.image.get_rect()
class Coin(pygame.sprite.Sprite):
def __init__(self,image):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image_resources + image).convert_alpha()
self.rect = self.image.get_rect()
pygame.init()
screen = pygame.display.set_mode(size)
pygame.display.set_caption("PyGame App")
background = GetSource().background("bg_solid_black_square.jpg")
player = GetSource().player("ball_blue.png")
player_dimension = player.get_width()
x,y = width/2-player_dimension,height/2-player_dimension
movex,movey = 0,0
walls = pygame.sprite.Group()
players = pygame.sprite.Group()
coins = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()
wall_1 = Wall("white", 0, 0, width, 5)
wall_2 = Wall("white", 0, 0, 5, height)
wall_3 = Wall("white", 0, height-5, width, 5)
wall_4 = Wall("white", width-5, 0, 5, height)
player = Player("ball_blue.png")
coin = Coin("coin.png")
walls.add(wall_1,wall_2,wall_3,wall_4)
players.add(player)
coins.add(coin)
all_sprites.add(wall_1,wall_2,wall_3,wall_4,player)
while True:
time.sleep(0.01)
elapsed_seconds += 0.01
collide_list_1 = pygame.sprite.spritecollideany(wall_1,players)
collide_list_2 = pygame.sprite.spritecollideany(wall_2,players)
collide_list_3 = pygame.sprite.spritecollideany(wall_3,players)
collide_list_4 = pygame.sprite.spritecollideany(wall_4,players)
for event in pygame.event.get():
if event.type == QUIT:
quit_game()
if event.type == KEYDOWN:
if event.key == K_q:
quit_game()
elif event.key == K_a:
movex = -1
elif event.key == K_d:
movex = 1
elif event.key == K_w:
movey = -1
elif event.key == K_s:
movey = 1
if event.type == KEYUP:
if event.key == K_a or event.key == K_d:
movex = 0
if event.key == K_w or event.key == K_s:
movey = 0
if collide_list_1 != None:
movey = 0
y += 1
if collide_list_2 != None:
movex = 0
x += 1
if collide_list_3 != None:
movey = 0
y -= 1
if collide_list_4 != None:
movex = 0
x -= 1
else:
x += movex
y += movey
player.rect.x = x
player.rect.y = y
coin.rect.x = random.randint(0,width)
coin.rect.y = random.randint(0,height)
screen.blit(background, (0,0))
if elapsed_seconds % 4 == 0:
coins.draw(screen)
coins.update()
all_sprites.draw(screen)
all_sprites.update()
pygame.display.update()
正如你所看到的,我在每一帧增加“elapsed_seconds”变量到等待的时间,以便每秒调节帧数,然后检查它是否是4的倍数。但事实是,当我监视时“elapsed_seconds”变量,它实际上从未变为4.它通常在“2,9998999128999”和东西。我试过了;
elapsed_seconds = math.floor(elapsed_seconds)
但这也不好。那么,我怎样才能在每个设定的时间间隔渲染这枚硬币?
编辑:我希望精灵不要在屏幕上闪烁,我希望他们在“证明时间”通过后留在原地。因此,在“一定的时间”之后它应该看起来好像移动了!感谢。答案 0 :(得分:2)
首先,pygame.time.get_ticks()
将返回自游戏开始以来经过的毫秒数。您的方法不准确,因为它没有考虑执行游戏循环所花费的时间。
您可以跟踪自上次添加硬币以来经过的时间。然后:
ticks = pygame.time.get_ticks()
if ticks - last_coin_ticks > 1000: # new coin once per second
last_coin_ticks = ticks
# add a new coin