1
+ import pygame
2
+ from random import randint
3
+
4
+ class Coinport :
5
+
6
+ def __init__ (self ):
7
+ pygame .init ()
8
+
9
+ self .load_images ()
10
+ self .width = 640
11
+ self .height = 480
12
+ self .window = pygame .display .set_mode ((self .width , self .height ))
13
+
14
+ self .game_font = pygame .font .SysFont ("Arial" , 24 )
15
+
16
+ pygame .display .set_caption ("Coinport" )
17
+
18
+
19
+ self .door ()
20
+
21
+ self .clock = pygame .time .Clock ()
22
+
23
+ self .to_left = False
24
+ self .to_right = False
25
+ self .x = 0
26
+ self .y = self .height - self .images ["robot" ].get_height ()
27
+
28
+ self .points = 0
29
+
30
+ self .start = True
31
+ self .end = False
32
+
33
+ self .number = 10
34
+
35
+ self .main_loop ()
36
+
37
+
38
+
39
+ def load_images (self ):
40
+ self .images = {}
41
+ for name in ["coin" , "door" , "monster" , "robot" ]:
42
+ self .images [name ] = pygame .image .load (name + ".png" )
43
+
44
+ def coin_monster_pos (self , number ):
45
+ self .position_coin = []
46
+ self .position_monster = []
47
+
48
+ self .number = number
49
+
50
+ for i in range (self .number ):
51
+ self .position_coin .append ([randint (0 , self .width - self .images ["coin" ].get_width ()), - randint (100 , 1000 )])
52
+ self .position_monster .append ([randint (0 , self .width - self .images ["monster" ].get_width ()), - randint (100 , 1000 )])
53
+
54
+ def main_loop (self ):
55
+ while True :
56
+ self .check_events ()
57
+ self .draw_window ()
58
+ if self .start :
59
+ continue
60
+ if self .end :
61
+ continue
62
+ self .fall ()
63
+ self .robot_move ()
64
+
65
+
66
+
67
+ def check_events (self ):
68
+ for event in pygame .event .get ():
69
+ if event .type == pygame .KEYDOWN :
70
+ if event .key == pygame .K_LEFT :
71
+ self .to_left = True
72
+ if event .key == pygame .K_RIGHT :
73
+ self .to_right = True
74
+
75
+ if event .key == pygame .K_SPACE :
76
+ self .x = self .d_x
77
+ self .y = self .y_x
78
+
79
+ self .door ()
80
+
81
+ if event .key == pygame .K_RETURN :
82
+ if self .end :
83
+ self .end = False
84
+ self .coin_monster_pos (self .number )
85
+ self .points = 0
86
+
87
+ # Giving level by changing amount of monster and coin
88
+ if event .key == pygame .K_1 :
89
+ self .start = False
90
+ self .number = 10
91
+ self .coin_monster_pos (10 )
92
+
93
+ if event .key == pygame .K_2 :
94
+ self .start = False
95
+ self .number = 20
96
+ self .coin_monster_pos (20 )
97
+
98
+ if event .key == pygame .K_3 :
99
+ self .start = False
100
+ self .number = 30
101
+ self .coin_monster_pos (30 )
102
+
103
+ if event .key == pygame .K_ESCAPE :
104
+ exit ()
105
+
106
+
107
+
108
+ if event .type == pygame .KEYUP :
109
+ if event .key == pygame .K_LEFT :
110
+ self .to_left = False
111
+ if event .key == pygame .K_RIGHT :
112
+ self .to_right = False
113
+
114
+ if event .type == pygame .QUIT :
115
+ exit ()
116
+
117
+ def fall (self ):
118
+ for i in range (self .number ):
119
+
120
+ if self .position_coin [i ][1 ] >= self .height :
121
+ self .position_coin [i ][0 ] = randint (0 , self .width - self .images ["coin" ].get_width ())
122
+ self .position_coin [i ][1 ] = - randint (100 , 3000 )
123
+
124
+ if self .position_monster [i ][1 ] >= self .height :
125
+ self .position_monster [i ][0 ] = randint (0 , self .width - self .images ["monster" ].get_width ())
126
+ self .position_monster [i ][1 ] = - randint (100 , 3000 )
127
+
128
+ # Checking points i.e, robot contacts with coin
129
+
130
+ if self .position_coin [i ][1 ] + self .images ["coin" ].get_height () in range (self .y , self .y + self .images ["robot" ].get_height ()):
131
+ if self .position_coin [i ][0 ] in range (self .x , self .x + self .images ["robot" ].get_width ()) or (self .position_coin [i ][0 ] + self .images ["coin" ].get_width ()) in range (self .x , self .x + self .images ["robot" ].get_width ()):
132
+ self .position_coin [i ][0 ] = randint (0 , self .width - self .images ["coin" ].get_width ())
133
+ self .position_coin [i ][1 ] = - randint (100 , 3000 )
134
+
135
+ self .points += 1
136
+
137
+ # checking if robot touches monster
138
+
139
+ if (self .position_monster [i ][1 ] + self .images ["monster" ].get_height ()) in range (self .y , self .y + self .images ["robot" ].get_height ()):
140
+ if self .position_monster [i ][0 ] in range (self .x , self .x + self .images ["robot" ].get_width ()) or (self .position_monster [i ][0 ] + self .images ["monster" ].get_width ()) in range (self .x , self .x + self .images ["robot" ].get_width ()):
141
+ self .position_monster [i ][0 ] = randint (0 , self .width - self .images ["monster" ].get_width ())
142
+ self .position_monster [i ][1 ] = - randint (100 , 3000 )
143
+
144
+ self .end = True
145
+
146
+ self .position_coin [i ][1 ] += 3
147
+ self .position_monster [i ][1 ] += 3
148
+
149
+
150
+
151
+ def robot_move (self ):
152
+
153
+ if self .to_left and self .x > 0 :
154
+ self .x -= 4
155
+ if self .to_right and self .width - self .images ["robot" ].get_width () > self .x :
156
+ self .x += 4
157
+
158
+ def door (self ):
159
+ self .d_x = randint (0 , self .width - self .images ["robot" ].get_width ())
160
+ self .y_x = self .height - self .images ["robot" ].get_height ()
161
+
162
+
163
+ def draw_window (self ):
164
+ self .window .fill ((235 , 227 , 213 ))
165
+
166
+ if self .start :
167
+ x_start = self .width / 2
168
+
169
+ s_font = pygame .font .SysFont ("Arial bold" , 24 )
170
+ start_text = s_font .render ("Use LEFT, RIGHT arrow keys to move and SPACE for telport to door" , True , (117 , 106 , 182 ))
171
+ choose_level = s_font .render ("choose Level: Press(1 for easy, 2 for normal, 3 for hard)" , True , (85 , 173 , 155 ))
172
+ pygame .draw .rect (self .window , "white" , (50 , 200 , 540 , 100 ))
173
+ self .window .blit (start_text , (x_start - start_text .get_width ()/ 2 ,220 ))
174
+ self .window .blit (choose_level , (x_start - choose_level .get_width ()/ 2 ,260 ))
175
+
176
+
177
+ elif self .end :
178
+ end_font = pygame .font .SysFont ("Arial" , 25 )
179
+ end_text = end_font .render (f"Game Over! Collected Coins: { self .points } " , True , "Red" )
180
+ end_info = end_font .render (f"Press: Return for restart and Escape for exit" , True , "black" )
181
+ pygame .draw .rect (self .window , "white" , (50 , 200 , 540 , 100 ))
182
+ self .window .blit (end_text , (self .width / 2 - end_text .get_width ()/ 2 ,220 ))
183
+ self .window .blit (end_info , (self .width / 2 - end_info .get_width ()/ 2 ,260 ))
184
+
185
+ else :
186
+ game_text = self .game_font .render (f"Coins: { self .points } " , True , "red" )
187
+ self .window .blit (game_text , (510 , 0 ))
188
+
189
+ for i in range (self .number ):
190
+ self .window .blit (self .images ["coin" ], (self .position_coin [i ][0 ], self .position_coin [i ][1 ]))
191
+ self .window .blit (self .images ["monster" ], (self .position_monster [i ][0 ], self .position_monster [i ][1 ]))
192
+
193
+
194
+
195
+ self .window .blit (self .images ["robot" ], (self .x , self .y ))
196
+ self .window .blit (self .images ["door" ], (self .d_x , self .y_x ))
197
+
198
+ pygame .display .flip ()
199
+ self .clock .tick (60 )
200
+
201
+ if __name__ == "__main__" :
202
+ Coinport ()
0 commit comments