简体   繁体   English

有人可以告诉我这个 python 代码哪里错了吗?

[英]Can someone please tell me where am I wrong in this python code?

Here's a simple pygame code where I inserted a screen, a pink rectangle and tried moving it.这是一个简单的 pygame 代码,我在其中插入了一个屏幕,一个粉红色的矩形并尝试移动它。 The rectangle in the pygame window isn't moving. pygame window 中的矩形没有移动。 Which means the code inside '**' isn't working.这意味着 '**' 中的代码不起作用。 How do I solve that?我该如何解决?

import pygame, sys

pygame.init()

width = 800
height = 600

pink = (244,133,227)

player_pos = [400, 300]
player_size = 50

screen = pygame.display.set_mode((width,height))

game_over = False

while not game_over:

    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            sys.exit()

**      if event.type == pygame.KEYDOWN:
            x = player_pos[0]
            y = player_pos[1]
            if event.type == pygame.K_LEFT:
                x -= player_size
            elif event.type == pygame.K_RIGHT:
                x += player_size
            player_pos = [x, y]
    screen.fill((0,0,0))  **

    pygame.draw.rect(screen, pink, (player_pos[0], player_pos[1], player_size, player_size))

    pygame.display.update()

The key is stored in the key attribute, rather then the type attribute.密钥存储在key属性中,而不是type属性中。 See pygame.event :请参阅pygame.event

if event.type== pygame.K_LEFT:

if event.key == pygame.K_LEFT:

See the example:请参阅示例:

import pygame, sys

pygame.init()

width = 800
height = 600
pink = (244,133,227)
player_pos = [400, 300]
player_size = 50
screen = pygame.display.set_mode((width,height))

game_over = False
while not game_over:

    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            sys.exit()

        if event.type == pygame.KEYDOWN:
            x = player_pos[0]
            y = player_pos[1]
            if event.key == pygame.K_LEFT:
                x -= player_size
            elif event.key == pygame.K_RIGHT:
                x += player_size
            player_pos = [x, y]

    screen.fill((0,0,0))  
    pygame.draw.rect(screen, pink, (player_pos[0], player_pos[1], player_size, player_size))
    pygame.display.update()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有人可以告诉我我做错了什么新到 Python - Can someone please tell me what am I doing wrong New to Python 我不明白为什么这段代码不起作用! 有人可以告诉我我做错了吗? - I don't see why this code is not working! can someone please tell me what i am doing wrong? 有人可以告诉我这段代码有什么问题吗 - Can someone please tell me whats wrong with this code 有人可以告诉我我做错了什么吗? - Can someone tell me what am I doing wrong? 有人可以告诉我我的代码有什么问题吗? 我在调试时遇到问题 - Can someone tell me what is wrong with my code? I am having problems debugging 有人可以告诉我这种结构在哪里出错吗? - Can someone tell me where I'm going wrong with this structure? 我正在编写将二进制数转换为十进制数的代码,这就是我所做的,有人可以告诉我哪里错了,(我进入 Python 1 个月 - I am making a code to turn a Binary number into decimal number, This is what I have done, Can someone tell where I am wrong, (i am 1 month into Python 有人能告诉我这个 python 代码有什么问题吗? - Can someone tell me what is wrong with this python code? 索引超出范围错误,有人可以告诉我它出了什么问题 - Index out of range error , Can someone please tell me where Its going wrong 我是python的初学者,有人可以详细解释一下这段代码吗? - I am a beginner in python, can someone please explain me this piece of code in detail?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM