diff options
author | Rodolfo D. Arce S <rodolfo.arce.s@gmail.com> | 2011-05-20 16:16:08 (GMT) |
---|---|---|
committer | Rodolfo D. Arce S <rodolfo.arce.s@gmail.com> | 2011-05-20 16:16:08 (GMT) |
commit | 1468bb10e27545bb13b4ecf51e2f4728d571ac84 (patch) | |
tree | 3d327f30688ed30067a63375e32743ad6ee96098 | |
parent | 83edc4d7a95b11072c5604d19cad16ea545c5f64 (diff) |
Added ranger class for our hero, and images representing characteres
-rw-r--r-- | images/ranger_hat.png | bin | 0 -> 3487 bytes | |||
-rw-r--r-- | images/treecutter_hat.png | bin | 0 -> 41702 bytes | |||
-rw-r--r-- | main.py | 17 | ||||
-rw-r--r-- | ranger.py | 49 |
4 files changed, 63 insertions, 3 deletions
diff --git a/images/ranger_hat.png b/images/ranger_hat.png Binary files differnew file mode 100644 index 0000000..3a7dfff --- /dev/null +++ b/images/ranger_hat.png diff --git a/images/treecutter_hat.png b/images/treecutter_hat.png Binary files differnew file mode 100644 index 0000000..b215968 --- /dev/null +++ b/images/treecutter_hat.png @@ -1,7 +1,10 @@ import pygame from pygame import K_ESCAPE +from pygame.locals import * + from random import randint from tree import Tree +from ranger import Ranger global window_h, window_w window_h = 640 @@ -18,6 +21,10 @@ tree_radius = 15 trees = [] clock = pygame.time.Clock() +# Ranger +unit = 1.0 +ranger_speed = 6 +ranger = Ranger(window_h, window_w, unit, ranger_speed) # Draw background screen.fill(bg_color) @@ -39,11 +46,15 @@ while True: screen.fill(bg_color) + ranger.update() + + ranger.draw(screen) + #for arbol in trees: # trees.remove(arbol) - for arbol in trees: - arbol.draw() + #for arbol in trees: + # arbol.draw() pygame.display.update() - clock.tick(60) + #clock.tick(60) diff --git a/ranger.py b/ranger.py new file mode 100644 index 0000000..2c2cddb --- /dev/null +++ b/ranger.py @@ -0,0 +1,49 @@ +import pygame + +class Ranger: + def __init__(self, screen_w, screen_h, unit, ranger_speed): + self.unit = unit + self.speed = ranger_speed + self.image = pygame.image.load("images/ranger_hat.png") + image_rect = self.image.get_rect() + size_reduction = 15*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 + 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 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) |