在 Pygame 中使用 WASD 移动立方体?
Posted
技术标签:
【中文标题】在 Pygame 中使用 WASD 移动立方体?【英文标题】:Move around cube using WASD in Pygame? 【发布时间】:2020-11-17 19:19:43 【问题描述】:问题是当我按下一个键时,相机会继续移动,而不是当我按住键时它会移动,然后它会停在某个地方。我该如何解决?这是我导入 opengl 模块并为立方体创建顶点边缘和曲面的项目的代码。我正在尝试使用 pygame 和 pyopengl 创建一个立方体,我可以在其中通过 WASD 移动在立方体周围移动。
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
verts = (
(1, -1, -1),
(1, 1, -1),
(-1, 1, -1),
(-1, -1, -1),
(1, -1, 1),
(1, 1, 1),
(-1, -1, 1),
(-1, 1, 1),
)
edges = (
(0,1),
(0,3),
(0,4),
(2,1),
(2,3),
(2,7),
(6,3),
(6,4),
(6,7),
(5,1),
(5,4),
(5,7),
)
surfaces = (
(0, 1, 2, 3),
(3, 2, 7, 6),
(6, 7, 5, 4),
(1, 5, 7, 2),
(4, 5, 1, 0),
(4, 0, 3, 6),
)
colors = (
(1, 0, 0),
(0, 1, 0),
(0, 0, 1),
(0,0,0),
(1,1,1),
(0,1,1),
(1, 0, 0),
(0, 1, 0),
(0, 0, 1),
(0,0,0),
(1,1,1),
(0,1,1),
)
def Cube():
glBegin(GL_QUADS)
x = 0
for surface in surfaces:
x+=1
for vertex in surface:
glColor3fv(colors[x])
glVertex3fv(verts[vertex])
glEnd()
glBegin(GL_LINES)
for edge in edges:
for vertex in edge:
glVertex3fv(verts[vertex])
glEnd()
def main():
pygame.init()
display = (1280, 720)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
glRotatef(0, 0, 0, 0)
pos = 0.0
back = 0.0
glTranslatef(pos, 0.0, back)
while True:
pressed_left = False
pressed_right = False
pressed_up = False
pressed_down = False
glTranslatef(pos, 0.0, back)
x_move = 0
y_move = 0
#camera
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
pressed_left = True
elif event.key == pygame.K_RIGHT:
pressed_right = True
elif event.key == pygame.K_DOWN:
pressed_down = True
elif event.key == pygame.K_UP:
pressed_up = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
pressed_left = False
print('off key')
elif event.key == pygame.K_RIGHT:
pressed_right = False
print('off key')
if event.key == pygame.K_UP:
pressed_up = False
print('off key')
elif event.key == pygame.K_DOWN:
pressed_down = False
print('off key')
if pressed_left == True:
pos = -0.1
print('key left')
elif pressed_right == True:
pos = 0.1
print('key right')
elif pressed_up == True:
back = 0.1
print('key up')
elif pressed_down == True:
back = -0.1
print('key down')
else:
pos = pos
back = back
#glRotatef(1, 3, 1, 1)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
Cube()
pygame.display.flip()
pygame.time.wait(10)
main()
【问题讨论】:
【参考方案1】:像glTranslatef
这样的传统 OpenGL 矩阵变换不设置变换。矩阵运算定义了一个新的变换矩阵,并将当前的变换矩阵乘以新的矩阵。
键盘事件(参见pygame.event 模块)仅在按键状态更改时发生一次。每次按下某个键时,KEYDOWN
事件就会发生一次。 KEYUP
每次释放一个键时都会出现一次。pygame.key.get_pressed()
返回一个包含每个键状态的列表。如果按住某个键,则该键的状态为True
,否则为False
。使用pygame.key.get_pressed()
评估按钮的当前状态并在按住某个键时获得连续移动:
def main():
pygame.init()
display = (1280, 720)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
clock = pygame.time.Clock()
glEnable(GL_DEPTH_TEST)
glMatrixMode(GL_PROJECTION)
gluPerspective(90, (display[0]/display[1]), 0.1, 50.0)
glMatrixMode(GL_MODELVIEW)
pos, back = 0.0, -5.0
glTranslatef(pos, 0.0, back)
while True:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
glTranslatef(-0.1, 0.0, 0.0)
print('key left')
elif keys[pygame.K_RIGHT]:
glTranslatef(0.1, 0.0, 0.0)
print('key right')
elif keys[pygame.K_UP]:
glTranslatef(0.0, 0.1, 0.0)
print('key up')
elif keys[pygame.K_DOWN]:
glTranslatef(0.0, -0.1, 0.0)
print('key down')
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
Cube()
pygame.display.flip()
【讨论】:
以上是关于在 Pygame 中使用 WASD 移动立方体?的主要内容,如果未能解决你的问题,请参考以下文章