Skip to content
Browse files

Moved the hud rendering code from the main python file into the game.py.

  • Loading branch information...
1 parent 7499d9a commit bb69fe9643249238be9560e5a21215019ab3e7e3 @donno committed Jan 29, 2012
Showing with 43 additions and 27 deletions.
  1. +1 −25 SnakeGame.py
  2. +42 −2 game.py
View
26 SnakeGame.py
@@ -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:
@@ -50,7 +27,6 @@ def main():
game.onUpdate()
game.onRender(screen)
- renderhud(screen)
pygame.display.flip()
clock.tick(100)
View
44 game.py
@@ -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]))
@@ -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.
Something went wrong with that request. Please try again.