-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
80 lines (59 loc) · 1.98 KB
/
example.py
File metadata and controls
80 lines (59 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import pygame
from pygame.locals import *
import timestep
pygame.init()
RESOLUTION = pygame.math.Vector2(1000, 600)
screen = pygame.display.set_mode(RESOLUTION)
pygame.display.set_caption("Timestep Test")
SCREEN_WIDTH, SCREEN_HEIGHT = screen.get_size()
class Player(timestep.Character):
def __init__(self, x_pos: float, y_pos: float) -> None:
self.image = pygame.Surface((100, 100))
self.image.fill("red")
self.rect = self.image.get_frect()
super().__init__(x_pos, y_pos, self.image, self.rect)
self.gravity = pygame.math.Vector2(0, 1)
self.vel = pygame.math.Vector2(0, 0)
self.friction = 0.8
self.jumped = False
def update(self) -> None:
super().update()
self.vel.y += self.gravity.y
self.vel.x *= self.friction
keys = pygame.key.get_pressed()
if keys[K_RIGHT]:
self.vel.x = 10
if keys[K_LEFT]:
self.vel.x = -10
if keys[K_UP] and not self.jumped:
self.vel.y = -15
self.jumped = True
self.rect.x += self.vel.x
self.rect.y += self.vel.y
if self.rect.bottom > SCREEN_HEIGHT:
self.rect.bottom = SCREEN_HEIGHT
self.vel.y = 0
self.jumped = False
if self.rect.right > SCREEN_WIDTH:
self.rect.right = SCREEN_WIDTH
self.vel.x = 0
elif self.rect.left < 0:
self.rect.left = 0
self.vel.x = 0
player = Player(500, 0)
class game_loop(timestep.Timestep):
def update(self):
global game_running
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
game_running = False
player.update()
def render(self, alpha):
screen.fill((0, 0, 0))
player.draw(screen, alpha)
pygame.display.flip()
game = game_loop(1/60)
game_running = True
while game_running:
game.run_game()
pygame.quit()