Advertisement
Guest User

Untitled

a guest
Aug 30th, 2011
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.92 KB | None | 0 0
  1. import pygame
  2. from pygame.locals import *
  3.  
  4. import os, math
  5.  
  6. SCREEN_SIZE_X = 800
  7. SCREEN_SIZE_Y = 600
  8.  
  9. SPEED = 2
  10. TURN_SPEED = 3
  11.  
  12. RIGHT = 0
  13. LEFT = 1
  14.  
  15. class Car():
  16.         def __init__(self):
  17.                 self.sprite = pygame.image.load(os.path.join('img', 'car.png')).convert()
  18.                 self.rect = self.sprite.get_rect()
  19.  
  20.                 self.rect.center = SCREEN_SIZE_X/2, SCREEN_SIZE_Y - self.rect.height * 2
  21.  
  22.                 self.sprite.set_colorkey((255, 255, 255))
  23.  
  24.                 self.angle = 0
  25.  
  26.                 self.original = self.sprite
  27.  
  28.         def change_direction(self, direction):
  29.                 """
  30.                Set the orientation of the car.
  31.                """
  32.  
  33.                 if direction == RIGHT:
  34.                         self.angle += TURN_SPEED
  35.  
  36.                 elif direction == LEFT:
  37.                         self.angle -= TURN_SPEED                                                                                                                                    
  38.                                                                                                                                                                                    
  39.         def update(self):                                                                                                                                                          
  40.                 """                                                                                                                                                                
  41.                Update the position of car regarding the current orientation and the SPEED value.                                                                                  
  42.                """                                                                                                                                                                
  43.                                                                                                                                                                                    
  44.                 speedx = math.sin(self.angle * (math.pi/180)) * SPEED
  45.                 speedy = math.cos(self.angle * (math.pi/180)) * SPEED * -1
  46.  
  47.                 #rotate the car
  48.                 a_rad = math.asin(speedx/SPEED)
  49.                 a_deg = math.degrees(a_rad)
  50.  
  51.                 self.sprite = pygame.transform.rotate(self.original, a_deg * -1)
  52.                 self.rect = self.sprite.get_rect(center = self.rect.center)
  53.  
  54.                 #move the car
  55.                 self.rect.center = self.rect.center[0] + speedx, self.rect.center[1] + speedy
  56.  
  57.         def draw(self, screen):
  58.                 """
  59.                Draw the car on the screen.
  60.                """
  61.  
  62.                 screen.blit(self.sprite, self.rect.center)
  63.  
  64. def main():
  65.         pygame.init()
  66.  
  67.         screen = pygame.display.set_mode((SCREEN_SIZE_X, SCREEN_SIZE_Y), HWSURFACE|DOUBLEBUF)
  68.  
  69.         car = Car()
  70.  
  71.         clock = pygame.time.Clock()
  72.  
  73.         loop = True
  74.         while loop:
  75.                 clock.tick(60)
  76.  
  77.                 for event in pygame.event.get():
  78.                         if event.type == QUIT:
  79.                                 loop = False
  80.  
  81.                         if event.type == KEYDOWN:
  82.                                 if event.key == K_ESCAPE:
  83.                                         loop = False
  84.  
  85.  
  86.                 pygame.event.pump()
  87.  
  88.                 key = pygame.key.get_pressed()
  89.  
  90.                 #set the orientation of the car with the RIGHT and LEFT button
  91.                 if key[K_RIGHT]:
  92.                         car.change_direction(RIGHT)
  93.  
  94.                 if key[K_LEFT]:
  95.                         car.change_direction(LEFT)
  96.  
  97.                 car.update()
  98.  
  99.                 screen.fill((255, 255, 255))
  100.  
  101.                 car.draw(screen)
  102.  
  103.                 pygame.display.flip()
  104.  
  105. if __name__ == '__main__':
  106.         main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement