Skip to content

Commit

Permalink
Moved the hud rendering code from the main python file into the game.py.
Browse files Browse the repository at this point in the history
  • Loading branch information
donno committed Jan 29, 2012
1 parent 7499d9a commit bb69fe9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 27 deletions.
26 changes: 1 addition & 25 deletions SnakeGame.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,15 @@

SCREEN_SIZE = 1024, 600

def renderHud():
fontName = pygame.font.get_default_font()
font = pygame.font.Font(fontName, 14)
colour = (0, 0, 255)

nameSurface = font.render(__NAME__, False, colour)
nameRect = nameSurface.get_rect()
authorSurface = font.render(', '.join(__AUTHORS__), False, colour)
authorsY = nameRect.y + nameRect.height + 5
authorsRect = authorSurface.get_rect()
authorsRect.y = authorsY
del authorsY
offset = 10

def _renderHud(screen):
nameRect.x = screen.get_rect().width - nameRect.width - offset
authorsRect.x = screen.get_rect().width - authorsRect.width - offset
screen.blit(nameSurface, nameRect)
screen.blit(authorSurface, authorsRect)

return _renderHud

def main():
pygame.init()
pygame.font.init()

game = Game()
game = Game(__NAME__)
size = SCREEN_SIZE
screen = pygame.display.set_mode(size)
pygame.display.set_caption(__NAME__)
isRunning = True
renderhud = renderHud()
clock = pygame.time.Clock()
game.onResize(size)
while isRunning:
Expand All @@ -50,7 +27,6 @@ def main():

game.onUpdate()
game.onRender(screen)
renderhud(screen)
pygame.display.flip()
clock.tick(100)

Expand Down
44 changes: 42 additions & 2 deletions game.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,54 @@

__AUTHORS__ = ['Donno', 'Hattiel', 'Shrubber']

import pygame
import engine.snake
import engine.mouse
import random

def renderHud(title):
fontName = pygame.font.get_default_font()
font = pygame.font.Font(fontName, 14)
colour = (0, 0, 255)

nameSurface = font.render(title, False, colour)
nameRect = nameSurface.get_rect()
authorSurface = font.render(', '.join(__AUTHORS__), False, colour)
authorsY = nameRect.y + nameRect.height + 5
authorsRect = authorSurface.get_rect()
authorsRect.y = authorsY
del authorsY
offset = 10

hudRect = (0, 0, 400, 38)
#hudBackground = pygame.Surface((400, 38))
#hudBackground.fill(

def _renderHud(screen):
nameRect.x = screen.get_rect().width - nameRect.width - offset
authorsRect.x = screen.get_rect().width - authorsRect.width - offset
screen.fill( ( 38, 38, 38, 28), hudRect, pygame.BLEND_RGBA_ADD)
screen.blit(nameSurface, nameRect)
screen.blit(authorSurface, authorsRect)

return _renderHud

class Game:
def __init__(self):
def __init__(self, title):
self.title = title
self.player = engine.snake.Snake((500, 300))
self.leftPressed = False
self.rightPressed = False
self.screenSize = None
self.mice = []


self.renderhud = renderHud(title)

def spawnMouse(self):
# TODO: Write some awesome here to figure out a good place to
# place the mouse.

position = (400, 300) if not self.screenSize else (
random.randint(0, self.screenSize[0]),
random.randint(0, self.screenSize[1]))
Expand Down Expand Up @@ -59,8 +92,15 @@ def onUpdate(self):
mouse.update()

def onRender(self, screen):
# Clear the whole screen
screen.fill((0,0,0))

# Draw the player (snake)
self.player.draw(screen)

# Draw the mobs (mice)
for mouse in self.mice:
mouse.draw(screen)

# Render the heads up display
self.renderhud(screen)

0 comments on commit bb69fe9

Please sign in to comment.