Skip to content

Commit 67e73e7

Browse files
committed
added UserInterface menu, firtst working version
1 parent c02da52 commit 67e73e7

2 files changed

Lines changed: 124 additions & 11 deletions

File tree

snake.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
#!/usr/bin/python
1+
#!/usr/bin/env python
22

3-
import os, sys, random
3+
4+
import os
5+
import sys
6+
import random
47
import pygame
8+
from ui import ui
59
from pygame.sprite import Sprite
610

711
#sys.stdout = os.devnull
@@ -50,6 +54,8 @@ class game(object):
5054
def __init__(self):
5155
self.screen = None
5256
self.playerBox = None
57+
self.ui = None
58+
5359

5460
def move(self, direction):
5561
#print 'direction:', direction
@@ -138,14 +144,9 @@ def headDied(self, elements):
138144
f.write(highscores)
139145
f.close()
140146

141-
lines = ['GAME OVER', 'POINTS: ' + str(scores), "HIGHSCORE "+highscores, "Button / Keyboard: 1 = Speed Up", "Button / Keyboard: 2 = Speed Down", "R = restart / respawn", 'Q = Quit / Exit']
142-
fontSize = 30
143-
fnt = pygame.font.SysFont("MS Comic Sans", fontSize)
144-
xPos = (self.SCREEN_WIDTH / 2)
145-
#print fnt.size(resultText)
146-
for i in range(len(lines)):
147-
txt = lines[i]
148-
self.screen.blit(fnt.render(txt, 1, (0,0,255)), ( xPos - (fnt.size(txt)[0] / 2), 250 +(i * fontSize)))
147+
148+
self.iUi.addSimpleMenu("gameOver", ["GAME OVER", "POINTS: " + str(scores), "HIGHSCORE "+highscores, "Button / Keyboard: 1 = Speed Up", "Button / Keyboard: 2 = Speed Down", "R = restart / respawn", "Q = Quit / Exit"])
149+
self.iUi.draw("gameOver")
149150

150151
return dead
151152

@@ -187,10 +188,14 @@ def run_game(self):
187188
pygame.mouse.set_visible(False)
188189

189190
self.screen = pygame.display.set_mode( (self.SCREEN_WIDTH, self.SCREEN_HEIGHT), 0, 32)
190-
pygame.display.toggle_fullscreen()
191+
#TODO: make it optional
192+
#pygame.display.toggle_fullscreen()
191193
clock = pygame.time.Clock()
192194
redrawCount = 0
193195

196+
# init the menu, add stuff later
197+
self.iUi = ui(self.screen)
198+
194199
pygame.joystick.init()
195200
self.myJoystick = None
196201
self.joystick_names = []

ui.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#!/usr/bin/env python
2+
3+
# notes:
4+
# The Ten Commandments Of Video Game Menus http://kotaku.com/5955855/the-ten-commandments-of-video-game-menus
5+
6+
import pygame
7+
import sys
8+
9+
class ui(object):
10+
def __init__(self, screen):
11+
12+
# the main screen, result from:
13+
# pygame.display.set_mode(...)
14+
self._screen = screen
15+
self.keymap = {pygame.K_UP:1, pygame.K_RIGHT:2, pygame.K_DOWN:3, pygame.K_LEFT:4}
16+
17+
self.nextAction = None
18+
19+
self.menus = {}
20+
21+
def interaction(self, eventKey):
22+
canInteract = False
23+
24+
if eventKey in self.keymap:
25+
canInteract = True
26+
nextAction = self.keymap[eventKey]
27+
else:
28+
# reset the action if multiple times pressed keys before
29+
# actually did something
30+
self.nextAction = None
31+
32+
return canInteract
33+
34+
def addMenu(self, menuKey, menuRows):
35+
''' add an menu
36+
menuKey: unique name of the menu
37+
menueRows: [
38+
{"rowName":"title", "selectable":False, "font":"MS Comic Sans", "fontSize":30, "color":(0,0,255), "text":"UserInterfaceGenerator"},
39+
{"rowName":"start", "selectable":True, "font":"MS Comic Sans", "fontSize":30, "color":(0,0,255), "text":"Start Game"},
40+
{"rowName":"q", "selectable":True, "font":"MS Comic Sans", "fontSize":30, "color":(123,55,255), "text":"QUIT"}
41+
]
42+
'''
43+
self.menus[menuKey] = menuRows
44+
45+
def addSimpleMenu(self, menuKey, menuRows, font="MS Comic Sans", size=30, color=(245, 101, 44)):
46+
''' add an SIMPLE menu
47+
menuKey: unique name of the menu
48+
menueRows: ["You Shall", "NOT PASS"]
49+
'''
50+
menu = []
51+
rowCount = 0
52+
for row in menuRows:
53+
menu.append({"rowName":"simple_" + str(rowCount), "selectable":False, "font":font, "fontSize":size, "color":color, "text":row})
54+
55+
self.menus[menuKey] = menu
56+
57+
def draw(self, menuKey):
58+
if menuKey in self.menus:
59+
menuToDraw = self.menus[menuKey]
60+
61+
#print fnt.size(resultText)
62+
menuRowsCount = len(menuToDraw)
63+
for i in range(menuRowsCount):
64+
fontSize = menuToDraw[i]["fontSize"]
65+
fnt = pygame.font.SysFont(menuToDraw[i]["font"] , fontSize)
66+
xPos = (self._screen.get_width() / 2)
67+
yPos = (self._screen.get_height() / 30) * menuRowsCount
68+
txt = menuToDraw[i]["text"]
69+
self._screen.blit(fnt.render(txt, 1, menuToDraw[i]["color"]), ( xPos - (fnt.size(txt)[0] / 2), yPos +(i * fontSize)))
70+
else:
71+
raise Exception("Error menu '"+ menuKey +"' does not exist")
72+
73+
if __name__ == "__main__":
74+
pygame.init()
75+
screen = pygame.display.set_mode( (1024, 768), 0, 32)
76+
clock = pygame.time.Clock()
77+
78+
BG_COLOR = (8, 13, 41)
79+
80+
iUi = ui(screen)
81+
iUi.addMenu("main", [
82+
{"rowName":"title", "selectable":False, "font":"MS Comic Sans", "fontSize":30, "color":(0,0,255), "text":"UserInterfaceGenerator"},
83+
{"rowName":"start", "selectable":True, "font":"MS Comic Sans", "fontSize":30, "color":(0,0,255), "text":"Start Game"},
84+
{"rowName":"q", "selectable":True, "font":"MS Comic Sans", "fontSize":30, "color":(123,55,255), "text":"QUIT"}
85+
]
86+
)
87+
88+
while True:
89+
# Limit frame speed to 50 FPS
90+
time_passed = clock.tick(50)
91+
screen.fill(BG_COLOR)
92+
93+
for event in pygame.event.get():
94+
if event.type == pygame.QUIT:
95+
sys.exit()
96+
97+
elif event.type == pygame.KEYDOWN:
98+
if iUi.interaction(event.key):
99+
print "UI catched this key"
100+
else:
101+
print "no UI catch, key pressed:", event.key
102+
103+
if event.key == pygame.K_q:
104+
sys.exit(0)
105+
106+
iUi.draw("main")
107+
108+
pygame.display.flip()

0 commit comments

Comments
 (0)