commit c7439e40570ef736866244ffd0426b9022c50bd6 Author: Braeden Sowinski Date: Mon May 22 13:00:02 2023 -0700 initial prototype diff --git a/__main__.py b/__main__.py new file mode 100755 index 0000000..36d69e4 --- /dev/null +++ b/__main__.py @@ -0,0 +1,6 @@ +#!/usr/bin/env python3.11 +from game import Window + +if __name__ == '__main__': + game = Window("Formula2D", (1000, 800)) + game.main_loop() diff --git a/__pycache__/__main__.cpython-311.pyc b/__pycache__/__main__.cpython-311.pyc new file mode 100644 index 0000000..8b0fb48 Binary files /dev/null and b/__pycache__/__main__.cpython-311.pyc differ diff --git a/__pycache__/car.cpython-311.pyc b/__pycache__/car.cpython-311.pyc new file mode 100644 index 0000000..94de20b Binary files /dev/null and b/__pycache__/car.cpython-311.pyc differ diff --git a/__pycache__/game.cpython-311.pyc b/__pycache__/game.cpython-311.pyc new file mode 100644 index 0000000..d88ddae Binary files /dev/null and b/__pycache__/game.cpython-311.pyc differ diff --git a/__pycache__/models.cpython-311.pyc b/__pycache__/models.cpython-311.pyc new file mode 100644 index 0000000..4f1c7a9 Binary files /dev/null and b/__pycache__/models.cpython-311.pyc differ diff --git a/__pycache__/util.cpython-311.pyc b/__pycache__/util.cpython-311.pyc new file mode 100644 index 0000000..7fec2ca Binary files /dev/null and b/__pycache__/util.cpython-311.pyc differ diff --git a/__pycache__/utils.cpython-311.pyc b/__pycache__/utils.cpython-311.pyc new file mode 100644 index 0000000..7d8bbd5 Binary files /dev/null and b/__pycache__/utils.cpython-311.pyc differ diff --git a/game.py b/game.py new file mode 100644 index 0000000..acb98ea --- /dev/null +++ b/game.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python3.11 +import pygame +from models import Car +from util import load_image + +class Background(pygame.sprite.Sprite): + def __init__(self, filename, location, size): + super().__init__() + self.image = load_image(filename) + self.image = pygame.transform.scale(self.image, size) + self.rect = self.image.get_rect() + self.rect.left, self.rect.top = location + + def draw(self, window): + window.blit(self.image, self.rect) + +class Window: + def __init__(self, caption, size): + self.caption = caption + self.size = size + self.background = Background('track.jpg', [0, 0], size) + self.running = False + + self._init_pygame() + self.screen = pygame.display.set_mode((self.size)) + self.clock = pygame.time.Clock() + + self.car = Car(880, 300) + + def main_loop(self): + self.running = True + while self.running: + self._handle_input() + self._process_game_logic() + self._draw() + + def _init_pygame(self): + pygame.init() + pygame.display.set_caption(self.caption) + + def _handle_input(self): + for event in pygame.event.get(): + if event.type == pygame.QUIT or ( + event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE + ): + quit() + + pressed = pygame.key.get_pressed() + self.car.speed *= 0.95 + if pressed[pygame.K_UP]: self.car.speed += 0.2 + if pressed[pygame.K_DOWN]: self.car.speed -= 0.2 + if round(self.car.speed, 1) == 0.1: self.car.speed = 0 + + c = 5 + a = c / (self.car.speed + 1) + if self.car.speed == 0: a = 0 + + if pressed[pygame.K_LEFT]: self.car.angle += a + if pressed[pygame.K_RIGHT]: self.car.angle -= a + + def _process_game_logic(self): + self.car.update() + + def _draw(self): + self.screen.fill((0, 0, 0)) + self.background.draw(self.screen) + self.car.draw(self.screen) + pygame.display.flip() + self.clock.tick(60) diff --git a/models.py b/models.py new file mode 100644 index 0000000..3054413 --- /dev/null +++ b/models.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python3.11 +from pygame import Rect, Surface, SRCALPHA +from pygame.transform import rotate, scale +from util import load_image +import math, os + +class Car: + def __init__(self, x, y): + # Load image and set to scale + img = load_image('car.png') + img = rotate(img, 90) + img = scale(img, (img.get_width()/10, img.get_height()/10)) + + self.width = img.get_height() + self.height = img.get_width() + self.x = x - self.width / 2 + self.y = y - self.height / 2 + self.rect = Rect(x, y, self.height, self.width) + self.surface = Surface((self.height, self.width), SRCALPHA, 32).convert_alpha() + self.surface.blit(img, (0, 0)) + self.angle = 180 + self.speed = 0 + + def update(self): + self.x -= self.speed * math.sin(math.radians(self.angle)) + self.y -= self.speed * math.cos(math.radians(-self.angle)) + + def draw(self, window): + self.rect.topleft = (int(self.x), int(self.y)) + rotated = rotate(self.surface, self.angle) + surface_rect = self.surface.get_rect(topleft = self.rect.topleft) + new_rect = rotated.get_rect(center = surface_rect.center) + window.blit(rotated, new_rect.topleft) \ No newline at end of file diff --git a/resources/car.png b/resources/car.png new file mode 100644 index 0000000..b5d93d8 Binary files /dev/null and b/resources/car.png differ diff --git a/resources/track.jpg b/resources/track.jpg new file mode 100644 index 0000000..b3f15bf Binary files /dev/null and b/resources/track.jpg differ diff --git a/util.py b/util.py new file mode 100644 index 0000000..9d8afd8 --- /dev/null +++ b/util.py @@ -0,0 +1,17 @@ +#!/usr/bin/env python3.11 +from pygame.image import load +from pygame import Surface +import os + +def load_image(filename: str) -> Surface: + if '.' in filename and filename.split('.')[1] not in ['png', 'jpg', 'jpeg']: + raise Exception(f'Invalid filename: must be of filetype \'.png\' \'.jpg\' or \'.jpeg\' not of .{filename.split(".")[1]}') + + cwd = os.getcwd() + dirName = '' if cwd.endswith('formula2D') else '/formula2D' + path = f'{cwd}{dirName}/resources/{filename}' + + if not os.path.isfile(path): + raise Exception(f'Filepath not found: \'{path}\' cannot be found') + + return load(path)