问题描述
我正在尝试创建一个我以前做过的项目,但除此之外,我将使用我学校免费提供的一个游戏站.问题是当我向上和向下移动操纵杆时,它显示相同的坐标.(如果你不明白我的意思,请看下图).然后我也不确定我需要在 if 语句中输入什么,以便它检查操纵杆是向上还是向下.我也很难考虑如何检查操纵杆是否没有方向.
我已经尝试使用 if 语句,如果操纵杆大于一个数字且小于另一个数字(第一个数字在操纵杆的上半部分,另一个数字表示它在下半部分)操纵杆它会向下移动.当前的 if 语句不会发出任何错误,但不起作用.我已经尝试使用 if 语句来检查它是否在中间,但我不太确定.
joystick_count = pygame.joystick.get_count()如果joystick_count == 0:# 没有操纵杆!print("错误,我没有找到任何操纵杆.")别的:# 使用操纵杆 #0 并初始化它操纵杆 = pygame.joystick.Joystick(0)游戏杆.init()如果 pygame.joystick.Joystick(0).get_axis(0) >= -0.0 并且 pygame.joystick.Joystick(0).get_axis(0)
实际结果是操纵杆向下移动时精灵不移动.
导入pygamepygame.init()大小 = (800,600)屏幕 = pygame.display.set_mode(size)时钟 = pygame.time.Clock()pos = [大小[0]/2,大小[1]/2]速度 = 5操纵杆 = 无完成 = 错误虽然没有完成:时钟滴答(60)对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:完成 = 真elif event.type == pygame.KEYDOWN:如果 event.key==pygame.K_RETURN:完成 = 真如果操纵杆:axis_x,axis_y = (joystick.get_axis(0), joystick.get_axis(1))如果 abs(axis_x) >0.1:pos[0] += 速度 *axis_x如果 abs(axis_y) >0.1:pos[1] += 速度 *axis_y别的:如果 pygame.joystick.get_count() >0:操纵杆 = pygame.joystick.Joystick(0)游戏杆.init()打印(操纵杆初始化")screen.fill((0, 0, 255))pygame.draw.rect(屏幕, (255,255,255), (*pos, 10, 10))pygame.display.flip()
I am trying to create a project that I have made before but apart from this time I am going to be using a play-station one remote I was given for free from my school. The problem that when I move the joystick upwards and downwards it shows the same coordinates.(If you do not understand what I mean then look at the picture below). Then also I am not sure what I wold need to put into the if statement so that it checks if the joystick is upwards or downwards. I am also having trouble thinking on how you would check if the joystick is going in no direction.
I have already tried using an if statement where if the joystick is more than one number and less than another one (the first number being in the top half of the joystick and the other number meaning that it is in the bottom half of the joystick it will move downwards. The current if statement does not give off any errors but does not work. I have tried an if statement to check if it is in the middle but I am not too sure about it.
joystick_count = pygame.joystick.get_count()
if joystick_count == 0:
# No joysticks!
print("Error, I didn't find any joysticks.")
else:
# Use joystick #0 and initialize it
joystick = pygame.joystick.Joystick(0)
joystick.init()
if pygame.joystick.Joystick(0).get_axis(0) >= -0.0 and pygame.joystick.Joystick(0).get_axis(0) <= 0.0:
player_one.speed_y = 5
elif pygame.joystick.Joystick(0).get_axis(0) > -0.1 and pygame.joystick.Joystick(0).get_axis(0) < -0.9:
player_one.speed_y = -5
elif pygame.joystick(0).get_axis(0) == 0.0:
player_one.speed_y = -5
#The first if statement checks if the joystick is up and the second one
#checks if the joystick is downwards
# the middle one checks if the if statement is in the middle (not too sure)
#player one and two speed is what gets added on each time
The actual results that are that the sprite does not move when the joystick is moved downwards.
First ensure that you have a joystick, by getting the number of joysticks by pygame.joystick.get_count()
. Initialize the joystick by pygame.joystick.Joystick.init
:
joystick = None
if pygame.joystick.get_count() > 0:
joystick = pygame.joystick.Joystick(0)
joystick.init()
Once a joystick is initialized, ist axis value can be get by pygame.joystick.Joystick.get_axis
. The value returned by this function is in range [-1, 1]. -1 for the maximum negative tilt and +1 for the maximum positive tilt.
Note, each analog stick of a gamepad or joystick has 2 axis, one for the horizontal direction and 1 for the vertical direction. Since the amount of the value returned by get_axis()
depends on the tilting of the analog stick you should multiply the speed by the value.
Further you should ignore values near 0 for the dead center, because of the inaccuracy of the hardware. This can be don by a simple check using the built in function abs(x)
e.g. abs(axisval) > 0.1
:
if joystick:
axis_x, axis_y = (joystick.get_axis(0), joystick.get_axis(1))
if abs(axis_x) > 0.1:
player_one.speed_x = 5 * axis_x
if abs(axis_y) > 0.1:
player_one.speed_y = 5 * axis_y
See the following simple demo app:
import pygame
pygame.init()
size = (800,600)
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
pos = [size[0]/2, size[1]/2]
speed = 5
joystick = None
done = False
while not done:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key==pygame.K_RETURN:
done = True
if joystick:
axis_x, axis_y = (joystick.get_axis(0), joystick.get_axis(1))
if abs(axis_x) > 0.1:
pos[0] += speed * axis_x
if abs(axis_y) > 0.1:
pos[1] += speed * axis_y
else:
if pygame.joystick.get_count() > 0:
joystick = pygame.joystick.Joystick(0)
joystick.init()
print("joystick initialized")
screen.fill((0, 0, 255))
pygame.draw.rect(screen, (255,255,255), (*pos, 10, 10))
pygame.display.flip()
这篇关于如何在pygame中使用操纵杆使精灵上下移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!