-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathPart-07-Objects.py
More file actions
109 lines (94 loc) · 3.36 KB
/
Part-07-Objects.py
File metadata and controls
109 lines (94 loc) · 3.36 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import pygame
import os
# Init and Create Window (win)
pygame.init()
win_height = 400
win_width = 800
win = pygame.display.set_mode((win_width, win_height))
# Load and Size Images
stationary = pygame.image.load(os.path.join("Assets/Hero", "standing.png"))
left = [pygame.image.load(os.path.join("Assets/Hero", "L1.png")),
pygame.image.load(os.path.join("Assets/Hero", "L2.png")),
pygame.image.load(os.path.join("Assets/Hero", "L3.png")),
pygame.image.load(os.path.join("Assets/Hero", "L4.png")),
pygame.image.load(os.path.join("Assets/Hero", "L5.png")),
pygame.image.load(os.path.join("Assets/Hero", "L6.png")),
pygame.image.load(os.path.join("Assets/Hero", "L7.png")),
pygame.image.load(os.path.join("Assets/Hero", "L8.png")),
pygame.image.load(os.path.join("Assets/Hero", "L9.png"))
]
right =[pygame.image.load(os.path.join("Assets/Hero", "R1.png")),
pygame.image.load(os.path.join("Assets/Hero", "R2.png")),
pygame.image.load(os.path.join("Assets/Hero", "R3.png")),
pygame.image.load(os.path.join("Assets/Hero", "R4.png")),
pygame.image.load(os.path.join("Assets/Hero", "R5.png")),
pygame.image.load(os.path.join("Assets/Hero", "R6.png")),
pygame.image.load(os.path.join("Assets/Hero", "R7.png")),
pygame.image.load(os.path.join("Assets/Hero", "R8.png")),
pygame.image.load(os.path.join("Assets/Hero", "R9.png"))
]
background = pygame.transform.scale(pygame.image.load(os.path.join("Assets", "Background.png")), (win_width, win_height))
class Hero:
def __init__(self, x, y):
# Walk
self.x = x
self.y = y
self.velx = 10
self.vely = 10
self.face_right = True
self.face_left = False
self.stepIndex = 0
# Jump
self.jump = False
def move_hero(self, userInput):
if userInput[pygame.K_RIGHT] and self.x <= win_width - 62:
self.x += self.velx
self.face_right = True
self.face_left = False
elif userInput[pygame.K_LEFT] and self.x >= 0:
self.x -= self.velx
self.face_right = False
self.face_left = True
else:
self.stepIndex = 0
def draw(self, win):
if self.stepIndex >= 9:
self.stepIndex = 0
if self.face_left:
win.blit(left[self.stepIndex], (self.x, self.y))
self.stepIndex += 1
if self.face_right:
win.blit(right[self.stepIndex], (self.x, self.y))
self.stepIndex += 1
def jump_motion(self, userInput):
if userInput[pygame.K_SPACE] and self.jump is False:
self.jump = True
if self.jump:
self.y -= self.vely*4
self.vely -= 1
if self.vely < -10:
self.jump = False
self.vely = 10
# Draw Game
def draw_game():
win.fill((0, 0, 0))
win.blit(background, (0,0))
player.draw(win)
pygame.time.delay(30)
pygame.display.update()
# Instance of Hero-Class
player = Hero(250, 290)
# Mainloop
run = True
while run:
# Quit Game
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# Input
userInput = pygame.key.get_pressed()
# Movement
player.move_hero(userInput)
player.jump_motion(userInput)
# Draw Game in Window
draw_game()