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
|
#!/usr/bin/python
import math
import pygame
from gi.repository import Gtk
BALL_COLOR = 255, 255, 255
GROUND_COLOR = 0, 0, 0
BACKGROUND_COLOR = 180, 180, 180
BALL_SIZE = 20
class Ball(object):
def __init__(self, initial_x, initial_y):
self._x = initial_x
self._y = initial_y
# FIXME, this should be modified by the accelerometer
self._vx = 5
self._vy = 2
def get_position(self):
return self._x, self._y
def set_position(self, new_x, new_y):
self._x = new_x
self._y = new_y
def update(self):
self._x += self._vx
self._y += self._vy
def draw(self, surface):
pygame.draw.circle(surface, BALL_COLOR, (self._x, self._y), BALL_SIZE)
class Level(object):
def __init__(self, hardness, parent_width, parent_height):
# FIXME, use the hardness value to make random levels of
# different difficulty
self._hardness = hardness
self._parent_width = parent_width
self._parent_height = parent_height
self._rects = []
self._ball_start = None
self._ball_end = None
self.calculate_level()
def calculate_level(self):
rect = pygame.Rect((self._parent_width / 4, self._parent_height / 4),
(self._parent_width / 2, self._parent_height / 2))
self._rects.append(rect)
self._ball_start = self._parent_width / 2, self._parent_height / 2
self._ball_end = self._parent_width / 3, self._parent_height / 3
def get_ball_start(self):
return self._ball_start
def is_on_hole(self, ball_position):
"""Test if the ball is on the hole."""
distance_x = ball_position[0] - self._ball_end[0]
distance_y = ball_position[1] - self._ball_end[1]
distance = math.hypot(distance_x, distance_y)
return distance < BALL_SIZE / 2
def is_on_ground(self, ball_position):
"""Test if the ball is on the ground."""
for rect in self._rects:
if rect.collidepoint(ball_position):
return True
return False
def draw(self, surface):
# draw ground:
for rect in self._rects:
pygame.draw.rect(surface, GROUND_COLOR, rect)
# draw hole:
pygame.draw.circle(surface, BALL_COLOR, self._ball_end, BALL_SIZE, 2)
class TiltGame(object):
def __init__(self):
# Set up a clock for managing the frame rate.
self.clock = pygame.time.Clock()
self._level = None
self._ball = None
self._running = False
self._paused = False
self._screen = None
def setup(self):
self._level = Level(0.5, *self._screen.get_size())
ball_x, ball_y = self._level.get_ball_start()
self._ball = Ball(ball_x, ball_y)
def set_paused(self, paused):
self._paused = paused
# Called to save the state of the game to the Journal.
def write_file(self, file_path):
pass
# Called to load the state of the game from the Journal.
def read_file(self, file_path):
pass
# The main game loop.
def run(self):
self._screen = pygame.display.get_surface()
self.setup()
self._running = True
while self._running:
# Pump GTK messages.
while Gtk.events_pending():
Gtk.main_iteration()
# Pump PyGame messages.
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
elif event.type == pygame.VIDEORESIZE:
pygame.display.set_mode(event.size, pygame.RESIZABLE)
# Move the ball
if not self._paused:
if self._level.is_on_hole(self._ball.get_position()):
# Success! Restart
self._ball.set_position(*self._level.get_ball_start())
if self._level.is_on_ground(self._ball.get_position()):
self._ball.update()
else:
# Fail! Restart
self._ball.set_position(*self._level.get_ball_start())
# Clear Display
self._screen.fill(BACKGROUND_COLOR)
# Draw the level
self._level.draw(self._screen)
# Draw the ball
self._ball.draw(self._screen)
# Flip Display
pygame.display.flip()
# Try to stay at 30 FPS
self.clock.tick(30)
# This function is called when the game is run directly from the command line:
# ./game.py
def main():
pygame.init()
pygame.display.set_mode((0, 0), pygame.RESIZABLE)
game = TiltGame()
game.run()
if __name__ == '__main__':
main()
|