如何在opengl和pygames中使用键盘键移动多维数据集?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在opengl和pygames中使用键盘键移动多维数据集?相关的知识,希望对你有一定的参考价值。
我正在尝试使用OpenGL和pygames制作3D蛇游戏。我能够生成一个立方体,并在随机位置“生成”它。但我无法弄清楚如何使用键盘移动立方体。后来我也应该可以增加更多的“身体碎片”,它们也朝着同一个方向移动。我希望有一个人可以帮助我。我会发布洞程序,但我是一个新手程序员,所以请不要因为我的“糟糕的编程”和我的英语而使我感到高兴。如果您对更多详细信息有任何疑问,请随时提出。我会尽快回复。
谢谢
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
import random
xr = 0
yr = 0
zr = 0
snakevertices = (
(2+xr, 0+yr, 0+zr),
(2+xr, 2+yr, 0+zr),
(0+xr, 2+yr, 0+zr),
(0+xr, 0+yr, 0+zr),
(2+xr, 0+yr, 2+zr),
(2+xr, 2+yr, 2+zr),
(0+xr, 0+yr, 2+zr),
(0+xr, 2+yr, 2+zr),
)
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),
(4,5,1,0),
(1,5,7,2),
(4,0,3,6),
)
snakecolors = (
(0.1,1,0.1),
(0.1,1,0.1),
(0.1,1,0.1),
(0.1,1,0.1),
(0.1,1,0.1),
(0.1,1,0.1),
(0.1,1,0.1),
(0.1,1,0.1),
(0.1,1,0.1),
(0.1,1,0.1),
(0.1,1,0.1),
(0.1,1,0.1),
)
def set_snakevertices(max_snakedistance):
x_snake_change = 1 + xr#random.randrange(-1,10)
y_snake_change = 1 + yr#random.randrange(-5,5)
z_snake_change = random.randrange(-15,-5)
new_snakevertices = []
for snakevert in snakevertices:
new_snakevert = []
new_snakex = snakevert[0] + x_snake_change
new_snakey = snakevert[1] + y_snake_change
new_snakez = snakevert[2] + z_snake_change
new_snakevert.append(new_snakex)
new_snakevert.append(new_snakey)
new_snakevert.append(new_snakez)
new_snakevertices.append(new_snakevert)
return new_snakevertices
def Snake(snakevertices):
glBegin(GL_QUADS)
for surface in surfaces:
for vertex in surface:
glColor3fv((0,1,0))
glVertex3fv(snakevertices[vertex])
glEnd()
glBegin(GL_LINES)
for edge in edges:
for vertex in edge:
glColor3fv((0,0,0))
glVertex3fv(snakevertices[vertex])
glEnd()
def main():
pygame.init()
display = (800,600)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
gluPerspective(45, (display[0]/display[1]), 0.1, 100.0)
x_koor = 0
y_koor = 0
glTranslatef(x_koor, y_koor, -5)
glRotatef(25, 0, 50, 0)
object_passed = False
max_snakedistance = 100
snake_dict = {}
for x in range(1): #anzahl würfel
snake_dict[x] = set_snakevertices(max_snakedistance)
while not object_passed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.type == pygame.K_a:
xr += -1
for x in range(1):
snake_dict[x] = set_snakevertices(max_snakedistance)
if event.type == pygame.K_d:
xr += 1
for x in range(1):
snake_dict[x] = set_snakevertices(max_snakedistance)
if event.type == pygame.K_w:
yr += -1
for x in range(1):
snake_dict[x] = set_snakevertices(max_snakedistance)
if event.type == pygame.K_s:
yr += 1
for x in range(1):
snake_dict[x] = set_snakevertices(max_snakedistance)
if event.type == pygame.K_o:
zr += -1
for x in range(1):
snake_dict[x] = set_snakevertices(max_snakedistance)
if event.type == pygame.K_l:
zr += 1
for x in range(1):
snake_dict[x] = set_snakevertices(max_snakedistance)
if event.key == pygame.K_m:
for x in range(1):
snake_dict[x] = set_snakevertices(max_snakedistance)
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 4:
glTranslatef(0,0,1.0)
if event.button == 5:
glTranslatef(0,0,-1.0)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glTranslatef(x_koor,y_koor,0)
for each_Snake in snake_dict:
Snake(snake_dict[each_Snake])
pygame.display.flip()
pygame.time.wait(1)
main()
pygame.quit()
quit()
答案
按下的键存储在key
成员中,而不是存储在type
对象的event
成员中:
当您检查按下的键时,将type
更改为key
:
EG
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT :
glTranslatef(-1.0,0,0.0)
if event.key == pygame.K_RIGHT :
glTranslatef(1.0,0,0.0)
if event.key == pygame.K_UP :
glTranslatef(0.0,0,-1.0)
if event.key == pygame.K_DOWN :
glTranslatef(0.0,0,1.0)
以上是关于如何在opengl和pygames中使用键盘键移动多维数据集?的主要内容,如果未能解决你的问题,请参考以下文章
旋转后的OpenGL python和pygame翻译不适用于mouselook和移动