├── Audi.png ├── Track.png └── gta2_pygame.py /Audi.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clear-code-projects/pygame-gta2/f4104c0b8b630ba2bc9a3360f5b88005691e3ad4/Audi.png -------------------------------------------------------------------------------- /Track.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clear-code-projects/pygame-gta2/f4104c0b8b630ba2bc9a3360f5b88005691e3ad4/Track.png -------------------------------------------------------------------------------- /gta2_pygame.py: -------------------------------------------------------------------------------- 1 | import pygame,sys 2 | 3 | class Car(pygame.sprite.Sprite): 4 | def __init__(self): 5 | super().__init__() 6 | self.original_image = pygame.image.load('Audi.png') 7 | self.image = self.original_image 8 | self.rect = self.image.get_rect(center = (640,360)) 9 | self.angle = 0 10 | self.rotation_speed = 1.8 11 | self.direction = 0 12 | self.forward = pygame.math.Vector2(0,-1) 13 | self.active = False 14 | 15 | def set_rotation(self): 16 | if self.direction == 1: 17 | self.angle -= self.rotation_speed 18 | if self.direction == -1: 19 | self.angle += self.rotation_speed 20 | 21 | self.image = pygame.transform.rotozoom(self.original_image,self.angle,0.25) 22 | self.rect = self.image.get_rect(center = self.rect.center) 23 | 24 | def get_rotation(self): 25 | if self.direction == 1: 26 | self.forward.rotate_ip(self.rotation_speed) 27 | if self.direction == -1: 28 | self.forward.rotate_ip(-self.rotation_speed) 29 | 30 | def accelerate(self): 31 | if self.active: 32 | self.rect.center += self.forward * 5 33 | 34 | def update(self): 35 | self.set_rotation() 36 | self.get_rotation() 37 | self.accelerate() 38 | 39 | 40 | pygame.init() 41 | screen = pygame.display.set_mode((1280,720)) 42 | clock = pygame.time.Clock() 43 | bg_track = pygame.image.load('Track.png') 44 | 45 | car = pygame.sprite.GroupSingle(Car()) 46 | 47 | while True: 48 | for event in pygame.event.get(): 49 | if event.type == pygame.QUIT: 50 | pygame.quit() 51 | sys.exit() 52 | if event.type == pygame.KEYDOWN: 53 | if event.key == pygame.K_RIGHT: car.sprite.direction += 1 54 | if event.key == pygame.K_LEFT: car.sprite.direction -= 1 55 | if event.key == pygame.K_SPACE: car.sprite.active = True 56 | 57 | if event.type == pygame.KEYUP: 58 | if event.key == pygame.K_RIGHT: car.sprite.direction -= 1 59 | if event.key == pygame.K_LEFT: car.sprite.direction += 1 60 | if event.key == pygame.K_SPACE: car.sprite.active = False 61 | 62 | screen.blit(bg_track,(0,0)) 63 | car.draw(screen) 64 | car.update() 65 | pygame.display.update() 66 | clock.tick(120) --------------------------------------------------------------------------------