initial prototype
This commit is contained in:
12 files changed
+125
No files matched your search
Executable
+6
@@ -0,0 +1,6 @@
|
|||||||
|
#!/usr/bin/env python3.11
|
||||||
|
from game import Window
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
game = Window("Formula2D", (1000, 800))
|
||||||
|
game.main_loop()
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -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)
|
||||||
@@ -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)
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 182 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 26 KiB |
@@ -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)
|
||||||
Reference in new issue
Block a user