Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ca9480847c | ||
|
|
c9e2c4f199 |
@@ -3,7 +3,7 @@ import pygame
|
||||
class Input:
|
||||
def __init__(self):
|
||||
self.left: bool = False
|
||||
self.right: bool = True
|
||||
self.right: bool = False
|
||||
self.down: bool = False
|
||||
self.up: bool = False
|
||||
self.close: bool = False
|
||||
@@ -24,13 +24,13 @@ class Input:
|
||||
self.down = True
|
||||
if event.type == pygame.KEYUP:
|
||||
if event.key == pygame.K_LEFT:
|
||||
self.left = True
|
||||
self.left = False
|
||||
if event.key == pygame.K_RIGHT:
|
||||
self.right = True
|
||||
self.right = False
|
||||
if event.key == pygame.K_UP:
|
||||
self.up = True
|
||||
self.up = False
|
||||
if event.key == pygame.K_DOWN:
|
||||
self.down = True
|
||||
self.down = False
|
||||
|
||||
def horizontal(self) -> float:
|
||||
return self.right - self.left
|
||||
|
||||
@@ -23,11 +23,12 @@ class Kart:
|
||||
def tick(self, input: Input):
|
||||
input_x = input.horizontal()
|
||||
input_y = input.vertical()
|
||||
assert abs(input_x) < 1
|
||||
assert abs(input_y) < 1
|
||||
assert abs(input_x) <= 1
|
||||
assert abs(input_y) <= 1
|
||||
|
||||
acc_amount = input_y * 2
|
||||
self.vel += Vec2(acc_amount, 0).rotate(self.dir)
|
||||
self.vel *= 0.9
|
||||
steer = input_x * 2
|
||||
self.dir += steer
|
||||
|
||||
@@ -44,8 +45,8 @@ class Kart:
|
||||
|
||||
def consume_item(self, item: Item):
|
||||
if item.kind == items.ITEM_BANANA:
|
||||
self.speed_max += 1
|
||||
self.speed_max += 0.01
|
||||
elif item.kind == items.ITEM_BANANA:
|
||||
self.speed_max -= 1
|
||||
self.speed_max -= 0.01
|
||||
|
||||
self.speed_max = clamp(self.speed_max, 0, 100)
|
||||
|
||||
@@ -1,13 +1,55 @@
|
||||
import pygame
|
||||
|
||||
from input import Input
|
||||
from kart import Kart
|
||||
|
||||
Vec2 = pygame.Vector2
|
||||
|
||||
kart_image = pygame.surface.Surface((10, 10))
|
||||
|
||||
player = Kart(
|
||||
name = "epsilon",
|
||||
pos = Vec2(0, 0),
|
||||
vel = Vec2(2, 0),
|
||||
dir = 0,
|
||||
speed_max = 10,
|
||||
speed_max = 3,
|
||||
)
|
||||
|
||||
clock = pygame.Clock()
|
||||
|
||||
def main():
|
||||
pygame.init()
|
||||
|
||||
input = Input()
|
||||
width, height = 800, 600
|
||||
screen = pygame.display.set_mode((width, height))
|
||||
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:
|
||||
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__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user