Changeset e79c833 in python-game
- Timestamp:
- Mar 24, 2017, 7:26:27 PM (8 years ago)
- Branches:
- master
- Children:
- 1af0f6d, 7145e24
- Parents:
- 1d1c77e
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
README.md
r1d1c77e re79c833 1 1 On linux, you need to install tkinter: 2 2 sudo apt-get install python3-tk 3 sudo apt-get install python3-opengl 3 4 4 5 On mac, install pygame like this: … … 11 12 pip3 install pygame 12 13 pip3 install pyobjc 14 15 Run the game with: 16 python3 game.py -
game.py
r1d1c77e re79c833 1 import pygame, platform 1 import pygame, OpenGL, math 2 3 from OpenGL.GLU import * 4 from OpenGL.GL import * 5 6 import platform 7 8 [w, h] = [0, 0] 9 10 def render(surface, overlay, font, state): 11 #surface.fill((0, 0, 0)) 12 13 #glEnable (GL_BLEND); glBlendFunc (GL_ONE, GL_ONE); 14 15 if state: 16 renderOverlay(overlay, font) 17 projectOverlay(surface, overlay) 18 else: 19 render3d(surface) 20 21 22 def renderOverlay(overlay, font): 23 #pygame.draw.rect(overlay, (0, 0, 255), pygame.Rect(0, 0, w, h)) 24 #pygame.draw.circle(overlay, (255, 0, 0), (int(w/2), int(h/2)), 540) 25 #overlay.set_colorkey((255,0,0)) 26 27 text = font.render("2D drawing", True, (0, 128, 0)) 28 29 pygame.draw.rect(overlay, (0, 128, 255), pygame.Rect(30, 30, 60, 60)) 30 overlay.blit(text, (500, 100)) 31 32 def projectOverlay(surface, overlay): 33 34 glPushMatrix() 35 glTranslatef(0.0,0.0,-zNear) 36 37 textureData = pygame.image.tostring(overlay, "RGBA", 1) 38 width = overlay.get_width() 39 height = overlay.get_height() 40 41 glEnable(GL_TEXTURE_2D) 42 43 glBindTexture(GL_TEXTURE_2D, glGenTextures(1)) 44 # maybe use GL_RGBA here instead 45 #glPixelStorei(GL_UNPACK_ALIGNMENT,1) 46 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData) 47 48 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) 49 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) 50 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) 51 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) 52 53 glBegin(GL_QUADS) 54 55 glTexCoord2f(0.0, 0.0) 56 glVertex2f(-(w/h), -1) 57 58 glTexCoord2f(1.0, 0.0) 59 glVertex2f((w/h), -1) 60 61 glTexCoord2f(1.0, 1.0) 62 glVertex2f((w/h), 1) 63 64 glTexCoord2f(0.0, 1.0) 65 glVertex2f(-(w/h), 1) 66 67 glEnd() 68 69 glPopMatrix() 70 71 def render3d(surface): 72 vertices = ( 73 (1, -1, -1), 74 (1, 1, -1), 75 (-1, 1, -1), 76 (-1, -1, -1), 77 (1, -1, 1), 78 (1, 1, 1), 79 (-1, -1, 1), 80 (-1, 1, 1) 81 ) 82 edges = ( 83 (0,1), 84 (0,3), 85 (0,4), 86 (2,1), 87 (2,3), 88 (2,7), 89 (6,3), 90 (6,4), 91 (6,7), 92 (5,1), 93 (5,4), 94 (5,7) 95 ) 96 97 glPushMatrix() 98 99 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) 100 101 glTranslatef(0.0,0.0,-2*zNear) 102 glColor3f(1.0, 1.0, 1.0) 103 104 glBegin(GL_LINES) 105 for edge in edges: 106 for vertex in edge: 107 glVertex3fv(vertices[vertex]) 108 glEnd() 109 110 glPopMatrix() 2 111 3 112 system = platform.system() 4 5 [w, h] = [0, 0]6 113 7 114 if system == 'Windows': … … 37 144 gameDisplay = pygame.display.set_mode((800, 600)) 38 145 else: 39 gameDisplay = pygame.display.set_mode((w, h), pygame.FULLSCREEN) 146 gameDisplay = pygame.display.set_mode((w, h), pygame.FULLSCREEN|pygame.DOUBLEBUF|pygame.OPENGL) 147 148 overlay = pygame.Surface((w, h)) 40 149 41 150 pygame.display.set_caption('Space Game') 42 151 43 152 clock = pygame.time.Clock() 153 154 font = pygame.font.SysFont("comicsansms", 48) 44 155 45 156 running = True 157 state = False 158 159 zNear = 1.0/math.tan(math.radians(45.0/2)) 160 gluPerspective(45, (w/h), zNear, 500.0) 161 162 ''' 163 # Use glFrustum to focus the camera at a point other than the screen center 164 zNear = 1.0 165 glFrustum(-1.5, 0.5, -1.0, 1.0, zNear, 500.0) 166 glTranslatef(0.0,0.0,-5.0) 167 ''' 46 168 47 169 while running: … … 52 174 if event.key == pygame.K_ESCAPE: 53 175 running = False 176 elif event.key == pygame.K_SPACE: 177 state = not state 54 178 55 179 #print(event) 56 180 57 pygame.display.update() 181 render(gameDisplay, overlay, font, state) 182 183 pygame.display.flip() 58 184 clock.tick(60) 59 185
Note:
See TracChangeset
for help on using the changeset viewer.