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-14 20:55:00 (GMT)
committer Rodolfo D. Arce S <rodolfo.arce.s@gmail.com>2011-05-14 20:55:00 (GMT)
commit1f60e9b2e7245f9fc7bc59d39bc6237a2e916aa1 (patch)
tree77b36318e8cb2ee87858ff626530e00a0e1cfeef
parentc6b7d7dbd1129c1597fe4b30ec7f1f90738cf781 (diff)
Separated files for each component, Added behavior to kill people when colliding with them
-rw-r--r--README6
-rw-r--r--garbage.py25
-rw-r--r--images/garbage_01.pngbin0 -> 1374 bytes
-rw-r--r--images/garbage_02.pngbin0 -> 2000 bytes
-rw-r--r--images/garbage_03.pngbin0 -> 1031 bytes
-rw-r--r--images/paper.pngbin1849 -> 0 bytes
-rw-r--r--images/person_01.png (renamed from images/person_1.png)bin5391 -> 5391 bytes
-rw-r--r--images/person_02.png (renamed from images/person_2.png)bin9020 -> 9020 bytes
-rw-r--r--images/person_03.png (renamed from images/kid.png)bin2037 -> 2037 bytes
-rw-r--r--images/person_04.png (renamed from images/person_4.png)bin6272 -> 6272 bytes
-rw-r--r--main.py86
-rw-r--r--person.py36
-rw-r--r--robot.py17
13 files changed, 136 insertions, 34 deletions
diff --git a/README b/README
index 369c6ad..7165eb2 100644
--- a/README
+++ b/README
@@ -10,14 +10,16 @@ http://www.openclipart.org/detail/62479
Robot:
http://www.openclipart.org/detail/161
-Paper:
+Garbage:
http://www.openclipart.org/detail/16727
-http://www.openclipart.org/detail/102793
+http://www.openclipart.org/detail/26099
+http://www.openclipart.org/detail/86503
Person:
http://www.openclipart.org/detail/27903
http://www.openclipart.org/detail/48367
http://www.openclipart.org/detail/89587
+http://www.openclipart.org/detail/132841
Kid:
http://www.openclipart.org/detail/132841
diff --git a/garbage.py b/garbage.py
new file mode 100644
index 0000000..8cf5bb3
--- /dev/null
+++ b/garbage.py
@@ -0,0 +1,25 @@
+import pygame
+from random import randint
+
+fallers_size = 3
+fallers = ["images/garbage_%02d.png" % faller_number for faller_number in xrange(1, fallers_size + 1) ]
+
+class Garbage:
+ def __init__(self, screen_w, screen_h, unit, garbage_speed):
+ self.unit = unit
+ self.speed = garbage_speed
+ self.image = pygame.image.load(fallers[randint(0, fallers_size - 1)])
+ 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 update(self):
+ image_center = self.image.get_width()/2
+ self.position = [((self.position[0] + randint(0,10) )%self.screen_w), \
+ ((self.position[1] + randint(0,10) )%self.screen_h) ]
+
+ def draw(self, on_surface):
+ on_surface.blit(self.image, self.position)
diff --git a/images/garbage_01.png b/images/garbage_01.png
new file mode 100644
index 0000000..f7399dd
--- /dev/null
+++ b/images/garbage_01.png
Binary files differ
diff --git a/images/garbage_02.png b/images/garbage_02.png
new file mode 100644
index 0000000..02c75f9
--- /dev/null
+++ b/images/garbage_02.png
Binary files differ
diff --git a/images/garbage_03.png b/images/garbage_03.png
new file mode 100644
index 0000000..2d62880
--- /dev/null
+++ b/images/garbage_03.png
Binary files differ
diff --git a/images/paper.png b/images/paper.png
deleted file mode 100644
index 7c24e85..0000000
--- a/images/paper.png
+++ /dev/null
Binary files differ
diff --git a/images/person_1.png b/images/person_01.png
index 5aa47a9..5aa47a9 100644
--- a/images/person_1.png
+++ b/images/person_01.png
Binary files differ
diff --git a/images/person_2.png b/images/person_02.png
index 0b95973..0b95973 100644
--- a/images/person_2.png
+++ b/images/person_02.png
Binary files differ
diff --git a/images/kid.png b/images/person_03.png
index d3d9a2a..d3d9a2a 100644
--- a/images/kid.png
+++ b/images/person_03.png
Binary files differ
diff --git a/images/person_4.png b/images/person_04.png
index cc221a1..cc221a1 100644
--- a/images/person_4.png
+++ b/images/person_04.png
Binary files differ
diff --git a/main.py b/main.py
index e99fb3b..8639311 100644
--- a/main.py
+++ b/main.py
@@ -9,6 +9,9 @@ import pygame
from pygame.locals import *
from robot import Robot
+from garbage import Garbage
+from person import Person
+
pygame.init()
@@ -16,35 +19,37 @@ pygame.init()
window_h = 600
window_w = 800
unit = 1.0
-speed = 9
-screen = pygame.display.set_mode((window_w, window_h))
+robot_speed = 9
+garbage_speed = 3
+person_speed = 4
-# 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)
+# Person generation interval
+person_generation_ticks = 100
+last_person_tick = 0
-# Normal speed
+#garbages = []
+people = []
+screen = pygame.display.set_mode((window_w, window_h))
-# Garbage image
-object_garbage = pygame.image.load('images/paper.png')
-position_garbage = [ 400, 300 ]
-screen.blit(object_garbage, position_garbage)
+# Background image
+object_background = pygame.image.load('images/park_2.png')
-# Initialize images
-pygame.display.update()
-position_garbage = [0,0]
+# Robot
+robot = Robot(window_w, window_h, unit, robot_speed)
+# Person
+person = Person( window_w, window_h, unit, person_speed)
+
start_time = time.time()
fps = 30
frame_time = 1.0/fps
# Main loop
exit = False
+lost_game = False
while not exit:
@@ -63,32 +68,55 @@ while not exit:
if current_time - start_time <= frame_time:
continue
start_time = current_time
-
- # draw the background first, because it will cover all the screen
- screen.blit(object_background, (0,0))
- # draw everything else over the background
+ if lost_game == False:
+ # If enough time passed generate a new object
+ if last_person_tick == 0:
+ last_person_tick = person_generation_ticks
+ people.append(Person( window_w, window_h, unit, person_speed))
+ else:
+ last_person_tick -= 1
+
# Score text
score = 10
- score_text = "Score: " + str(score)
+ score_text = "Score: " + str(score) + " Next in : " + str(last_person_tick)
font = pygame.font.Font(None, 36)
text = font.render( score_text , 1, (0, 0, 0, 0))
- screen.blit(text, (0, 0))
-
- # Robot
- 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) ]
+ # check collisions
+ for person in people:
+ if robot.collides_with(person):
+ #lost_game = True
+ people.remove(person)
+
+ # Remove objects that "died"
+ for person in people:
+ if person.died():
+ people.remove(person)
+
+ # update objects
+ for person in people:
+ person.update()
+
robot.update()
+
+ # draw the background first, because it will cover all the screen
+ screen.blit(object_background, (0,0))
+ # draw everything else over the background
+ # Score
+ screen.blit(text, (0, 0))
+
+ # Persons
+ for person in people:
+ person.draw(screen)
+ #Robot
+ robot.draw(screen)
# Add score if you pick garbage
#if position_garbage == position_robot:
# score = score + 1
+
pygame.display.update()
pygame.quit()
diff --git a/person.py b/person.py
new file mode 100644
index 0000000..4243677
--- /dev/null
+++ b/person.py
@@ -0,0 +1,36 @@
+import pygame
+from random import randint
+
+
+person_number = 4
+persons = ["images/person_%02d.png" % person_number for person_number in xrange(1, person_number + 1) ]
+
+class Person:
+ def __init__(self, screen_w, screen_h, unit, speed):
+ self.unit = unit
+ self.speed = speed
+ self.image = pygame.image.load(persons[randint(0, person_number - 1)])
+ image_rect = self.image.get_rect()
+ size_reduction = 20*unit
+ self.collision_rect = pygame.Rect(image_rect.left + size_reduction, \
+ image_rect.top + size_reduction, \
+ image_rect.width - size_reduction, \
+ image_rect.height - size_reduction)
+
+ self.position = [ 0, randint(0, screen_h -1)]
+ self.screen_h = screen_h
+ self.screen_w = screen_w
+
+ def update(self):
+ image_center = self.image.get_width()/2
+ # Move the person
+ self.position = [ (self.position[0] + self.speed ), self.position[1] ]
+
+ def draw(self, on_surface):
+ on_surface.blit(self.image, self.position)
+
+ def died(self):
+ return self.position[0] > self.screen_w
+
+ def get_absolute_rect(self):
+ return self.collision_rect.move(self.position)
diff --git a/robot.py b/robot.py
index 15938b1..c47b2a2 100644
--- a/robot.py
+++ b/robot.py
@@ -1,11 +1,16 @@
import pygame
class Robot:
- def __init__(self, screen_w, screen_h, unit, speed):
+ def __init__(self, screen_w, screen_h, unit, robot_speed):
self.unit = unit
- self.speed = speed
+ self.speed = robot_speed
self.image = pygame.image.load("images/robot_1.png")
image_rect = self.image.get_rect()
+ size_reduction = 20*unit
+ self.collision_rect = pygame.Rect(image_rect.left + size_reduction, \
+ image_rect.top + size_reduction, \
+ image_rect.width - size_reduction, \
+ image_rect.height - size_reduction)
self.position = [0, 0]
self.screen_w = screen_w
self.screen_h = screen_h
@@ -33,6 +38,12 @@ class Robot:
# 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 collides_with(self, other):
+ return self.get_absolute_rect().colliderect(other.get_absolute_rect())
+
def draw(self, on_surface):
on_surface.blit(self.image, self.position)
+
+ def get_absolute_rect(self):
+ return self.collision_rect.move(self.position)