-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRPG_Game1.py
More file actions
173 lines (132 loc) · 5.46 KB
/
RPG_Game1.py
File metadata and controls
173 lines (132 loc) · 5.46 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#Pygame dev 1
import pygame
Screen_Title = "Crossy RPG"
Screen_Width = 800
Screen_Height = 800
White_Color = (255, 255, 255)
Black_Color = (0, 0, 0)
clock = pygame.time.Clock()
pygame.font.init()
font = pygame.font.SysFont('comicsans', 75)
class Game:
Tick_Rate = 60
def __init__(self, image_path, title, width, height):
self.title = title
self.width = width
self.height = height
self.game_screen = pygame.display.set_mode((width, height))
self.game_screen.fill(White_Color)
pygame.display.set_caption(title)
background_image = pygame.image.load(image_path)
self.image = pygame.transform.scale(background_image, (width, height))
def run_game_loop(self, level_speed):
is_game_over = False
did_win = False
direction = 0
player_character = PlayerCharacter('player.png', 382, 700, 36, 48)
enemy_0 = NonPlayerCharacter('enemy.png', 20, 600, 48, 36)
enemy_0.Speed *= level_speed
enemy_1 = NonPlayerCharacter('enemy.png', self.width - 50, 400, 48, 36)
enemy_1.Speed *= level_speed
enemy_2 = NonPlayerCharacter('enemy.png', self.width - 400, 200, 48, 36)
enemy_2.Speed *= level_speed
enemies = [enemy_0]
treasure = GameObject('treasure.png', 374, 50, 52, 36)
while not is_game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
is_game_over = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
direction = 1
elif event.key == pygame.K_DOWN:
direction = -1
elif event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
direction = 0
print(event)
self.game_screen.fill(White_Color)
self.game_screen.blit(self.image, (0, 0))
treasure.draw(self.game_screen)
player_character.move(direction, self.height)
player_character.draw(self.game_screen)
enemy_0.move(self.width)
enemy_0.draw(self.game_screen)
if level_speed > 2:
enemies.append(enemy_1)
enemy_1.move(self.width)
enemy_1.draw(self.game_screen)
if level_speed > 4:
enemies.append(enemy_2)
enemy_2.move(self.width)
enemy_2.draw(self.game_screen)
if player_character.detect_collision(treasure):
is_game_over = True
did_win = True
text = font.render('And there was much rejoicing....', True, Black_Color)
self.game_screen.blit(text, (5, 150))
pygame.display.update()
clock.tick(1)
break
else:
for enemy in enemies:
if player_character.detect_collision(enemy):
is_game_over = True
did_win = False
text = font.render('Your Mother was a hampster!', True, Black_Color)
self.game_screen.blit(text, (25, 150))
pygame.display.update()
clock.tick(1)
break
pygame.display.update()
clock.tick(self.Tick_Rate)
if did_win:
self.run_game_loop(level_speed + 0.5)
else:
return
class GameObject:
def __init__(self, image_path, x, y, width, height):
object_image = pygame.image.load(image_path)
self.image = pygame.transform.scale(object_image, (width, height))
self.x_pos = x
self.y_pos = y
self.width = width
self.height = height
def draw(self, background):
background.blit(self.image, (self.x_pos, self.y_pos))
class PlayerCharacter(GameObject):
Speed = 5
def __init__(self, image_path, x, y, width, height):
super().__init__(image_path, x, y, width, height)
def move(self, direction, max_height):
if direction > 0:
self.y_pos -= self.Speed
elif direction < 0:
self.y_pos += self.Speed
if self.y_pos >= max_height - 50:
self.y_pos = max_height - 50
def detect_collision(self, other_body):
if self.y_pos >= other_body.y_pos + other_body.height:
return False
elif self.y_pos <= other_body.y_pos:
return False
if self.x_pos >= other_body.x_pos + other_body.width:
return False
elif self.x_pos + self.width <= other_body.x_pos:
return False
return True
class NonPlayerCharacter(GameObject):
Speed = 5
def __init__(self, image_path, x, y, width, height):
super().__init__(image_path, x, y, width, height)
def move(self, max_width):
if self.x_pos <= 20:
self.Speed = abs(self.Speed)
elif self.x_pos >= max_width - 70:
self.Speed = -abs(self.Speed)
self.x_pos += self.Speed
pygame.init()
new_game = Game('background.png',Screen_Title, Screen_Width, Screen_Height)
new_game.run_game_loop(1)
pygame.quit()
quit()