【发布时间】:2017-06-19 16:08:25
【问题描述】:
我试图在连接了 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()
【问题讨论】: