forked from 2021HYUopensource-2/Space-Rocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
153 lines (140 loc) · 4.62 KB
/
game.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
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
#importing modules
import pygame
import sys
import random
import time
from pygame import mixer
from pygame.locals import *
#initializing pygame
pygame.init()
#resolution of the screen
WIDTH=800
HEIGHT=600
screen=pygame.display.set_mode((WIDTH,HEIGHT))
#caption and icon
pygame.display.set_caption("Space Rocket")
icon=pygame.image.load("Images\\icon.png")
pygame.display.set_icon(icon)
#player
player_image=pygame.image.load("Images\\rocket.png")
player_size=30
player_pos=[int(WIDTH/2),int(HEIGHT-3*player_size)]
#enemy
enemy_image=pygame.image.load("Images\\asteroid.png")
enemy_size = 50
enemy_pos = [random.randint(0,WIDTH-enemy_size),random.randint(0,HEIGHT)]
enemy_list=[enemy_pos]
enemy_speed=0
#background image
background = pygame.image.load("Images\\background.png")
#background sound
mixer.music.load("Sounds\\background.ogg")
mixer.music.play(-1)
#basic
score=0
game_over=False
clock=pygame.time.Clock()
#fonts
myFont=pygame.font.SysFont("calibri",35, bold=True, italic=True)
game_over_font=pygame.font.Font("Fonts\\font.otf",60,italic=True)
def speed(score, enemy_speed):
if score<10:
enemy_speed=5
elif score<20:
enemy_speed=6
elif score<30:
enemy_speed=9
elif score<50:
enemy_speed=13
elif score<65:
enemy_speed=16
else:
enemy_speed=20
return enemy_speed
def drop_enemies(enemy_list):
delay = random.random()
if len(enemy_list)<8 and delay<0.06:
x_pos=random.randint(0,WIDTH-enemy_size)
y_pos=0
enemy_list.append([x_pos,y_pos])
def draw_enemies(enemy_list):
for enemy_pos in enemy_list:
screen.blit(enemy_image,(enemy_pos[0],enemy_pos[1]))
def new_enemy_pos(enemy_list, score):
for idx, enemy_pos in enumerate(enemy_list):
if enemy_pos[1]>=0 and enemy_pos[1]<HEIGHT:
enemy_pos[1]+=enemy_speed
else:
enemy_list.pop(idx)
score+=1
return score
def multiple_collision(enemy_list, player_pos):
for enemy_pos in enemy_list:
if collision(player_pos, enemy_pos):
return True
return False
def collision(player_pos, enemy_pos):
p_x=player_pos[0]
p_y=player_pos[1]
e_x=enemy_pos[0]
e_y=enemy_pos[1]
if (e_x>=p_x and e_x<(p_x+player_size)) or (p_x>=e_x and p_x<(e_x+enemy_size)):
if(e_y>=p_y and e_y<(p_y+player_size)) or (p_y>=e_y and p_y<(e_y+enemy_size)):
explosion=mixer.Sound("Sounds\\explosion.wav")
mixer.music.stop()
explosion.play()
return True
return False
def gameover(game_over, score):
screen.blit(background,(0,0))
text=" Game Over !"
label=game_over_font.render(text, 1, (255, 153, 102))
screen.blit(label,(int(WIDTH/2)-150,int(HEIGHT/2)-30))
text="Your Score: "+str(score)
label=game_over_font.render(text, 1, (255, 153, 102))
screen.blit(label,(int(WIDTH/2)-150,int(HEIGHT/2)+20))
running = True
while(running == True):
if game_over==False:
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
if event.type==pygame.KEYDOWN:
x=player_pos[0]
y=player_pos[1]
if event.key==pygame.K_LEFT:
if (x>0):
x-=player_size+10
elif event.key==pygame.K_RIGHT:
if(x<700):
x+=player_size+10
elif event.key==pygame.K_UP:
if(y>30):
y-=player_size+10
elif event.key==pygame.K_DOWN:
if(y<500):
y+=player_size+10
player_pos=[x,y]
screen.blit(background,(0,0))
game_over=collision(player_pos,enemy_pos)
drop_enemies(enemy_list)
score=new_enemy_pos(enemy_list, score)
enemy_speed=speed(score, enemy_speed)
text="Your Score: "+str(score)
label=myFont.render(text, 1, (204, 255, 51))
screen.blit(label,(int(WIDTH-250),0))
game_over=multiple_collision(enemy_list,player_pos)
draw_enemies(enemy_list)
screen.blit(player_image,(player_pos[0],player_pos[1]))
clock.tick(30)
pygame.display.update()
elif game_over==True:
gameover(game_over, score)
pygame.display.update()
for event in pygame.event.get():
if event.type==pygame.QUIT:
running = False
pygame.display.quit()
pygame.quit()
sys.exit()
pygame.display.update()