我制作了一个pygame游戏,其中2辆汽车在一个足球场上比赛,并试图进球。我添加了一个增强功能,只要按下向上按钮,倒计时计时器就会启动,而不再按下该键时,计时器就会停止计时。当计时器= 0时,升压功能停止。这种方法在70%到80%的时间内都有效,但是有一些小故障。
(故障1)有时,即使放开向上箭头,提升的速度也会保持不变,并且计时器也不会打开。
(故障2)另一个奇怪的故障是,当boosttimer变为0时,用户同时按住向上和向左的箭头,然后放开向左的箭头(向左或向右),提升的速度仍然显示。
我下面有用于增强功能的代码,希望有人可以提供帮助。
import pygame
from pygame.math import Vector2
import time
pygame.font.init()
pygame.init()
WIDTH = 1150
HEIGHT = 800
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Rocket League")
clock = pygame.time.Clock()
start_ticks = pygame.time.get_ticks()
REDGOAL = pygame.Surface((50, 150), pygame.SRCALPHA)
REDGOAL.fill((255, 0, 0))
redgoal_rect = REDGOAL.get_rect(topleft=(0, 340))
redgoal_mask = pygame.mask.from_surface(REDGOAL)
REDCAR_ORIGINAL = pygame.Surface((50, 30), pygame.SRCALPHA)
pygame.draw.polygon(
REDCAR_ORIGINAL, (255, 0, 0), [(0, 30), (50, 20), (50, 10), (0, 0)])
redangle = 180
redcar = pygame.transform.rotate(REDCAR_ORIGINAL, redangle)
redspeed = 5
dx = 0
dy = 0
x = 800
y = 500
pos_red = Vector2(x,y)
vel_red = Vector2(-redspeed,0)
redrect = redcar.get_rect(center=pos_red)
redangle = 180
redcar = pygame.transform.rotate(REDCAR_ORIGINAL, redangle)
mask_red = pygame.mask.from_surface(redcar)
def starttimer():
global timer
timer -= dt
def starttimer1():
global timer1
timer1 -= dt
def redboost():
font4 = pygame.font.Font(None, 72)
timer10 = int(timer)
timer11 = round(timer10, 1)
timer12 = str(timer11)
global timer13
timer13 = font4.render(timer12, False, (255,0,0))
timer = 3
dt = 1
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
redboost()
print(timer)
if redrect.top < 0 and vel_red.y < 0:
redrect.top = 0
pos_red = Vector2(redrect.center)
elif redrect.bottom > screen.get_height() and vel_red.y > 0:
redrect.bottom = screen.get_height()
pos_red = Vector2(redrect.center)
if redrect.left < 0 and vel_red.x < 0:
redrect.left = 0
pos_red = Vector2(redrect.center)
elif redrect.right > screen.get_width() and vel_red.x > 0:
redrect.right = screen.get_width()
pos_red = Vector2(redrect.center)
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
redangle += 5
vel_red.rotate_ip(-5)
redcar = pygame.transform.rotate(REDCAR_ORIGINAL, redangle)
redrect = redcar.get_rect(center=redrect.center)
mask_red = pygame.mask.from_surface(redcar)
elif keys[pygame.K_RIGHT]:
redangle -= 5
vel_red.rotate_ip(5)
redcar = pygame.transform.rotate(REDCAR_ORIGINAL, redangle)
redrect = redcar.get_rect(center=redrect.center)
mask_red = pygame.mask.from_surface(redcar)
elif keys[pygame.K_UP]:
vel_red.scale_to_length(10)
starttimer()
if keys[pygame.K_UP] and keys[pygame.K_RIGHT]:
starttimer()
elif keys[pygame.K_UP] and keys[pygame.K_LEFT]:
starttimer()
elif keys[pygame.K_UP] and keys[pygame.K_LEFT] and keys [pygame.K_RIGHT]:
starttimer()
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
vel_red.scale_to_length(5)
elif timer <= 0:
vel_red.scale_to_length(5)
elif keys[pygame.K_DOWN]:
vel_red.scale_to_length(2)
pos_red += vel_red
redrect.center = pos_red
if timer <= 0:
timer = 0
vel_red.scale_to_length(5)
elif timer == 0:
timer = 0
vel_red.scake_to_length(5)
if keys[pygame.K_UP] and timer == 0:
vel_red.scale_to_length(5)
screen.fill((50,200,50))
screen.blit(redcar, redrect)
screen.blit(timer13, (950,750))
pygame.display.flip()
pygame.display.update()
dt = clock.tick(30)/1000
答案 0 :(得分:1)
我将重新排列事件处理代码,并在事件循环而不是主循环中缩放速度矢量,以开始增强。因此,当按下↑时,缩放向量一次,并在释放键时将其重置。在while循环中,您仍然可以检查if keys[pygame.K_UP]:
以减少计时器,并if timer <= 0:
再次将向量缩放为5。
import pygame
from pygame.math import Vector2
pygame.init()
WIDTH = 1150
HEIGHT = 800
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Rocket League")
clock = pygame.time.Clock()
REDCAR_ORIGINAL = pygame.Surface((50, 30), pygame.SRCALPHA)
pygame.draw.polygon(
REDCAR_ORIGINAL, (255, 0, 0), [(0, 30), (50, 20), (50, 10), (0, 0)])
redangle = 180
redspeed = 5
x = 800
y = 500
pos_red = Vector2(x,y)
vel_red = Vector2(-redspeed,0)
redrect = redcar.get_rect(center=pos_red)
redangle = 180
redcar = pygame.transform.rotate(REDCAR_ORIGINAL, redangle)
mask_red = pygame.mask.from_surface(redcar)
timer = 3
dt = 1
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == pygame.KEYDOWN:
# Start boosting.
if event.key == pygame.K_UP:
vel_red.scale_to_length(10)
# Slow down.
elif event.key == pygame.K_DOWN:
vel_red.scale_to_length(2)
elif event.type == pygame.KEYUP:
# Stop boosting. Reset the velocity.
if event.key == pygame.K_UP:
vel_red.scale_to_length(5)
elif event.key == pygame.K_DOWN:
vel_red.scale_to_length(5)
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
redangle += 5
vel_red.rotate_ip(-5)
redcar = pygame.transform.rotate(REDCAR_ORIGINAL, redangle)
redrect = redcar.get_rect(center=redrect.center)
mask_red = pygame.mask.from_surface(redcar)
elif keys[pygame.K_RIGHT]:
redangle -= 5
vel_red.rotate_ip(5)
redcar = pygame.transform.rotate(REDCAR_ORIGINAL, redangle)
redrect = redcar.get_rect(center=redrect.center)
mask_red = pygame.mask.from_surface(redcar)
if keys[pygame.K_UP]: # If boosting.
timer -= dt
if timer <= 0: # Reset the velocity.
timer = 0
vel_red.scale_to_length(5)
# Movement.
pos_red += vel_red
redrect.center = pos_red
# Bounds checking.
if redrect.top < 0 and vel_red.y < 0:
redrect.top = 0
pos_red = Vector2(redrect.center)
elif redrect.bottom > screen.get_height() and vel_red.y > 0:
redrect.bottom = screen.get_height()
pos_red = Vector2(redrect.center)
if redrect.left < 0 and vel_red.x < 0:
redrect.left = 0
pos_red = Vector2(redrect.center)
elif redrect.right > screen.get_width() and vel_red.x > 0:
redrect.right = screen.get_width()
pos_red = Vector2(redrect.center)
screen.fill((50,200,50))
screen.blit(redcar, redrect)
pygame.display.set_caption('{:.2f}'.format(timer))
# Call either display.flip or display.update but not both.
pygame.display.flip()
dt = clock.tick(30)/1000
pygame.quit()