diff options
author | Rodolfo D. Arce S <rodolfo.arce.s@gmail.com> | 2011-05-12 16:05:53 (GMT) |
---|---|---|
committer | Rodolfo D. Arce S <rodolfo.arce.s@gmail.com> | 2011-05-12 16:05:53 (GMT) |
commit | 6b0c5b7a96f5c3a204fe2f5ef028457743ffe981 (patch) | |
tree | 2d2b088214d4a8ff416a47b2bab1c6e2afcb9c70 | |
parent | f982561ca100be85f903641542676ed74b76d504 (diff) |
Commented motion stop when releasing key
-rw-r--r-- | main.py | 30 |
1 files changed, 19 insertions, 11 deletions
@@ -1,8 +1,10 @@ # Robot saving nature -import pygame +import os +import sys import time +import pygame from pygame.locals import * pygame.init() @@ -16,12 +18,14 @@ screen = pygame.display.set_mode((window_w, window_h)) object_background = pygame.image.load('images/park_2.png') screen.blit(object_background, (0, 0)) +# 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()] @@ -30,6 +34,7 @@ y_direction = 0 screen.blit(object_robot, position_robot) + # Initialize images pygame.display.update() @@ -42,6 +47,7 @@ frame_time = 1.0/fps exit = False while not exit: + # Process pygame events for event in pygame.event.get(): # Escape key to quit the game @@ -49,7 +55,7 @@ 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: @@ -64,13 +70,15 @@ while not exit: if event.key == pygame.K_DOWN: y_direction = 1 x_direction = 0 - 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 + # 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 + if exit: break @@ -85,7 +93,7 @@ while not exit: screen.blit(object_robot, (position_robot[0], position_robot[1])) # Move the character around the screen - position_robot = [((position_robot[0] + x_direction*6)%window_w), ((position_robot[1] + y_direction*6)%window_h) ] + position_robot = [((position_robot[0] + x_direction * speed )%window_w), ((position_robot[1] + y_direction * speed )%window_h) ] pygame.display.update() |