Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.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 3.x PyGame挡板不移动_Python 3.x_Pygame - Fatal编程技术网

Python 3.x PyGame挡板不移动

Python 3.x PyGame挡板不移动,python-3.x,pygame,Python 3.x,Pygame,在控制台上可以很好地注册密钥事件。另外,我的桨出现了,这表明从另一个文件调用的桨类可以工作。但是,我希望当按下左箭头键或右箭头键时,我的拨片能够平稳移动,这样用户就不必重复单击。但是,根据我当前的代码,拨杆根本不移动。我尝试过改变if/else语句的位置,但没有效果。我做错了什么 主要游戏: import pygame from pygame.locals import * from Config import Config from Paddle import Paddle pygame

在控制台上可以很好地注册密钥事件。另外,我的桨出现了,这表明从另一个文件调用的桨类可以工作。但是,我希望当按下左箭头键或右箭头键时,我的拨片能够平稳移动,这样用户就不必重复单击。但是,根据我当前的代码,拨杆根本不移动。我尝试过改变if/else语句的位置,但没有效果。我做错了什么

主要游戏:

import pygame
from pygame.locals import *

from Config import Config 
from Paddle import Paddle

pygame.init()

#--Display settings
pygame.display.set_caption(Config['game']['caption'])
game_display = pygame.display.set_mode((Config['game']['display_width'], 
Config['game']['display_height']))
clock = pygame.time.Clock()
paddle= Paddle(game_display)

x_change = 0
pressed_left = False
pressed_right = False

def event_handler():
for event in pygame.event.get():
    print (event)
    if (event.type == QUIT) or (event.type == KEYDOWN and event.key == 
K_ESCAPE):
        pygame.quit()
        quit()

    #To move the paddle to the left and right
    if event.type == KEYDOWN:
        if event.key == K_LEFT:
            pressed_left = True  
        elif event.key== K_RIGHT:
            pressed_right = True

    #If the key is up
    if event.type == KEYUP:
        if event.key == K_LEFT:
            pressed_left = False
            x_change = 0
        elif event.key == K_RIGHT:
            pressed_right = False 
            x_change = 0

#--This changes the position
if pressed_left:
    x_change = -5
if pressed_right:
    x_change = 5

while True:
    event_handler()
    game_display.fill(Config['colors']['white'])
    paddle.draw()
    paddle.movement(x_change)

    pygame.display.update()
    clock.tick(Config['game']['fps'])
从包含桨类的my Pable.py中:

import pygame
from pygame.locals import *

from Config import Config

class Paddle:
    def __init__(self, game_display):
        self.x = (Config['game']['display_width'] * 0.45)
        self.y = (Config['game']['display_height'] * 0.92)
        self.game_display=game_display


    def draw(self):
        pygame.draw.rect(self.game_display, Config['colors']['red'], 
[self.x, self.y, 40, 25])

    def movement(self, x_change):
        self.x += x_change

让我们看一个更简单的例子。试着运行这个程序

pressed_left = False
def event_handler():
    pressed_left = True

event_handler()

print(pressed_left)
pressed_left = False
def event_handler():
    global pressed_left
    pressed_left = True

event_handler()

print(pressed_left)
虽然您希望程序打印
True
,但它打印
False
。这是由于python中变量作用域的工作方式造成的。当您试图修改函数中未在该函数本身中定义的变量时,python会定义一个新变量,而不是查找全局变量。 因此,
event\u handler()
中的
pressed\u left=True
行只创建一个新的
pressed\u left
变量,该变量仅存在于
event\u handler
函数中,而不修改全局变量

要解决这个问题,您需要在函数中将变量声明为全局变量。试着运行这个程序

pressed_left = False
def event_handler():
    pressed_left = True

event_handler()

print(pressed_left)
pressed_left = False
def event_handler():
    global pressed_left
    pressed_left = True

event_handler()

print(pressed_left)

把你的钥匙换成这样

key = pygame.key.pressed()

if key[pygame.K_left]:
    pygame movement

为什么这是一个更好的选择?为什么这会解决“左按”和“右按”被注册为“假”的问题?因为玩家会不断移动,直到ey as停止被按下。我添加了这一点,因为这一解决方案是有意义的。但是,按下左键和按下右键仍被注册为False,并且按键在注册时不会将变量的值从False更改为True。不过非常感谢你!我更改了事件处理程序函数的顶部以包含全局声明。很抱歉,我正在尝试正确设置格式:@HasanatJahan您是否也可以共享配置文件,以便我可以尝试运行program@HasanatJahan为什么x_更改更新不是事件处理程序函数的一部分。它也不是在循环中运行的。我只是在事件处理程序函数中的x_change update中对它进行了更改,它成功了。桨在动。谢谢你善良的向导。