-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathdino_step3_final.py
More file actions
88 lines (71 loc) · 2.14 KB
/
dino_step3_final.py
File metadata and controls
88 lines (71 loc) · 2.14 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
81
82
83
84
85
86
87
88
# python game with pygame : Jumping dino
# by. BlockDMask
import pygame
import sys
# step1 : set screen, fps
# step2 : show dino, jump dino
# step3 : show tree, move tree
pygame.init()
pygame.display.set_caption('Jumping dino')
MAX_WIDTH = 800
MAX_HEIGHT = 400
def main():
# set screen, fps
screen = pygame.display.set_mode((MAX_WIDTH, MAX_HEIGHT))
fps = pygame.time.Clock()
# dino
imgDino1 = pygame.image.load('images/dino1.png')
imgDino2 = pygame.image.load('images/dino2.png')
dino_height = imgDino1.get_size()[1]
dino_bottom = MAX_HEIGHT - dino_height
dino_x = 50
dino_y = dino_bottom
jump_top = 200
leg_swap = True
is_bottom = True
is_go_up = False
# tree
imgTree = pygame.image.load('images/tree.png')
tree_height = imgTree.get_size()[1]
tree_x = MAX_WIDTH
tree_y = MAX_HEIGHT - tree_height
while True:
screen.fill((255, 255, 255))
# event check
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if is_bottom:
is_go_up = True
is_bottom = False
# dino move
if is_go_up:
dino_y -= 10.0
elif not is_go_up and not is_bottom:
dino_y += 10.0
# dino top and bottom check
if is_go_up and dino_y <= jump_top:
is_go_up = False
if not is_bottom and dino_y >= dino_bottom:
is_bottom = True
dino_y = dino_bottom
# tree move
tree_x -= 12.0
if tree_x <= 0:
tree_x = MAX_WIDTH
# draw tree
screen.blit(imgTree, (tree_x, tree_y))
# draw dino
if leg_swap:
screen.blit(imgDino1, (dino_x, dino_y))
leg_swap = False
else:
screen.blit(imgDino2, (dino_x, dino_y))
leg_swap = True
# update
pygame.display.update()
fps.tick(30)
if __name__ == '__main__':
main()