|
1 |
| -import time |
2 | 1 | import pygame
|
3 | 2 | from pygame.locals import *
|
4 | 3 | import timestep
|
|
12 | 11 | pygame.display.set_caption("Timestep Test")
|
13 | 12 | SCREEN_WIDTH, SCREEN_HEIGHT = screen.get_size()
|
14 | 13 |
|
15 |
| -font = pygame.font.SysFont("Calibri", 40) |
16 |
| -start = False |
17 |
| -start_time = 0 |
18 |
| - |
19 | 14 |
|
20 | 15 | class Player(timestep.Character):
|
21 | 16 | def __init__(self, x: int, y: int) -> None:
|
22 | 17 | self.image = pygame.Surface((100, 100))
|
23 | 18 | self.image.fill("red")
|
24 | 19 | super().__init__(x, y, self.image)
|
25 |
| - self.gravity = pgvec2(0, 0.1) |
| 20 | + self.gravity = pgvec2(0, 1) |
| 21 | + self.friction = 0.8 |
| 22 | + self.jumped = False |
26 | 23 |
|
27 | 24 | def update(self) -> None:
|
28 | 25 | super().update()
|
29 | 26 |
|
30 | 27 | self.vel.y += self.gravity.y
|
| 28 | + self.vel.x *= self.friction |
| 29 | + |
| 30 | + keys = pygame.key.get_pressed() |
| 31 | + if keys[K_RIGHT]: |
| 32 | + self.vel.x = 10 |
| 33 | + if keys[K_LEFT]: |
| 34 | + self.vel.x = -10 |
| 35 | + if keys[K_UP] and not self.jumped: |
| 36 | + self.vel.y = -15 |
| 37 | + self.jumped = True |
| 38 | + |
| 39 | + self.rect.x += round(self.vel.x) |
31 | 40 | self.rect.y += round(self.vel.y)
|
32 | 41 |
|
| 42 | + if self.rect.bottom > SCREEN_HEIGHT: |
| 43 | + self.rect.bottom = SCREEN_HEIGHT |
| 44 | + self.vel.y = 0 |
| 45 | + self.jumped = False |
| 46 | + if self.rect.right > SCREEN_WIDTH: |
| 47 | + self.rect.right = SCREEN_WIDTH |
| 48 | + self.vel.x = 0 |
| 49 | + elif self.rect.left < 0: |
| 50 | + self.rect.left = 0 |
| 51 | + self.vel.x = 0 |
| 52 | + |
33 | 53 |
|
34 | 54 | player = Player(500, 0)
|
35 | 55 |
|
36 | 56 |
|
37 | 57 | class game_loop(timestep.Timestep):
|
38 | 58 | def update(self):
|
39 |
| - global game_running, start_time, start |
| 59 | + global game_running |
40 | 60 | for event in pygame.event.get():
|
41 | 61 | if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
|
42 | 62 | game_running = False
|
43 |
| - if event.type == KEYDOWN: |
44 |
| - if event.key == K_SPACE and not start: |
45 |
| - start = True |
46 |
| - start_time = time.time() |
47 | 63 |
|
48 |
| - if start: |
49 |
| - player.update() |
| 64 | + player.update() |
50 | 65 |
|
51 | 66 | def render(self, alpha):
|
52 |
| - global game_running |
53 | 67 |
|
54 | 68 | screen.fill((0, 0, 0))
|
55 | 69 |
|
56 | 70 | player.draw(screen, alpha)
|
57 | 71 |
|
58 |
| - if player.rect.y >= SCREEN_HEIGHT: |
59 |
| - finish = time.time() - start_time |
60 |
| - print(finish) |
61 |
| - game_running = False |
62 |
| - |
63 | 72 | pygame.display.flip()
|
64 | 73 |
|
65 | 74 |
|
|
0 commit comments