我目前正在使用PyOpenGL和PyGame为自上而下的2D侧卷轴编写代码。
下面显示的代码将在窗口中生成2D图像。
添加的图像是背景。我现在想使用OpenGL将sprite添加到后台。换句话说,我想将一个2D图像叠加在另一个上面。我似乎无法找到一个好方法。
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
from screenDictionary import *
### Initialize variables ###
gameOn = True
dispWidth = 1920
dispHeight = 1024
bgImg = 'A1_64.png'
bgImg2 = 'HookedFront64.png'
transTuple = (255, 0, 255)
blackTuple = (100, 100, 100)
screenX = 0
screenY = 0
### Initialize display ###
def createDisplay(dispWidth, dispHeight):
pygame.init()
screen = pygame.display.set_mode((dispWidth, dispHeight), DOUBLEBUF|OPENGL)
pygame.display.set_caption('Hooked')
return screen
def createSurfaces(screen):
background = pygame.Surface(screen.get_size())
background.fill(blackTuple)
sprite = pygame.Surface(screen.get_size())
sprite.set_colorkey(transTuple)
return background
def loadScene(bgImg):
img = pygame.image.load(bgImg)
textureData = pygame.image.tostring(img, "RGB", 1)
width = img.get_width()
height = img.get_height()
bgImgGL = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, bgImgGL)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, textureData)
glEnable(GL_TEXTURE_2D)
def placeScene():
glLoadIdentity()
gluPerspective(90, 1, 0.05, 100)
glTranslatef(0,0,0)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glBegin(GL_QUADS)
glTexCoord2f(0,0)
glVertex3f(-4,-4,-4)
glTexCoord2f(0,1)
glVertex3f(-4,4,-4)
glTexCoord2f(1,1)
glVertex3f(4,4,-4)
glTexCoord2f(1,0)
glVertex3f(4,-4,-4)
glEnd()
screen = createDisplay(dispWidth, dispHeight)
background = createSurfaces(screen)
mapParams = screenDictionary(screenX, screenY)
bgImg = mapParams[0]
loadScene(bgImg)
loadScene(bgImg2)
loadScene(bgImg)
###Run the game###
while gameOn:
placeScene()
pygame.display.flip()
pygame.time.wait(1)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
gameOn = False
答案 0 :(得分:0)
问题解决了。
所做的更改:使用gluOrtho2D代替gluPerspective。使用glVertex2f而不是glVertex3f。
最重要的变化:删除了glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) 每次执行时都会清理现场... DUH!还在学习openGL,但是会留下这个以防万一其他人像我一样愚蠢并遇到类似的问题。