-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_choice.py
executable file
·132 lines (111 loc) · 4.4 KB
/
game_choice.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
#!/usr/bin/env python3
"""
This is a game of "snake" for John's tetris table.
Copyright John Cooper - 2016
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from random import randrange
from operator import add
from copy import copy
import logging
import pygame
from pygame.locals import *
from pygame.color import *
log = logging.getLogger(__name__)
levelup_length = 20
BUTTONEVENT = USEREVENT+1
MOVE_L, MOVE_R, MOVE_U, MOVE_D = [[-1,0], [1,0], [0,1], [0,-1]]
COL_WALL, COL_SNAKE, COL_ERASE, COL_TARGET = [5, 4, 0, 2]
class Games():
"""The snake game"""
def __init__(self, board, light_board, table_buttons, games):
self.board = board
self.board_real_height = board['height'] - board['height_hidden']
self.running = False
self.clock = pygame.time.Clock()
self.button_state = [True] * 8
self.move_interval = 500 # 0.5 second
self.ended = False
self.light_board = light_board
self.buttons = table_buttons
self.games = games
def show_logo(self, logo):
this_logo = copy(logo)
this_logo.reverse()
self.board['pixels'] = this_logo
self.light_board.show_board(self.board)
def next_game(self, selected_game, direction):
if selected_game + direction < 0:
selected_game = len(self.games) - 1
elif selected_game + direction > len(self.games) - 1:
selected_game = 0
else:
selected_game += direction
return selected_game
def buttons_pressed(self, new_values):
pressed = []
# log.debug("Buttons pressed %s", new_values)
for x, val in enumerate(new_values):
if not val and self.button_state[x]:
log.debug("Button state: %s", self.button_state)
pressed.append(x)
self.button_state[x] = val
return pressed
def button_event(self, button):
if button == 1:
return pygame.K_LEFT
elif button == 3:
return pygame.K_RIGHT
elif button == 2:
return pygame.K_UP
elif button == 0:
return pygame.K_DOWN
def run(self):
"""Select the game"""
log.debug("Run the game")
pygame.display.init()
pygame.init()
selected_game = 0
controls = self.buttons
self.light_board.show_board(self.board)
self.running = True
self.show_logo(self.games[selected_game].LOGO)
pygame.time.set_timer(USEREVENT, 100) # every 100 miliseconds
pygame.time.set_timer(USEREVENT+1, 10) # every 10 milliseconds
while self.running and not self.ended:
key = False
event = pygame.event.wait()
if event.type == QUIT:
self.running = False
if event.type == USEREVENT+1:
for button in self.buttons_pressed(controls.get_buttons()):
log.debug("Button pressed %s", button)
key = self.button_event(button)
if event.type == KEYDOWN:
key = event.key
if event.type == KEYDOWN or event.type == USEREVENT+1:
if key == pygame.K_LEFT:
selected_game = self.next_game(selected_game, -1)
if key == pygame.K_RIGHT:
selected_game = self.next_game(selected_game, 1)
if key == pygame.K_UP:
self.running = False
self.ended = True
if key == pygame.K_DOWN:
self.ended = True
self.running = False
if key == pygame.K_q:
self.ended = True
self.running = False
self.show_logo(self.games[selected_game].LOGO)
if self.ended:
return self.games[selected_game]