代码未按预期打印碰撞时刻
Code does not print colision moment as expected
澄清一下上下文:
我不确定发生了什么错误。我的代码被构建为使用 pygame 显示 window,其中包含 player 和 enemy 图像,以及自动滑下屏幕的红色表面。注:此代码包含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专家,所以我最终没有使用"good practices"如何编写玩家、子弹和敌人之间的关系逻辑以及它们各自在屏幕上的效果图。但我相信这并不影响我问题本身的合法性。
这是我的有效代码:
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()
据我所知,您永远不会更新 currenttime 变量。
所以只需添加
currentTime = time.time() - startTime
在你的主循环中,它应该可以工作。
澄清一下上下文:
我不确定发生了什么错误。我的代码被构建为使用 pygame 显示 window,其中包含 player 和 enemy 图像,以及自动滑下屏幕的红色表面。注:此代码包含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专家,所以我最终没有使用"good practices"如何编写玩家、子弹和敌人之间的关系逻辑以及它们各自在屏幕上的效果图。但我相信这并不影响我问题本身的合法性。
这是我的有效代码:
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()
据我所知,您永远不会更新 currenttime 变量。 所以只需添加
currentTime = time.time() - startTime
在你的主循环中,它应该可以工作。