Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.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_Python 3.x_Pygame - Fatal编程技术网

Python pygame新手,画了一个长方形,为什么可以';我不能移动它吗?

Python pygame新手,画了一个长方形,为什么可以';我不能移动它吗?,python,python-3.x,pygame,Python,Python 3.x,Pygame,这是我的代码: import pygame, sys from pygame.locals import * pygame.init() window = pygame.display.set_mode((800, 600)) pygame.display.set_caption('window') black = (0,0,0) white = (255, 255, 255) logo = pygame.image.load('logo.png').convert_alpha() c

这是我的代码:

import pygame, sys
from pygame.locals import *

pygame.init()

window = pygame.display.set_mode((800, 600))
pygame.display.set_caption('window')

black = (0,0,0)
white = (255, 255, 255)

logo = pygame.image.load('logo.png').convert_alpha()

clock = pygame.time.Clock()

# Sprites
m1 = pygame.image.load('m1.png').convert_alpha()
m2 = pygame.image.load('m2.png').convert_alpha()


mci = 1

x, y = 0, 0

run = True

while run:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

        if event.type == pygame.KEYDOWN:
            if event.type == pygame.K_LEFT:
                x -= 10

            if event.type == pygame.K_RIGHT:
                x += 10

            if event.type == pygame.K_UP:
                y -= 10

            if event.type == pygame.K_DOWN:
                y += 10




        window.fill(white)

        pygame.draw.rect(window, black,(x,y, 50, 50))

        pygame.display.flip()

        clock.tick(10)
所有的东西都显示出来了,但是我不能用箭头键移动矩形,退出后我总是会出错,救命。。提前谢谢!
P.S我显然是在照搬教程,但我不确定我做错了什么?

我写的错误是

event.type
而不是
event.key

当你计算时,你需要使用

event.key==…
。您可能还想查看循环的嵌套,目前您有:

while running:
    for event in list_of_events:
        process_event
        draw_to_screen
        wait_a_while
这导致了另一个问题()中的问题。您可能想要的是更像:

while running:
    for event in list_of_events:
        process_event

    draw_to_screen
    wait_a_while
您可能还想更改
pygame.quit();sys.exit()
run=false
,然后在程序末尾添加
pygame.quit()