Python Pygame操纵杆控制-屏幕未更新?

Python Pygame操纵杆控制-屏幕未更新?,python,pygame,joystick,Python,Pygame,Joystick,我正试图在连接PS4控制器的情况下在屏幕上移动一个给定的球。我能够连接控制器,并使其在移动左侧模拟棒时返回不同的值。我觉得屏幕好像只需要更新一下?我搞不清楚 以下是我得到的: 谢谢 import pygame def main(): pygame.init() size = width, height = 800, 800 black = 0, 0, 0 speed = [5,5] screen = pygame.display.set_

我正试图在连接PS4控制器的情况下在屏幕上移动一个给定的球。我能够连接控制器,并使其在移动左侧模拟棒时返回不同的值。我觉得屏幕好像只需要更新一下?我搞不清楚

以下是我得到的:

谢谢

import pygame

def main():

     pygame.init()

     size = width, height = 800, 800
     black = 0, 0, 0
     speed = [5,5]

     screen = pygame.display.set_mode(size)
     ball = pygame.image.load("ball1.jpg")
     ballrect = ball.get_rect()

     pygame.joystick.init()
     joysticks = [pygame.joystick.Joystick(x) for x in 
 range(pygame.joystick.get_count())]

     for joystick in joysticks:
         joystick.init()

     controller = joysticks[0]

     while True:

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

         ballrect.move(speed)
         if controller.get_axis(0) < -.5:
             speed[0] = -speed[0]

         if controller.get_axis(0) > .5:
             speed[0] = speed[0]

         if controller.get_axis(1) < -.5:
             speed[1] = speed[1]

         if controller.get_axis(1) > .5:
             speed[1] = -speed[1]

         screen.fill(black)
         screen.blit(ball, ballrect)
         pygame.display.flip()
导入pygame
def main():
pygame.init()
尺寸=宽度,高度=800800
黑色=0,0,0
速度=[5,5]
screen=pygame.display.set_模式(大小)
ball=pygame.image.load(“ball1.jpg”)
ballrect=ball.get_rect()
pygame.maggage.init()
操纵杆=[pygame.Goverage.Goverage(x)代表x英寸
范围(pygame.mogage.get_count())]
对于操纵手柄中的操纵手柄:
init()文件
控制器=操纵手柄[0]
尽管如此:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:sys.exit()
正确移动(速度)
如果控制器获得_轴(0)<-.5:
速度[0]=-速度[0]
如果控制器获得_轴(0)>.5:
速度[0]=速度[0]
如果控制器获得_轴(1)<-.5:
速度[1]=速度[1]
如果控制器获得_轴(1)>5:
速度[1]=-速度[1]
屏幕填充(黑色)
屏幕。blit(球、球)
pygame.display.flip()
返回必须分配给
ballrect
变量的新rect,例如
ballrect=ballrect.move(speed)
。或者使用
ballrect.move_ip(速度)
,在不创建新rect的情况下修改现有rect

控制器代码似乎也被破坏了。尝试这样做:

# x and y axis positions of the stick (between -1.0 and 1.0).
x_axis_pos = controller.get_axis(0)
y_axis_pos = controller.get_axis(1)
# Set the speed to a multiple of the axis positions.
if x_axis_pos > .5 or x_axis_pos < -.5:
    speed[0] = int(x_axis_pos * 5)
else:
    speed[0] = 0
if y_axis_pos > .5 or y_axis_pos < -.5:
    speed[1] = int(y_axis_pos * 5)
else:
    speed[1] = 0
#斗杆的x轴和y轴位置(介于-1.0和1.0之间)。
x_轴_位置=控制器。获取_轴(0)
y_轴_位置=控制器。获取_轴(1)
#将速度设置为轴位置的倍数。
如果x_轴位置>0.5或x_轴位置<-.5:
速度[0]=int(x轴位置*5)
其他:
速度[0]=0
如果y轴位置>0.5或y轴位置<0.5:
速度[1]=int(y轴位置*5)
其他:
速度[1]=0
返回必须分配给
ballrect
变量的新rect,例如
ballrect=ballrect.move(speed)
。或者使用
ballrect.move_ip(速度)
,在不创建新rect的情况下修改现有rect

控制器代码似乎也被破坏了。尝试这样做:

# x and y axis positions of the stick (between -1.0 and 1.0).
x_axis_pos = controller.get_axis(0)
y_axis_pos = controller.get_axis(1)
# Set the speed to a multiple of the axis positions.
if x_axis_pos > .5 or x_axis_pos < -.5:
    speed[0] = int(x_axis_pos * 5)
else:
    speed[0] = 0
if y_axis_pos > .5 or y_axis_pos < -.5:
    speed[1] = int(y_axis_pos * 5)
else:
    speed[1] = 0
#斗杆的x轴和y轴位置(介于-1.0和1.0之间)。
x_轴_位置=控制器。获取_轴(0)
y_轴_位置=控制器。获取_轴(1)
#将速度设置为轴位置的倍数。
如果x_轴位置>0.5或x_轴位置<-.5:
速度[0]=int(x轴位置*5)
其他:
速度[0]=0
如果y轴位置>0.5或y轴位置<0.5:
速度[1]=int(y轴位置*5)
其他:
速度[1]=0

我忘了提到,如果值不在
的x\u轴位置>0.5或x\u轴位置<-.5:
范围内,则需要将速度设置为0。只需添加两个
else
子句。我忘了提到,如果值不在
的x\u轴位置.5或x\u轴位置范围内,则需要将速度设置为0。只需添加两个
else
子句。