繁体   English   中英

Pygame操纵杆控制 - 屏幕没有更新?

[英]Pygame Joystick Control - Screen not updating?

我试图在连接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()

Rect.move返回一个你必须分配给ballrect变量的新rect,例如ballrect = ballrect.move(speed) 或者使用ballrect.move_ip(speed)修改现有的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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM