-
Notifications
You must be signed in to change notification settings - Fork 0
/
step2.py
57 lines (41 loc) · 1.15 KB
/
step2.py
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
import pygame
import random
pygame.init()
width = 600
height = 400
fps = 30
clock = pygame.time.Clock()
surface = pygame.display.set_mode((width, height))
class Player:
def __init__(self):
self.rect = pygame.Rect(0, 0, 60, 60)
self.rect.center = 100, -20
self.vel_y = 0
def draw(self, surface):
pygame.draw.rect(surface, (0, 0, 255), self.rect)
def update(self):
self.vel_y += gravity
self.rect.y += self.vel_y
if self.rect.bottom > floor_level:
self.rect.bottom = floor_level
self.vel_y = 0
def on_ground(self):
return self.rect.bottom == floor_level
p = Player()
gravity = 2
floor_level = 350
jump_strength = 30
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
if p.on_ground():
p.vel_y = -jump_strength
p.update()
surface.fill((255, 255, 255))
p.draw(surface)
pygame.display.update()
clock.tick(fps)