diff options
author | Manuel Kaufmann <humitos@gmail.com> | 2012-05-31 01:46:52 (GMT) |
---|---|---|
committer | Manuel Kaufmann <humitos@gmail.com> | 2012-05-31 01:46:52 (GMT) |
commit | 2facac89d780f4d9e7b7494c205be45cd6f42911 (patch) | |
tree | 04f2a9f47673317770c415596acd5058f44122e6 | |
parent | db9cd77b749008b45885ce6c4c4863841e08dfc1 (diff) |
First usable version
The user is able to move the ball by moving the XO to get the hole. When the
hole is reached by the user, a new hole appears in the screen.
-rwxr-xr-x | game.py | 52 |
1 files changed, 33 insertions, 19 deletions
@@ -1,6 +1,7 @@ #!/usr/bin/python import math import pygame +import random from gi.repository import Gtk BALL_COLOR = 255, 255, 255 @@ -14,10 +15,10 @@ class AccelerometerDevice(object): ACCELEROMETER_DEVICE = '/sys/devices/platform/lis3lv02d/position' SPEED = (0.0, 0.0) FRICTION = 0.993 - ACCELERATION = 0.05 - def __init__(self): + def __init__(self, acceleration=0.05): self._device = open(self.ACCELEROMETER_DEVICE, 'r') + self.acceleration = acceleration def reseek(self): self._device.seek(0) @@ -28,6 +29,7 @@ class AccelerometerDevice(object): return x, y, z def parse(self, data): + # discart parentheses from the data data = data[1:-2] x, y, z = map(int, data.split(',')) return x, y, z @@ -41,10 +43,6 @@ class Ball(object): 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 @@ -52,9 +50,9 @@ class Ball(object): self._x = new_x self._y = new_y - def update(self): - self._x += self._vx - self._y += self._vy + def update(self, vx, vy): + self._x += vx + self._y += vy def draw(self, surface): pygame.draw.circle(surface, BALL_COLOR, (self._x, self._y), BALL_SIZE) @@ -74,9 +72,12 @@ class Level(object): self._ball_end = None self.calculate_level() + def set_hole(self, x, y): + self._ball_end = (x, y) + def calculate_level(self): - rect = pygame.Rect((self._parent_width / 4, self._parent_height / 4), - (self._parent_width / 2, self._parent_height / 2)) + rect = pygame.Rect((0, 0), + (self._parent_width, self._parent_height)) 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 @@ -119,6 +120,9 @@ class TiltGame(object): self._paused = False self._screen = None + # TODO: add a button to switch this + self.show_axis = True + self.accelerometer = AccelerometerDevice() def setup(self): @@ -144,6 +148,8 @@ class TiltGame(object): self.setup() self._running = True + font = pygame.font.Font(pygame.font.get_default_font(), 22) + while self._running: # Pump GTK messages. while Gtk.events_pending(): @@ -158,12 +164,24 @@ class TiltGame(object): # Move the ball if not self._paused: + # Get the position of the accelerometer + x, y, z = self.accelerometer.read() + if self.show_axis: + surface_x = font.render('X: %s' % x, True, (255, 0, 0)) + surface_y = font.render('Y: %s' % y, True, (0, 255, 0)) + surface_z = font.render('Z: %s' % z, True, (0, 0, 255)) + if self._level.is_on_hole(self._ball.get_position()): # Success! Restart self._ball.set_position(*self._level.get_ball_start()) + x, y = self._screen.get_size() + self._level.set_hole(random.randint(0 ,x), + random.randint(0, y)) if self._level.is_on_ground(self._ball.get_position()): - self._ball.update() + vx = int(x * self.accelerometer.acceleration) + vy = int(y * self.accelerometer.acceleration) + self._ball.update(vx, vy) else: # Fail! Restart self._ball.set_position(*self._level.get_ball_start()) @@ -177,13 +195,6 @@ class TiltGame(object): # Draw the ball self._ball.draw(self._screen) - # Get the position of the accelerometer - x, y, z = self.accelerometer.read() - font = pygame.font.Font(pygame.font.get_default_font(), 18) - surface_x = font.render('X: %s' % x, True, (0, 0, 0)) - surface_y = font.render('Y: %s' % y, True, (0, 0, 0)) - surface_z = font.render('Z: %s' % z, True, (0, 0, 0)) - # Draw the accelerometer data self._screen.blit(surface_x, (15, 15)) self._screen.blit(surface_y, (15, surface_x.get_height() + 15)) @@ -196,6 +207,9 @@ class TiltGame(object): # Try to stay at 30 FPS self.clock.tick(30) + # Close the device before leave + self.accelerometer.close() + # This function is called when the game is run directly from the command line: # ./game.py |