Permalink
Browse files

Mostly ported to openGL

  • Loading branch information...
1 parent b100059 commit 2d8980a66ab8499513b1e804aaf49c8751098a3f namaste committed Apr 1, 2008
Showing with 117 additions and 46 deletions.
  1. +15 −2 soylent/weapon/Enemy.py
  2. +21 −0 soylent/weapon/Globals.py
  3. +23 −11 soylent/weapon/Hero.py
  4. +58 −33 soylent/weapon/Main.py
View
@@ -9,8 +9,8 @@ class Enemy(pygame.sprite.Sprite):
def __init__(self, rect=None):
pygame.sprite.Sprite.__init__(self)
- self.image = pygame.image.load("images/enemy1.png")
- self.rect = self.image.get_rect()
+ self.texture = g_EnemyTexture
+ self.rect = LoadTexture("images/enemy1.png", self.texture)
self.rect.center = rect.center
self.hp = 5000
self.armor = 1
@@ -19,6 +19,19 @@ def __init__(self, rect=None):
self.velocity = (0,0)
self.damage = 10
+ def Draw(self):
+ glBindTexture(GL_TEXTURE_2D, self.texture)
+ glPushMatrix()
+ glTranslatef(self.rect.centerx, self.rect.centery, 0)
+ glScalef(100, 100, 0)
+ glBegin(GL_QUADS)
+ glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, 1.0) # Bottom Left Of The Texture and Quad
+ glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0, 1.0) # Bottom Right Of The Texture and Quad
+ glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 1.0, 1.0) # Top Right Of The Texture and Quad
+ glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 1.0) # Top Left Of The Texture and Quad
+ glEnd()
+ glPopMatrix()
+
def Impact(self, mass, velocity, damage):
self.hp -= mass * sqrt((self.velocity[0] - velocity[0])**2 + (self.velocity[1] - velocity[1])**2)
self.velocity = self.velocity[0] + (mass/self.mass) * velocity[0], self.velocity[1] + (mass/self.mass) * velocity[1]
View
@@ -1,3 +1,24 @@
+import pygame
+from OpenGL.GL import *
+from OpenGL.GLU import *
+from pygame import *
+
g_screenWidth = 1024
g_screenHeight = 768
+g_worldWidth = 4000
+g_worldHeight = 3000
g_friction = 0.92
+g_HeroTexture = 0
+g_EnemyTexture = 1
+
+def LoadTexture(filename, texture):
+ surface = pygame.image.load(filename)
+ data = pygame.image.tostring(surface, "RGBA", 1)
+
+ glBindTexture(GL_TEXTURE_2D, texture)
+ glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, surface.get_width(), surface.get_height(), 0,
+ GL_RGBA, GL_UNSIGNED_BYTE, data );
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
+
+ return surface.get_rect()
View
@@ -9,9 +9,9 @@ class Hero(pygame.sprite.Sprite):
def __init__(self, position):
pygame.sprite.Sprite.__init__(self)
- self.image = pygame.image.load("images/ship1.png")
- self.jumpimage = pygame.image.load("images/ship1_jump.png")
- self.rect = self.image.get_rect()
+ """Load the Hero texture"""
+ self.texture = g_HeroTexture
+ self.rect = LoadTexture("images/ship1.png", self.texture)
self.rect.move_ip(position)
self.friendly_sprites = pygame.sprite.Group()
self.modules = []
@@ -32,11 +32,23 @@ def __init__(self, position):
self.airtime = 280 ##duration of jump
self.isJumping = False
- def Draw(self, surface):
+ def Draw(self):
+ glBindTexture(GL_TEXTURE_2D, self.texture)
+ glPushMatrix()
+ glTranslatef(self.rect.centerx, self.rect.centery, 0.0)
+ glScalef(self.rect.width, self.rect.height, 0.0)
+ glBegin(GL_QUADS)
+ glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, 1.0) # Bottom Left Of The Texture and Quad
+ glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0, 1.0) # Bottom Right Of The Texture and Quad
+ glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 1.0, 1.0) # Top Right Of The Texture and Quad
+ glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 1.0) # Top Left Of The Texture and Quad
+ glEnd()
+ glPopMatrix()
+
"""if jumping, draw the jumping image, otherwise draw the standard image"""
- if self.isJumping: surface.blit(self.jumpimage, self.rect.move(-16,-16))
- else: surface.blit(self.image, self.rect)
- self.friendly_sprites.draw(surface)
+# if self.isJumping: surface.blit(self.jumpimage, self.rect.move(-16,-16))
+# else: surface.blit(self.image, self.rect)
+# self.friendly_sprites.draw(surface)
def Update(self):
if self.isJumping and pygame.time.get_ticks() - self.startjump >= self.airtime:
@@ -64,14 +76,14 @@ def Move(self):
xMove = self.impulse
if pygame.key.get_pressed()[K_w]:
if pygame.key.get_pressed()[K_a] or pygame.key.get_pressed()[K_d]:
- yMove = -self.impulse / sqrt(2)
+ yMove = self.impulse / sqrt(2)
else:
- yMove = -self.impulse
+ yMove = self.impulse
if pygame.key.get_pressed()[K_s]:
if pygame.key.get_pressed()[K_a] or pygame.key.get_pressed()[K_d]:
- yMove = self.impulse / sqrt(2)
+ yMove = -self.impulse / sqrt(2)
else:
- yMove = self.impulse
+ yMove = -self.impulse
# if sqrt(self.velocity[0]**2 + self.velocity[0]**2) < self.max_speed: #cap speed
View
@@ -1,47 +1,41 @@
#! /usr/bin/env python
import os, sys
-import pygame
+#import pygame
from math import sqrt, sin, cos, pi
from random import *
from pygame import *
from Hero import *
from Enemy import *
+from OpenGL.GL import *
+from OpenGL.GLU import *
-
+rtri = rquad = 0.0
class GameMain:
"""The Main Game Class - This class handles the main
initialization and creating of the Game."""
def __init__(self):
- """Initialize PyGame"""
- pygame.init()
-
"""Set the window Size"""
self.width = g_screenWidth
self.height = g_screenHeight
- self.worldWidth = 2000
- self.worldHeight =1500
+ self.worldWidth = g_worldWidth
+ self.worldHeight = g_worldHeight
+ self.worldRect = pygame.Rect((0, 0), (self.worldWidth, self.worldHeight))
self.toggle_full_screen = False
- """Create the Screen"""
- self.screen = pygame.display.set_mode((self.width, self.height))
- self.world = pygame.Surface((self.worldWidth, self.worldHeight))
- """Create the background"""
- #self.background = pygame.image.load("images/floor1.png")
- #self.background = self.background.convert()
- self.background = pygame.Surface((self.worldWidth, self.worldHeight))
- self.background.fill((0,0,0))
- self.background = self.background.convert()
-
- """Create a blank fill region"""
- self.blank = pygame.Surface(self.screen.get_size())
- self.blank = self.blank.convert()
- self.blank.fill((0,0,0))
+ """Initialize PyGame"""
+ video_flags = OPENGL|DOUBLEBUF
+
+ pygame.init()
+ pygame.display.set_mode((self.width,self.height), video_flags)
- """create the Hero and friendly sprite group"""
- self.hero = Hero(self.world.get_rect().center)
+ self.resize((self.width, self.height))
+ self.init()
+
+ """create the hero and friends"""
+ self.hero = Hero(self.worldRect.center)
self.friendly_sprites = pygame.sprite.RenderPlain((self.hero))
"""create the enemies"""
@@ -51,7 +45,27 @@ def __init__(self):
seed()
pygame.time.set_timer(pygame.USEREVENT, 1000)
-
+ def resize(self, (width, height)):
+ if height==0:
+ height=1
+ glViewport(0, 0, width, height)
+ glMatrixMode(GL_PROJECTION)
+ glLoadIdentity()
+ gluPerspective(45, 1.0*width/height, 0.1, 1000.0)
+ glMatrixMode(GL_MODELVIEW)
+ glLoadIdentity()
+
+ def init(self):
+ glEnable(GL_TEXTURE_2D)
+ glEnable(GL_BLEND)
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
+ glShadeModel(GL_SMOOTH)
+ glClearColor(0.0, 0.0, 0.0, 0.0)
+ glClearDepth(1.0)
+ glEnable(GL_DEPTH_TEST)
+ glDepthFunc(GL_LEQUAL)
+ glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
+
def MainLoop(self):
frames = 0
ticks = pygame.time.get_ticks()
@@ -70,15 +84,26 @@ def MainLoop(self):
ticks = pygame.time.get_ticks()
def DrawAll(self):
- self.world.blit(self.background, (0, 0))
- self.enemy_sprites.draw(self.world)
- self.hero.Draw(self.world)
- s_rect = self.screen.get_rect()
- s_rect.center = self.hero.rect.center
- self.screen.blit(self.world, (0, 0), s_rect)
- """draw the gesture last"""
- self.hero.ges.Draw(self.screen)
+ glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
+ glLoadIdentity()
+
+ glTranslatef(-self.hero.rect.centerx, -self.hero.rect.centery, -1000.0)
+ for enemy in self.enemy_sprites:
+ enemy.Draw()
+ self.hero.Draw()
+ #self.hero.ges.Draw
+
pygame.display.flip()
+
+# self.world.blit(self.background, (0, 0))
+# self.enemy_sprites.draw(self.world)
+# self.hero.Draw(self.world)
+# s_rect = self.screen.get_rect()
+# s_rect.center = self.hero.rect.center
+# self.screen.blit(self.world, (0, 0), s_rect)
+# """draw the gesture last"""
+# self.hero.ges.Draw(self.screen)
+# pygame.display.flip()
def CheckCollisions(self):
"""friendlys colliding with enemies"""
@@ -111,7 +136,7 @@ def EventHandler(self):
"""timer event"""
if event.type == pygame.USEREVENT:
- self.enemy_sprites.add(Enemy(Rect(randrange(50, self.worldWidth - 50, 1),randrange(50, self.worldHeight- 50, 1),24,24)))
+ self.enemy_sprites.add(Enemy(Rect((randrange(50, self.worldWidth - 50, 1), randrange(50, self.worldHeight - 50, 1)) ,(24,24))))
"""end class GameMain"""
if __name__ == "__main__":

0 comments on commit 2d8980a

Please sign in to comment.