Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRodolfo D. Arce S <rodolfo.arce.s@gmail.com>2011-05-13 01:36:52 (GMT)
committer Rodolfo D. Arce S <rodolfo.arce.s@gmail.com>2011-05-13 01:36:52 (GMT)
commitce75b367b415edf0d524aea10c4281c5aaa54ae6 (patch)
tree692b3a4542a30b7cdc7d81eb59447f109950a7a5
parent843daab512ec3e9f07077e6feaa91711afb5f64f (diff)
Added separate file for robot class, with various actions
-rw-r--r--main.py56
-rw-r--r--robot.py38
2 files changed, 55 insertions, 39 deletions
diff --git a/main.py b/main.py
index 66db0eb..aebdc53 100644
--- a/main.py
+++ b/main.py
@@ -8,34 +8,33 @@ import random
import pygame
from pygame.locals import *
+from robot import Robot
+
pygame.init()
# Screen size
window_h = 600
window_w = 800
+unit = 1.0
+speed = 9
+
screen = pygame.display.set_mode((window_w, window_h))
# Background image
object_background = pygame.image.load('images/park_2.png')
screen.blit(object_background, (0, 0))
+# Robot
+robot = Robot( window_w, window_h, unit, speed)
+
# Normal speed
-speed = 9
+
# Garbage image
object_garbage = pygame.image.load('images/paper.png')
position_garbage = [ 400, 300 ]
screen.blit(object_garbage, position_garbage)
-# Robot image
-object_robot = pygame.image.load('images/robot_1.png')
-position_robot = [0, window_h - object_robot.get_height()]
-x_direction = 0
-y_direction = 0
-screen.blit(object_robot, position_robot)
-
-
-
# Initialize images
pygame.display.update()
@@ -56,33 +55,10 @@ while not exit:
(event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
exit = True
break
-
- # Handle movement for the character
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_LEFT:
- x_direction = -1
- y_direction = 0
- if event.key == pygame.K_RIGHT:
- x_direction = 1
- y_direction =0
- if event.key == pygame.K_UP:
- y_direction = -1
- x_direction = 0
- if event.key == pygame.K_DOWN:
- y_direction = 1
- x_direction = 0
- # Uncomment this if you want the robot to stop when you release the keys
-# if event.type == pygame.KEYUP:
-# if (event.key == pygame.K_LEFT and x_direction == -1 and y_direction == 0) or\
-# (event.key == pygame.K_RIGHT and x_direction == 1 and y_direction == 0) or\
-# (event.key == pygame.K_UP and y_direction == -1 and x_direction == 0) or\
-# (event.key == pygame.K_DOWN and y_direction == 1 and x_direction == 0):
-# x_direction = 0
-# y_direction = 0
-
+ robot.input(event)
if exit:
break
-
+
current_time = time.time()
if current_time - start_time <= frame_time:
continue
@@ -100,16 +76,18 @@ while not exit:
screen.blit(text, (0, 0))
# Robot
- screen.blit(object_robot, (position_robot[0], position_robot[1]))
- # Move the character around the screen
- position_robot = [((position_robot[0] + x_direction * speed )%window_w), \
- ((position_robot[1] + y_direction * speed )%window_h) ]
+ #screen.blit(object_robot, (position_robot[0], position_robot[1]))
+ robot.draw(screen)
# Garbage
screen.blit(object_garbage, (position_garbage[0], position_garbage[1]))
position_garbage = [((position_garbage[0] + random.randint(0,10) )%window_w), \
((position_garbage[1] + random.randint(0,10) )%window_h) ]
# position_garbage = [(position_garbage[0] + 3 ), (position_garbage[1] + 3 ) ]
+ robot.update()
+ # Add score if you pick garbage
+ #if position_garbage == position_robot:
+ # score = score + 1
pygame.display.update()
pygame.quit()
diff --git a/robot.py b/robot.py
new file mode 100644
index 0000000..15938b1
--- /dev/null
+++ b/robot.py
@@ -0,0 +1,38 @@
+import pygame
+
+class Robot:
+ def __init__(self, screen_w, screen_h, unit, speed):
+ self.unit = unit
+ self.speed = speed
+ self.image = pygame.image.load("images/robot_1.png")
+ image_rect = self.image.get_rect()
+ self.position = [0, 0]
+ self.screen_w = screen_w
+ self.screen_h = screen_h
+ self.direction_x = 0
+ self.direction_y = 0
+
+ def input(self, event):
+ # Handle movement for the character
+ if event.type == pygame.KEYDOWN:
+ if event.key == pygame.K_LEFT:
+ self.direction_x = -1
+ self.direction_y = 0
+ if event.key == pygame.K_RIGHT:
+ self.direction_x = 1
+ self.direction_y = 0
+ if event.key == pygame.K_UP:
+ self.direction_y = -1
+ self.direction_x = 0
+ if event.key == pygame.K_DOWN:
+ self.direction_y = 1
+ self.direction_x = 0
+
+ def update(self):
+ image_center = self.image.get_width()/2
+ # Move the character around the screen
+ self.position = ((self.position[0] + self.direction_x * self.speed * self.unit + image_center)%self.screen_w - image_center), \
+ ((self.position[1] + self.direction_y * self.speed * self.unit + image_center)%self.screen_h - image_center)
+
+ def draw(self, on_surface):
+ on_surface.blit(self.image, self.position)