我一直试图在树莓派上创建一个短跑游戏,但用户可以在倒计时时点击箭头键作弊。我如何才能阻止关键事件在实际的冲刺项目之外被收集?我猜我需要在"while distance < 64:循环“中添加一些东西。任何帮助都将不胜感激!
import pygame
import time
from time import sleep
from sense_hat import SenseHat
sense = SenseHat()
pygame.init()
pygame.display.set_mode((140,180))
high_score = 50
name = "no-one"
foot = 1 #only lets you start with your left foot
red = (80, 0, 0)
blue = (0, 0, 80)
gameStart = False
while True:
print ("READY!")
sleep(1)
print("SET!")
sleep(2)
print("GO!")
events = 0
distance = int(0)
start_time = time.time()
#sets the distance of the race (64) pixels
while distance < 64:
#Getting the Key input
events = pygame.event.get()
for event in events:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and foot == 1:
print("left")
foot += 1
distance += 1
elif event.key == pygame.K_RIGHT and foot == 2:
print("right")
foot -= 1
distance += 1
if distance == 32:
print("Halfway There!")
#Makes the pixels red if distance is less than the pixel number
pixels = [blue if i < distance else red for i in range(64)]
sense.set_pixels(pixels)
#subtracts your finish time by your start time
fin_time = time.time()-start_time
#rounds your finish time and start time to closest 100th of a second
fin_time = round(fin_time,2)
print("Finish!")
print (fin_time)
#checks to see if your time was a high score
if fin_time < high_score:
print("You got a high score!")
name = input("Please Enter your name: ")
high_score = fin_time
print("High Score", name, high_score)
#gives time to prepare for next round
sleep(5)
print("Click back on the black pygame window")
sleep(1)
print("Next round in...")
print("5")
sleep(1)
print("4")
sleep(1)
print("3")
sleep(1)
print("2")
sleep(1)
print("1")
sleep(1)发布于 2017-03-18 15:11:03
您应该看看pygame.event.clear()方法:http://www.pygame.org/docs/ref/event.html#pygame.event.clear。在您的情况下,这应该是可行的:
...
pygame.event.clear()
while distance < 64:
for event in pygame.event.get():
...您还可以查看pygame.time模块:http://www.pygame.org/docs/ref/time.html。
https://stackoverflow.com/questions/42869096
复制相似问题