Changeset e79c833 in python-game


Ignore:
Timestamp:
Mar 24, 2017, 7:26:27 PM (8 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
master
Children:
1af0f6d, 7145e24
Parents:
1d1c77e
Message:

Add basic OpenGL (along with the ability to still use 2d pygame functions) and convert game.py to UNIX-style line endings

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • README.md

    r1d1c77e re79c833  
    11On linux, you need to install tkinter:
    22sudo apt-get install python3-tk
     3sudo apt-get install python3-opengl
    34
    45On mac, install pygame like this:
     
    1112pip3 install pygame
    1213pip3 install pyobjc
     14
     15Run the game with:
     16python3 game.py
  • game.py

    r1d1c77e re79c833  
    1 import pygame, platform
     1import pygame, OpenGL, math
     2
     3from OpenGL.GLU import *
     4from OpenGL.GL import *
     5
     6import platform
     7
     8[w, h] = [0, 0]
     9
     10def 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
     22def 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
     32def 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
     71def 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()
    2111
    3112system = platform.system()
    4 
    5 [w, h] = [0, 0]
    6113
    7114if system == 'Windows':
     
    37144   gameDisplay = pygame.display.set_mode((800, 600))
    38145else:
    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
     148overlay = pygame.Surface((w, h))
    40149
    41150pygame.display.set_caption('Space Game')
    42151
    43152clock = pygame.time.Clock()
     153   
     154font = pygame.font.SysFont("comicsansms", 48)
    44155
    45156running = True
     157state = False
     158
     159zNear = 1.0/math.tan(math.radians(45.0/2))
     160gluPerspective(45, (w/h), zNear, 500.0)
     161
     162'''
     163# Use glFrustum to focus the camera at a point other than the screen center
     164zNear = 1.0
     165glFrustum(-1.5, 0.5, -1.0, 1.0, zNear, 500.0)
     166glTranslatef(0.0,0.0,-5.0)
     167'''
    46168
    47169while running:
     
    52174         if event.key == pygame.K_ESCAPE:
    53175            running = False
     176         elif event.key == pygame.K_SPACE:
     177            state = not state
    54178
    55179      #print(event)
    56180
    57    pygame.display.update()
     181   render(gameDisplay, overlay, font, state)
     182
     183   pygame.display.flip()
    58184   clock.tick(60)
    59185
Note: See TracChangeset for help on using the changeset viewer.