我正在尝试使用python,pygame和OpenGL制作游戏,我想在屏幕中央制作一个十字准线,以便当他们环顾四周时,他们可以看到要点击的内容。我做到了,所以它隐藏了他们的光标并将其移动到窗口的中心,但是我不知道如何在pygame中的opengl上制作形状。我是pygame和opengl的初学者。这可能真的很容易,但我所看到的却找不到答案。
请先帮助您,然后先谢谢您。
如果您想确保自己的工作在我的情况下能够正常工作,请点击这里。
import pygame
import time
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
block = [
[0,1,0],
[1,0,0],
[1,0,1],
[0,0,1],
[1,1,0],
[0,1,1]
]
def Cube(x,y,z):
glBegin(GL_TRIANGLES)
glColor3f(block[0][0], block[0][1], block[0][2]) # N
glVertex3f(x + .5, y + 1, z + .5)
glVertex3f(x - .5, y + 1, z + .5)
glVertex3f(x - .5, y, z + .5)
glVertex3f(x - .5, y, z + .5)
glVertex3f(x + .5, y, z + .5)
glVertex3f(x + .5, y + 1, z + .5)
glColor3f(block[1][0], block[1][1], block[1][2]) # S
glVertex3f(x - .5, y + 1, z - .5)
glVertex3f(x + .5, y + 1, z - .5)
glVertex3f(x + .5, y, z - .5)
glVertex3f(x + .5, y, z - .5)
glVertex3f(x - .5, y, z - .5)
glVertex3f(x - .5, y + 1, z - .5)
glColor3f(block[2][0], block[2][1], block[2][2]) # W
glVertex3f(x + .5, y + 1, z - .5)
glVertex3f(x + .5, y + 1, z + .5)
glVertex3f(x + .5, y, z + .5)
glVertex3f(x + .5, y, z + .5)
glVertex3f(x + .5, y, z - .5)
glVertex3f(x + .5, y + 1, z - .5)
glColor3f(block[3][0], block[3][1], block[3][2]) # E
glVertex3f(x - .5, y + 1, z + .5)
glVertex3f(x - .5, y + 1, z - .5)
glVertex3f(x - .5, y, z - .5)
glVertex3f(x - .5, y, z - .5)
glVertex3f(x - .5, y, z + .5)
glVertex3f(x - .5, y + 1, z + .5)
glColor3f(block[4][0], block[4][1], block[4][2]) # U
glVertex3f(x + .5, y + 1, z - .5)
glVertex3f(x - .5, y + 1, z - .5)
glVertex3f(x - .5, y + 1, z + .5)
glVertex3f(x - .5, y + 1, z + .5)
glVertex3f(x + .5, y + 1, z + .5)
glVertex3f(x + .5, y + 1, z - .5)
glColor3f(block[5][0], block[5][1], block[5][2]) # D
glVertex3f(x + .5, y, z - .5)
glVertex3f(x + .5, y, z + .5)
glVertex3f(x - .5, y, z + .5)
glVertex3f(x - .5, y, z + .5)
glVertex3f(x - .5, y, z - .5)
glVertex3f(x + .5, y, z - .5)
glEnd()
def main():
controls = False
pygame.init()
display = (800,600)
pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
glClearColor(0.0, 0.0, 0.0, 0.0)
glClearDepth(1.0)
glDepthMask(GL_TRUE)
glDepthFunc(GL_LESS)
glEnable(GL_DEPTH_TEST)
glEnable(GL_CULL_FACE)
glCullFace(GL_BACK)
glFrontFace(GL_CCW)
glShadeModel(GL_SMOOTH)
glDepthRange(0.0,1.0)
gluPerspective(45, (display[0]/display[1]), 0.1, 100.0)
glTranslatef(0.0, 0.0, -5)
while True:
if controls:
pygame.mouse.set_pos([400, 300])
pygame.mouse.set_visible(False)
else:
pygame.mouse.set_visible(True)
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
controls = not controls
if event.type == pygame.QUIT:
pygame.quit()
quit()
glRotatef(1, 3, 1, 1,)
glClearDepth(1.0)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
Cube(0,0,0)
Cube(0,1,0)
Cube(1,1,1)
glCullFace(GL_BACK)
pygame.display.flip()
time.sleep(.01)
main()
答案 0 :(得分:2)
请注意,数十年来,不赞成使用glBegin
/glEnd
序列和固定函数矩阵堆栈进行绘制。
无论如何,我建议使用正交投影绘制head-up display元素。
创建一个在窗口坐标中绘制十字线的函数:
def Crosshair(x, y, w):
glColor3f(1.0, 1.0, 1.0)
glBegin(GL_LINES)
glVertex2f(x-w, y)
glVertex2f(x+w, y)
glVertex2f(x, y-w)
glVertex2f(x, y+w)
glEnd()
将投影矩阵应用于投影矩阵堆栈,并将模型矩阵应用于模型视图矩阵堆栈。参见glMatrixMode
:
glMatrixMode(GL_PROJECTION)
gluPerspective(45, (display[0]/display[1]), 0.1, 100.0)
glMatrixMode(GL_MODELVIEW)
glTranslatef(0.0, 0.0, -5)
由于十字线始终应位于视图顶部,因此必须禁用depth test。
glDisable(GL_DEPTH_TEST)
mX, mY = pygame.mouse.get_pos()
Crosshair(mX, 600-mY, 20)
glEnable(GL_DEPTH_TEST)
可能您可以将深度函数(glDepthFunc
)更改为GL_ALWAYS
并更改glDepthMask
使用glLoadIdentity
来套叠identity matrix并使用glOrtho
设置与窗口坐标1:1的正交投影;
glLoadIdentity()
glOrtho(0.0, display[0], 0.0, display[1], -1.0, 1.0)
使用glPushMatrix
/glPopMatrix
在矩阵堆栈上存储和恢复矩阵:
glMatrixMode(GL_PROJECTION)
gluPerspective(45, (display[0]/display[1]), 0.1, 100.0)
glMatrixMode(GL_MODELVIEW)
glTranslatef(0.0, 0.0, -5)
while True:
if controls:
pygame.mouse.set_pos([400, 300])
pygame.mouse.set_visible(False)
else:
pygame.mouse.set_visible(True)
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
controls = not controls
if event.type == pygame.QUIT:
pygame.quit()
quit()
glClearDepth(1.0)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glRotate(1, 3, 1, 1,)
Cube(0,0,0)
Cube(0,1,0)
Cube(1,1,1)
glMatrixMode(GL_PROJECTION)
glPushMatrix()
glLoadIdentity()
glOrtho(0.0, display[0], 0.0, display[1], -1.0, 1.0)
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
glLoadIdentity()
glDisable(GL_DEPTH_TEST)
mX, mY = pygame.mouse.get_pos()
Crosshair(mX, 600-mY, 20)
glEnable(GL_DEPTH_TEST)
glMatrixMode(GL_PROJECTION)
glPopMatrix()
glMatrixMode(GL_MODELVIEW)
glPopMatrix()
glCullFace(GL_BACK)
pygame.display.flip()
time.sleep(.01)