Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 268 lines (193 sloc) 6.109 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
#!/usr/bin/python
import math
import random

import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
import font

def center(x, y, text): font.put(x - len(text) * 8 + 8, y, text)


class Sprite:

SCALE_FACTOR = 5

def __init__(self, img_name):

img = pygame.image.load(img_name)
w, h = img.get_size()
ws = w * self.SCALE_FACTOR
hs = h * self.SCALE_FACTOR
img = pygame.transform.scale(img, (ws, hs))
data = pygame.image.tostring(img, "RGBA", True)

self.tex = glGenTextures(1)

glBindTexture(GL_TEXTURE_2D, self.tex)
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, ws, hs, GL_RGBA, GL_UNSIGNED_BYTE, data)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR)

self.size = h
self.frames = w / h
self.list = glGenLists(self.frames)

s = h * 0.5
f = 1 / float(self.frames)

for i in range(self.frames):
glNewList(self.list + i, GL_COMPILE)
glBegin(GL_QUADS)
glTexCoord2f(i * f, 0)
glVertex2d(-s, -s)
glTexCoord2f((i + 1) * f, 0)
glVertex2d(s, -s)
glTexCoord2f((i + 1) * f, 1)
glVertex2d(s, s)
glTexCoord2f(i * f, 1)
glVertex2d(-s, s)
glEnd()
glEndList()

def draw(self, x, y, r=0, s=1, frame=0):
glBindTexture(GL_TEXTURE_2D, self.tex)

glPushMatrix()

glTranslate(x, y, 0)
glScale(s, s, 1)
glRotate(r, 0, 0, 1)
glCallList(self.list + frame)
glPopMatrix()


class Engine:

def __init__(self):

pygame.display.init()
# pygame.display.set_mode((0, 0), pygame.OPENGL | pygame.FULLSCREEN | pygame.DOUBLEBUF)
pygame.display.set_mode((800, 600), pygame.OPENGL | pygame.DOUBLEBUF)

pygame.display.set_caption("the beer game, yea!")
pygame.mouse.set_visible(False)

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glEnable(GL_LINE_SMOOTH)
glEnable(GL_BLEND)
glEnable(GL_ALPHA_TEST)
glEnable(GL_TEXTURE_2D)

glClearColor(0.2, 0.2, 0.2, 0.2)

glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluOrtho2D(-400, 400, -300, 300)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()

pygame.mixer.init()

self.keys = {}
self.running = True

def handle_events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT: self.running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE: self.running = False
self.keys[event.key] = 1
elif event.type == pygame.KEYUP:
self.keys[event.key] = 0

def keystate(self, key, kill=False):
if type(key) == str: key = ord(key)
c = self.keys.get(key, 0)
if kill: self.keys[key] = 0
return c



class Main:
def __init__(self):
global engine, level, mario
engine = Engine()

font.init()

self.bam_sound = pygame.mixer.Sound("bam.wav")
self.select_sound = pygame.mixer.Sound("select.wav")
self.collect_sound = pygame.mixer.Sound("collect.wav")

self.bottle_sprite = Sprite("bottle.png")
self.segment_sprite = Sprite("segment.png")
self.head_sprite = Sprite("head.png")

self.state = 0
self.ticks = 0

def start_values(self):

self.set_bottle()
self.segments = []
self.worm_x = 0
self.worm_y = 0
self.worm_dir = 0

self.speed = 3
self.length = 20

self.score = 0


def set_bottle(self):
self.bottle_x = random.randint(-380, 380)
self.bottle_y = random.randint(-280, 280)


def end_screen(self):

glColor(1, 1, 1)

glPushMatrix()
glLoadIdentity()
glScale(4, 4, 1)
center(0, 30, "Game Over")
glPopMatrix()

if self.score == 1: center(0, 0, "You drank %d bottle." % self.score)
else: center(0, 0, "You drank %d bottles." % self.score)
if self.score >= 20: center(0, -40, "Not too bad.")
center(0, -100, "Press SPACE.")

if engine.keystate(K_SPACE, True):
self.select_sound.play()
self.start_values()
self.state = 0


def splash_screen(self):

glColor(0.5, math.sin(self.ticks * 0.1) + 0.5 , 0.5)

glPushMatrix()
glLoadIdentity()
glScale(4, 4, 1)
center(math.sin(self.ticks * 0.03) * 10, 30, "Beer Snake")
glPopMatrix()

glColor(1, 1, 1)
center(0, 0, "Collect as many bottles as you can!")
center(0, -40, "Get hammered but don't hurt your head.")
center(0, -100, "Press SPACE to start.")
center(0, -140, "Press ESC to exit.")

if engine.keystate(K_SPACE, True):
self.select_sound.play()
self.start_values()
self.state = 1


def in_game_screen(self):

# drunkenness
self.worm_dir += math.sin(self.ticks * 0.2 ) * 0.004 * self.score

self.worm_dir += (engine.keystate(K_RIGHT) - engine.keystate(K_LEFT)) * 0.1
dx = math.sin(self.worm_dir) * self.speed
dy = math.cos(self.worm_dir) * self.speed
self.worm_x += dx
self.worm_y += dy
self.segments = [(self.worm_x, self.worm_y)] + self.segments[:self.length]

# collision
collision = False
if abs(self.worm_x) > 384 or abs(self.worm_y) > 284:
collision = True

for x, y in self.segments[25:]:
dx = x - self.worm_x
dy = y - self.worm_y
if dx * dx + dy * dy < 780:
collision = True
break

if collision:
print "Ouch!"
self.bam_sound.play()
self.state = 2

dx = self.bottle_x - self.worm_x
dy = self.bottle_y - self.worm_y
if dx * dx + dy * dy < 1200:
self.collect_sound.play()
self.score += 1
self.speed += 0.05
self.length += 20
self.set_bottle()


glColor(1, 1, 1)
self.bottle_sprite.draw(self.bottle_x, self.bottle_y, math.sin(self.ticks * 0.1) * 10, 1)

glColor(0, 0.7, 0)
for x, y in self.segments[4:]: self.segment_sprite.draw(x, y)

self.head_sprite.draw(self.worm_x, self.worm_y, self.worm_dir * -180 / math.pi, 0.6, (self.ticks/15) % 2)

glColor(1, 1, 1, 0.5)
font.put(-380, -280, "bottles: %2d" % self.score)

def start(self):
while engine.running:

engine.handle_events()
self.ticks += 1
glClear(GL_COLOR_BUFFER_BIT)

# splash screen
if self.state == 0: self.splash_screen()
elif self.state == 1: self.in_game_screen()
elif self.state == 2: self.end_screen()

pygame.display.flip()
pygame.time.wait(15)



if __name__ == "__main__":
global main
main = Main()
main.start()
print "Remember: don't drink and drive. :)"


Something went wrong with that request. Please try again.