-
Notifications
You must be signed in to change notification settings - Fork 2
/
menu.py
170 lines (144 loc) · 6.42 KB
/
menu.py
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
'''
@author: avalanchy (at) google mail dot com
@version: 0.1; python 2.7; pygame 1.9.2pre; SDL 1.2.14; MS Windows XP SP3
@date: 2012-04-08
@license: This document is under GNU GPL v3
README on the bottom of document.
@font: from http://www.dafont.com/coders-crux.font
more abuot license you can find in data/coders-crux/license.txt
'''
import pygame
from pygame.locals import *
if not pygame.display.get_init():
pygame.display.init()
if not pygame.font.get_init():
pygame.font.init()
background = pygame.image.load("images/titlescreen.png")
backgroundRect = background.get_rect()
class Menu:
lista = []
pola = []
rozmiar_fontu = 32
font_path = 'datafiles/coders_crux.ttf'
font = pygame.font.Font
dest_surface = pygame.Surface
ilosc_pol = 0
kolor_tla = (51,51,51)
kolor_tekstu = (255, 255, 153)
kolor_zaznaczenia = (153,102,255)
pozycja_zaznaczenia = 0
pozycja_wklejenia = (0,0)
menu_width = 0
menu_height = 0
class Pole:
tekst = ''
pole = pygame.Surface
pole_rect = pygame.Rect
zaznaczenie_rect = pygame.Rect
def move_menu(self, top, left):
self.pozycja_wklejenia = (top,left)
def set_colors(self, text, selection, background):
self.kolor_tla = background
self.kolor_tekstu = text
self.kolor_zaznaczenia = selection
def set_fontsize(self,font_size):
self.rozmiar_fontu = font_size
def set_font(self, path):
self.font_path = path
def get_position(self):
return self.pozycja_zaznaczenia
def init(self, lista, dest_surface):
self.lista = lista
self.dest_surface = dest_surface
self.ilosc_pol = len(self.lista)
self.stworz_strukture()
def draw(self,przesun=0):
if przesun:
self.pozycja_zaznaczenia += przesun
if self.pozycja_zaznaczenia == -1:
self.pozycja_zaznaczenia = self.ilosc_pol - 1
self.pozycja_zaznaczenia %= self.ilosc_pol
menu = pygame.Surface((self.menu_width, self.menu_height))
menu.fill(self.kolor_tla)
zaznaczenie_rect = self.pola[self.pozycja_zaznaczenia].zaznaczenie_rect
pygame.draw.rect(menu,self.kolor_zaznaczenia,zaznaczenie_rect)
for i in xrange(self.ilosc_pol):
menu.blit(self.pola[i].pole,self.pola[i].pole_rect)
self.dest_surface.blit(menu,self.pozycja_wklejenia)
return self.pozycja_zaznaczenia
def stworz_strukture(self):
przesuniecie = 0
self.menu_height = 0
self.font = pygame.font.Font(self.font_path, self.rozmiar_fontu)
for i in xrange(self.ilosc_pol):
self.pola.append(self.Pole())
self.pola[i].tekst = self.lista[i]
self.pola[i].pole = self.font.render(self.pola[i].tekst, 1, self.kolor_tekstu)
self.pola[i].pole_rect = self.pola[i].pole.get_rect()
przesuniecie = int(self.rozmiar_fontu * 0.2)
height = self.pola[i].pole_rect.height
self.pola[i].pole_rect.left = przesuniecie
self.pola[i].pole_rect.top = przesuniecie+(przesuniecie*2+height)*i
width = self.pola[i].pole_rect.width+przesuniecie*2
height = self.pola[i].pole_rect.height+przesuniecie*2
left = self.pola[i].pole_rect.left-przesuniecie
top = self.pola[i].pole_rect.top-przesuniecie
self.pola[i].zaznaczenie_rect = (left,top ,width, height)
if width > self.menu_width:
self.menu_width = width
self.menu_height += height
x = self.dest_surface.get_rect().centerx - self.menu_width / 2
y = self.dest_surface.get_rect().centery - self.menu_height / 2
mx, my = self.pozycja_wklejenia
self.pozycja_wklejenia = (x+mx, y+my)
if __name__ == "__main__":
import sys
surface = pygame.display.set_mode((1600,900)) #0,6671875 and 0,(6) of HD resoultion
surface.blit(background, backgroundRect)
f = pygame.font.Font(None, 128)
surf = f.render("SUPER ROTISSERIE DX.", 1, (255,255,255), (0,0,255))
surface.blit(surf, backgroundRect)
'''First you have to make an object of a *Menu class.
*init take 2 arguments. List of fields and destination surface.
Then you have a 4 configuration options:
*set_colors will set colors of menu (text, selection, background)
*set_fontsize will set size of font.
*set_font take a path to font you choose.
*move_menu is quite interseting. It is only option which you can use before
and after *init statement. When you use it before you will move menu from
center of your surface. When you use it after it will set constant coordinates.
Uncomment every one and check what is result!
*draw will blit menu on the surface. Be carefull better set only -1 and 1
arguments to move selection or nothing. This function will return actual
position of selection.
*get_postion will return actual position of seletion. '''
menu = Menu()#necessary
menu.set_colors((255,255,255), (0,0,255), (0,0,0))#optional
menu.set_fontsize(64)#optional
#menu.set_font('data/couree.fon')#optional
menu.move_menu(400, 200)#optional
menu.init(['Start','Options','Quit'], surface)#necessary
#menu.move_menu(0, 0)#optional
menu.draw()#necessary
pygame.key.set_repeat(199,69)#(delay,interval)
pygame.display.update()
#surface.blit(background, backgroundRect)
while 1:
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_W:
menu.draw(-1) #here is the Menu class function
if event.key == K_DOWN:
menu.draw(1) #here is the Menu class function
if event.key == K_RETURN:
if menu.get_position() == 2:#here is the Menu class function
pygame.display.quit()
sys.exit()
if event.key == K_ESCAPE:
pygame.display.quit()
sys.exit()
pygame.display.update()
elif event.type == QUIT:
pygame.display.quit()
sys.exit()
pygame.time.wait(8)