This commit is contained in:
alstergym
2026-09-07 15:51:09 +02:00
parent c9e2c4f199
commit ca9480847c
3 changed files with 37 additions and 10 deletions
+5 -5
View File
@@ -3,7 +3,7 @@ import pygame
class Input: class Input:
def __init__(self): def __init__(self):
self.left: bool = False self.left: bool = False
self.right: bool = True self.right: bool = False
self.down: bool = False self.down: bool = False
self.up: bool = False self.up: bool = False
self.close: bool = False self.close: bool = False
@@ -24,13 +24,13 @@ class Input:
self.down = True self.down = True
if event.type == pygame.KEYUP: if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT: if event.key == pygame.K_LEFT:
self.left = True self.left = False
if event.key == pygame.K_RIGHT: if event.key == pygame.K_RIGHT:
self.right = True self.right = False
if event.key == pygame.K_UP: if event.key == pygame.K_UP:
self.up = True self.up = False
if event.key == pygame.K_DOWN: if event.key == pygame.K_DOWN:
self.down = True self.down = False
def horizontal(self) -> float: def horizontal(self) -> float:
return self.right - self.left return self.right - self.left
+5 -4
View File
@@ -23,11 +23,12 @@ class Kart:
def tick(self, input: Input): def tick(self, input: Input):
input_x = input.horizontal() input_x = input.horizontal()
input_y = input.vertical() input_y = input.vertical()
assert abs(input_x) < 1 assert abs(input_x) <= 1
assert abs(input_y) < 1 assert abs(input_y) <= 1
acc_amount = input_y * 2 acc_amount = input_y * 2
self.vel += Vec2(acc_amount, 0).rotate(self.dir) self.vel += Vec2(acc_amount, 0).rotate(self.dir)
self.vel *= 0.9
steer = input_x * 2 steer = input_x * 2
self.dir += steer self.dir += steer
@@ -44,8 +45,8 @@ class Kart:
def consume_item(self, item: Item): def consume_item(self, item: Item):
if item.kind == items.ITEM_BANANA: if item.kind == items.ITEM_BANANA:
self.speed_max += 1 self.speed_max += 0.01
elif item.kind == items.ITEM_BANANA: elif item.kind == items.ITEM_BANANA:
self.speed_max -= 1 self.speed_max -= 0.01
self.speed_max = clamp(self.speed_max, 0, 100) self.speed_max = clamp(self.speed_max, 0, 100)
+27 -1
View File
@@ -5,14 +5,18 @@ from kart import Kart
Vec2 = pygame.Vector2 Vec2 = pygame.Vector2
kart_image = pygame.surface.Surface((10, 10))
player = Kart( player = Kart(
name = "epsilon", name = "epsilon",
pos = Vec2(0, 0), pos = Vec2(0, 0),
vel = Vec2(2, 0), vel = Vec2(2, 0),
dir = 0, dir = 0,
speed_max = 10, speed_max = 3,
) )
clock = pygame.Clock()
def main(): def main():
pygame.init() pygame.init()
@@ -21,8 +25,30 @@ def main():
screen = pygame.display.set_mode((width, height)) screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Pygame Window Example") pygame.display.set_caption("Pygame Window Example")
kart = Kart(
name = "jerry",
pos = Vec2(0.0, 0.0),
vel = Vec2(0.0, 0.0),
dir = 0.0,
speed_max = 10.0
)
while not input.close: while not input.close:
input.poll() input.poll()
kart.tick(input)
# Draw game:
screen.fill((0, 125, 0))
pygame.draw.rect(screen, (255, 0, 0), pygame.Rect(kart.pos.x - 5, kart.pos.y - 5, 5, 5))
pygame.draw.line(screen, (0, 0, 0), kart.pos, kart.pos + Vec2(100, 0).rotate(kart.dir))
pygame.draw.rect(screen, (0, 0, 255), pygame.Rect(kart.pos.x - 5, kart.pos.y - 5, 5, 5))
pygame.draw.line(screen, (0, 0, 0), kart.pos, kart.pos + Vec2(100, 0).rotate(kart.dir))
pygame.display.flip()
clock.tick(60)
if __name__ == "__main__": if __name__ == "__main__":