首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何防止Pygame在两个while循环之间获取事件?

如何防止Pygame在两个while循环之间获取事件?
EN

Stack Overflow用户
提问于 2017-03-18 08:31:44
回答 1查看 152关注 0票数 0

我一直试图在树莓派上创建一个短跑游戏,但用户可以在倒计时时点击箭头键作弊。我如何才能阻止关键事件在实际的冲刺项目之外被收集?我猜我需要在"while distance < 64:循环“中添加一些东西。任何帮助都将不胜感激!

代码语言:javascript
运行
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2017-03-18 15:11:03

您应该看看pygame.event.clear()方法:http://www.pygame.org/docs/ref/event.html#pygame.event.clear。在您的情况下,这应该是可行的:

代码语言:javascript
运行
复制
...
pygame.event.clear()
while distance < 64:
    for event in pygame.event.get():
        ...

您还可以查看pygame.time模块:http://www.pygame.org/docs/ref/time.html

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42869096

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档