Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pygame:玩家获胜';按键按下时,请勿移动_Python_Pygame - Fatal编程技术网

Python Pygame:玩家获胜';按键按下时,请勿移动

Python Pygame:玩家获胜';按键按下时,请勿移动,python,pygame,Python,Pygame,从pygame开始,当你按下箭头键时,试着让一个简单的点在屏幕上移动。目前,它只在你按下键时移动,但你必须反复按下它 import random import pygame import keyboard import time from pygame.locals import * class Player: def __init__(self): self.x = 150 self.y = 150 pygame.init() screen = py

从pygame开始,当你按下箭头键时,试着让一个简单的点在屏幕上移动。目前,它只在你按下键时移动,但你必须反复按下它

import random
import pygame
import keyboard
import time
from pygame.locals import *

class Player:
    def __init__(self):
        self.x = 150
        self.y = 150

pygame.init()
screen = pygame.display.set_mode((400, 300))
pygame.display.set_caption("Smile, you're beautiful!")
player = Player()
while True:
    pygame.time.Clock().tick(60)
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                player.x += 5
            if event.key == pygame.K_DOWN:
                player.y += 5
            if event.key == pygame.K_LEFT:
                player.x -= 5
            if event.key == pygame.K_UP:
                player.y -= 5
        pygame.event.pump()
    pygame.display.flip()
    pygame.display.update()
    screen.fill((0,0,0))
    pygame.draw.circle(screen, (180, 180, 180), (player.x, player.y), 5)
此外,如果您能提供有关我当前代码的任何提示,我将不胜感激,这些提示可能会得到改进或更改,以提高效率。

您需要使用按键关闭事件,而不是按键关闭事件。通过这种方式,您可以知道当前在每个滴答声上按下的键

while True:
    pressed = pygame.key.get_pressed()
    if pressed[pygame.K_RIGHT]:
        player.x += 5
    if pressed[pygame.K_DOWN]:
        player.y += 5
    if pressed[pygame.K_LEFT]:
        player.x -= 5
    if pressed[pygame.K_UP]:
        player.y -= 5
您需要使用而不是按键关闭事件。通过这种方式,您可以知道当前在每个滴答声上按下的键

while True:
    pressed = pygame.key.get_pressed()
    if pressed[pygame.K_RIGHT]:
        player.x += 5
    if pressed[pygame.K_DOWN]:
        player.y += 5
    if pressed[pygame.K_LEFT]:
        player.x -= 5
    if pressed[pygame.K_UP]:
        player.y -= 5