Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.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中检测不按键/不输入?_Python_Pygame_Key Bindings - Fatal编程技术网

如何在Python中检测不按键/不输入?

如何在Python中检测不按键/不输入?,python,pygame,key-bindings,Python,Pygame,Key Bindings,我正在制作一个简单的移动盒子的“游戏”来学习pygame的导入,但是我想在没有按键的情况下将盒子设置回灰色 import pygame from pygame.locals import * pygame.init() width = 400 height = 400 screen = pygame.display.set_mode((width,height)) done = False clock = pygame.time.Clock() color = (150, 150, 150) x

我正在制作一个简单的移动盒子的“游戏”来学习pygame的导入,但是我想在没有按键的情况下将盒子设置回灰色

import pygame
from pygame.locals import *
pygame.init()
width = 400
height = 400
screen = pygame.display.set_mode((width,height))
done = False
clock = pygame.time.Clock()
color = (150, 150, 150)
x = 0
y = 0
while not done:
    for event in pygame.event.get():
            if event.type == pygame.QUIT:
                    done = True
            if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
                    is_blue = not is_blue

    pressed = pygame.key.get_pressed()
    if pressed[pygame.K_UP] or pressed[pygame.K_w] and y > 0:
        y -= 3
        color = (0,0,255)
    if pressed[pygame.K_DOWN] or pressed[pygame.K_s] and y+60 < height: #Adding 60 because height of block = 60
        y += 3
        color = (255,255,0)
    if pressed[pygame.K_LEFT] or pressed[pygame.K_a] and x > 0:
        x -= 3
        color = (0,255,0)
    if pressed[pygame.K_RIGHT] or pressed[pygame.K_d] and x+60 < width: #Adding 60 because width of block = 60
        x += 3
        color = (255,0,0)
    screen.fill((0, 0, 0))
    pygame.draw.rect(screen, color, pygame.Rect(x, y, 60, 60))

    pygame.display.flip()
    clock.tick(60)
导入pygame
从pygame.locals导入*
pygame.init()
宽度=400
高度=400
screen=pygame.display.set_模式((宽度、高度))
完成=错误
clock=pygame.time.clock()
颜色=(150150150)
x=0
y=0
虽然没有这样做:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
完成=正确
如果event.type==pygame.KEYDOWN和event.key==pygame.K_空间:
is_blue=不是is_blue
pressed=pygame.key.get_pressed()
如果按下[pygame.K_UP]或[pygame.K_w]且y>0:
y-=3
颜色=(0,0255)
如果按[pygame.K_DOWN]或按[pygame.K_s]且y+60<高度:#添加60,因为块的高度=60
y+=3
颜色=(255255,0)
如果按[pygame.K_LEFT]或按[pygame.K_a]且x>0:
x-=3
颜色=(0255,0)
如果按[pygame.K_RIGHT]或按[pygame.K_d]和x+60
只需将
颜色=(150150)
的集合添加到
中,而
循环(:

把它作为循环中的第一行。

我想把它添加到最后一个显示的if语句之后。非常感谢!我只是在学习python语言,我想如果没有{}s(来自java),很容易错过一些东西!:-)它实际上是!python中的范围有时可能会令人困惑(尽管这不是我们的问题;),如果您能接受我的答案,而不仅仅是投票赞成,我将非常感激!(: