只是为了澄清上下文:
我不确定发生了什么错误。我的代码被构建为使用pygame显示一个窗口,其中包含一个玩家的图像和一个敌人的图像,以及一个自动滑下屏幕的红色表面。注意:此代码包含http://programarcadegames.com/中的部分代码。
现在我的问题:
但是我想添加一个关于敌人被玩家发射的子弹击中的确切时间的印象。问题在于print (currentTime)
它在打印的那一刻与敌人被击中的那一刻是不同的。但如果你使用print(time.time () - startTime)
的时刻被标记为预期。为了检查这种差异,我使用了秒表,从终端上的代码执行开始直到大约 7 秒(当我从屏幕上击中并消灭敌人时)我得到了两个不同的结果,证实了这个问题的存在。
案例 1(使用print (currentTime)
):启动代码执行并同时在外部某处启动计时器(例如您的智能手机)。在大约 7 秒内击中敌人并从屏幕上消灭他,同时停止外部计时器。结果:终端通过标记类似0.5645756721496582
.
案例 2(已使用print(time.time () - startTime)
):开始执行代码并同时在外部某处(例如您的智能手机)启动计时器。在大约 7 秒内击中敌人并从屏幕上消灭他,同时停止外部计时器。结果:终端收到类似的印象标记7.940780162811279
。
我想知道我使用print(time.time () - startTime)
时和使用时出现分歧的原因是什么print(currentTime)
。
由于我不是 python 专家,我最终没有使用“良好实践”来编写玩家、子弹和敌人之间的关系逻辑以及它们各自在屏幕上的渲染。但我相信这不会影响我的问题本身的合法性。
这是我的有效代码:
import pygame
import random
from pynput.keyboard import Listener, Key
import time
#store start timestamp
startTime = time.time()
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
#create window
size = (700, 500)
screen = pygame.display.set_mode(size)
#class
currentTime = time.time() - startTime
class Block(pygame.sprite.Sprite):
""" This class represents the block. """
def __init__(self, color):
# Call the parent class (Sprite) constructor
super().__init__()
self.image = pygame.Surface([20, 15])
self.image.fill(color)
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
class Diego(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("player.png").convert()
self.rect = self.image.get_rect()
class Apple(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("spr11.png").convert()
self.rect = self.image.get_rect()
self.image.set_colorkey(BLACK)
class Bala(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface([4, 10])
self.image.fill(BLACK)
self.rect = self.image.get_rect()
def update(self):
self.rect.y += -3
# Setup
pygame.init()
block_list = pygame.sprite.Group()
all_sprites_list = pygame.sprite.Group()
screen_width = 700
screen_height = 400
screen = pygame.display.set_mode([screen_width, screen_height])
bullet_list = pygame.sprite.Group()
#def block and include and track in the class Block
#for i in range(50):
# # This represents a block
# block = Block(BLUE)
# # Set a random location for the block
# block.rect.x = random.randrange(screen_width)
# block.rect.y = random.randrange(350)
# # Add the block to the list of objects
# block_list.add(block)
# all_sprites_list.add(block)
apple = pygame.image.load("spr11.png").convert()
block = Apple()
player_image = pygame.image.load("player.png").convert()
player = Diego()
block.rect.x = 200
block.rect.y = 200
block_list.add(block)
all_sprites_list.add(block)
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# Hide the mouse cursor
x_speed = 0
y_speed = 0
rect_x = 50
rect_y = 50
rect_change_x = 1
rect_change_y = 1
x = 0
y = 0
#player.rect.y = rect_x
#player.rect.y = rect_y
score = 0
# -------- Main Program Loop -----------
while not done:
x += x_speed
y += y_speed
# --- Event Processing
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
#bullet
elif event.type == pygame.MOUSEBUTTONDOWN:
# Fire a bullet if the user clicks the mouse button
bullet = Bala()
# Set the bullet so it is where the player is
bullet.rect.x = x
bullet.rect.y = y
# Add the bullet to the lists
all_sprites_list.add(bullet)
bullet_list.add(bullet)
# User pressed down on a key
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_speed = -3
elif event.key == pygame.K_RIGHT:
x_speed = 3
elif event.key == pygame.K_UP:
y_speed = -3
elif event.key == pygame.K_DOWN:
y_speed = 3
elif event.type == pygame.KEYUP:
# If it is an arrow key, reset vector back to zero
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_speed = 0
elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y_speed = 0
for bullet in bullet_list:
block_hit_list = pygame.sprite.spritecollide(bullet, block_list, True)
for block in block_hit_list:
bullet_list.remove(bullet)
all_sprites_list.remove(bullet)
score += 1
print(score)
print("BLUE")
print(time.time() - startTime)
if bullet.rect.y < -10:
bullet_list.remove(bullet)
all_sprites_list.remove(bullet)
all_sprites_list.update()
# --- Game Logic
# --- Drawing Code
# First, clear the screen to WHITE. Don't put other drawing commands
# above this, or they will be erased with this command.
screen.fill(WHITE)
# x += x_speed
# y += y_speed
all_sprites_list.draw(screen)
player_image.set_colorkey(BLACK)
apple.set_colorkey(BLACK)
screen.blit(player_image, [x, y])
pygame.draw.rect(screen, RED, [rect_x, rect_y, 10, 10])
rect_x += rect_change_x
rect_y += rect_change_y
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# Limit frames per second
clock.tick(60)
# Close the window and quit.
pygame.quit()
这里我的代码不能按预期工作
import pygame
import random
from pynput.keyboard import Listener, Key
import time
#store start timestamp
startTime = time.time()
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
#create window
size = (700, 500)
screen = pygame.display.set_mode(size)
#class
currentTime = time.time() - startTime
class Block(pygame.sprite.Sprite):
""" This class represents the block. """
def __init__(self, color):
# Call the parent class (Sprite) constructor
super().__init__()
self.image = pygame.Surface([20, 15])
self.image.fill(color)
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
class Diego(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("player.png").convert()
self.rect = self.image.get_rect()
class Apple(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("spr11.png").convert()
self.rect = self.image.get_rect()
self.image.set_colorkey(BLACK)
class Bala(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface([4, 10])
self.image.fill(BLACK)
self.rect = self.image.get_rect()
def update(self):
self.rect.y += -3
# Setup
pygame.init()
block_list = pygame.sprite.Group()
all_sprites_list = pygame.sprite.Group()
screen_width = 700
screen_height = 400
screen = pygame.display.set_mode([screen_width, screen_height])
bullet_list = pygame.sprite.Group()
#def block and include and track in the class Block
#for i in range(50):
# # This represents a block
# block = Block(BLUE)
# # Set a random location for the block
# block.rect.x = random.randrange(screen_width)
# block.rect.y = random.randrange(350)
# # Add the block to the list of objects
# block_list.add(block)
# all_sprites_list.add(block)
apple = pygame.image.load("spr11.png").convert()
block = Apple()
player_image = pygame.image.load("player.png").convert()
player = Diego()
block.rect.x = 200
block.rect.y = 200
block_list.add(block)
all_sprites_list.add(block)
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# Hide the mouse cursor
x_speed = 0
y_speed = 0
rect_x = 50
rect_y = 50
rect_change_x = 1
rect_change_y = 1
x = 0
y = 0
#player.rect.y = rect_x
#player.rect.y = rect_y
score = 0
# -------- Main Program Loop -----------
while not done:
x += x_speed
y += y_speed
# --- Event Processing
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
#bullet
elif event.type == pygame.MOUSEBUTTONDOWN:
# Fire a bullet if the user clicks the mouse button
bullet = Bala()
# Set the bullet so it is where the player is
bullet.rect.x = x
bullet.rect.y = y
# Add the bullet to the lists
all_sprites_list.add(bullet)
bullet_list.add(bullet)
# User pressed down on a key
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_speed = -3
elif event.key == pygame.K_RIGHT:
x_speed = 3
elif event.key == pygame.K_UP:
y_speed = -3
elif event.key == pygame.K_DOWN:
y_speed = 3
elif event.type == pygame.KEYUP:
# If it is an arrow key, reset vector back to zero
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_speed = 0
elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y_speed = 0
for bullet in bullet_list:
block_hit_list = pygame.sprite.spritecollide(bullet, block_list, True)
for block in block_hit_list:
bullet_list.remove(bullet)
all_sprites_list.remove(bullet)
score += 1
print(score)
print("BLUE")
print(currentTime)
if bullet.rect.y < -10:
bullet_list.remove(bullet)
all_sprites_list.remove(bullet)
all_sprites_list.update()
# --- Game Logic
# --- Drawing Code
# First, clear the screen to WHITE. Don't put other drawing commands
# above this, or they will be erased with this command.
screen.fill(WHITE)
# x += x_speed
# y += y_speed
all_sprites_list.draw(screen)
player_image.set_colorkey(BLACK)
apple.set_colorkey(BLACK)
screen.blit(player_image, [x, y])
pygame.draw.rect(screen, RED, [rect_x, rect_y, 10, 10])
rect_x += rect_change_x
rect_y += rect_change_y
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# Limit frames per second
clock.tick(60)
# Close the window and quit.
pygame.quit()