Skip to content

Commit bcd8d41

Browse files
committed
polish example
1 parent 44a95cc commit bcd8d41

File tree

2 files changed

+37
-27
lines changed

2 files changed

+37
-27
lines changed

example.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import time
21
import pygame
32
from pygame.locals import *
43
import timestep
@@ -12,54 +11,64 @@
1211
pygame.display.set_caption("Timestep Test")
1312
SCREEN_WIDTH, SCREEN_HEIGHT = screen.get_size()
1413

15-
font = pygame.font.SysFont("Calibri", 40)
16-
start = False
17-
start_time = 0
18-
1914

2015
class Player(timestep.Character):
2116
def __init__(self, x: int, y: int) -> None:
2217
self.image = pygame.Surface((100, 100))
2318
self.image.fill("red")
2419
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
2623

2724
def update(self) -> None:
2825
super().update()
2926

3027
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)
3140
self.rect.y += round(self.vel.y)
3241

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+
3353

3454
player = Player(500, 0)
3555

3656

3757
class game_loop(timestep.Timestep):
3858
def update(self):
39-
global game_running, start_time, start
59+
global game_running
4060
for event in pygame.event.get():
4161
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
4262
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()
4763

48-
if start:
49-
player.update()
64+
player.update()
5065

5166
def render(self, alpha):
52-
global game_running
5367

5468
screen.fill((0, 0, 0))
5569

5670
player.draw(screen, alpha)
5771

58-
if player.rect.y >= SCREEN_HEIGHT:
59-
finish = time.time() - start_time
60-
print(finish)
61-
game_running = False
62-
6372
pygame.display.flip()
6473

6574

timestep.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import pygame
44
import time
55

6-
76
class Character:
87
"""Base class for any sprites or objects"""
98

@@ -12,18 +11,20 @@ def __init__(self, x: int, y: int, image: pygame.Surface) -> None:
1211
self.rect = self.image.get_rect()
1312
self.rect.topleft = x, y
1413
self.vel = pygame.math.Vector2(0, 0)
15-
self.prev_pos = pygame.math.Vector2(self.rect.topleft)
14+
self.pos = pygame.math.Vector2(self.rect.topleft)
15+
self.prev_pos = self.pos
1616

1717
def update(self) -> None:
18-
"""Update """
18+
"""Override this method with movement, input, collisions etc."""
1919
self.prev_pos = self.__get_rect_pos()
2020

2121
def draw(self, surface: pygame.Surface, alpha: float) -> None:
22-
pos = self.prev_pos.lerp(self.__get_rect_pos(), alpha)
23-
surface.blit(self.image, pos)
22+
"""Draw the Character to the screen."""
23+
self.pos = self.prev_pos.lerp(self.__get_rect_pos(), alpha)
24+
surface.blit(self.image, (round(self.pos.x), round(self.pos.y)))
2425

2526
def __get_rect_pos(self) -> pygame.math.Vector2:
26-
"""Return the position of the Charater's rect as a Vec2"""
27+
"""Return the position of the Charater's rect as a Vec2."""
2728
return pygame.math.Vector2(self.rect.topleft)
2829

2930

@@ -37,9 +38,9 @@ def __init__(self, step: float) -> None:
3738

3839
def __calc_dt(self) -> float:
3940
now = time.time()
40-
dt = now - self.__last_time
41+
self.dt = now - self.__last_time
4142
self.__last_time = now
42-
return dt
43+
return self.dt
4344

4445
def update(self) -> None:
4546
"""Override this function with event loop and movement"""

0 commit comments

Comments
 (0)