-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
143 lines (120 loc) · 3.54 KB
/
main.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
import pygame
import random
import math
#Initial interface
pygame.init()
screen = pygame.display.set_mode((800,600)) #size
pygame.display.set_caption("Jeffery's plane game") #program name
icon = pygame.image.load('UFO.png') #icon image
pygame.display.set_icon(icon)
BackgroundImg = pygame.image.load('Background.png') #background image
#Score
Score = 0
score_font = pygame.font.Font('freesansbold.ttf',32)
def show_score():
score_text = f"Your score : {Score}"
score_render = score_font.render(score_text,True,(255,255,255))
screen.blit(score_render,(10,10))
#End
is_over = False
over_font = pygame.font.Font('freesansbold.ttf',64)
def check_is_over():
if is_over:
over_text = "Game Over"
over_render = over_font.render(over_text,True,(255,255,255))
screen.blit(over_render,(200,250))
#Plane
PlaneImg = pygame.image.load('Plane.png')
PlaneX = 400
PlaneY = 500
PlaneStep = 0 #plane's velocity
def move_plane():
global PlaneX
PlaneX += PlaneStep
#prevent plane get out of bounds
if PlaneX > 736:
PlaneX = 736
elif PlaneX < 0:
PlaneX = 0
def control_plane():
global running,PlaneStep
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#keyboard to control plane
if event.type == pygame.KEYDOWN: #press the keyboard to move
if event.key == pygame.K_RIGHT:
PlaneStep = 8
elif event.key == pygame.K_LEFT:
PlaneStep = -8
elif event.key == pygame.K_UP:
bullets.append(Bullet())
if event.type == pygame.KEYUP: #leave the keyboard to stop
PlaneStep = 0
#Enemy
number_of_enemy = 6
class Enemy():
def __init__(self):
self.img = pygame.image.load('Enemy.png')
self.x = random.randint(200,600)
self.y = random.randint(50,150)
self.step = random.randint(1,6)
def reset(self):
self.x = random.randint(200,600)
self.y = random.randint(50,150)
self.step = random.randint(1,6)
enemy = []
for i in range(number_of_enemy):
enemy.append(Enemy())
def show_enemy():
global is_over,Score
for e in enemy:
screen.blit(e.img,(e.x,e.y))
e.x += e.step
e.y += Score * 0.1
if e.x > 736 or e.x < 0:
e.step *= -1
if e.y > 400:
is_over = True
print("Game over")
enemy.clear()
#Distance
def distance(bx,by,ex,ey):
a = bx - ex
b = by - ey
return math.sqrt(a*a + b*b)
#Bullet
class Bullet():
def __init__(self):
self.img = pygame.image.load('Bullet.png')
self.x = PlaneX + 16 #middle of plane in x
self.y = PlaneY - 16 #head of plane in y
self.step = 10
#hit enemy
def check_hit(self):
global Score
for e in enemy:
if distance(self.x,self.y,e.x,e.y) < 30:
bullets.remove(self)
e.reset()
Score += 1
bullets = []
def show_bullet():
for b in bullets:
screen.blit(b.img,(b.x,b.y))
b.check_hit()
b.y -= b.step
if b.y < 0:
bullets.remove(b)
#Main loop
running = True
while running:
screen.blit(BackgroundImg,(0,0))
show_score()
screen.blit(PlaneImg,(PlaneX,PlaneY))
control_plane()
move_plane()
show_enemy()
show_bullet()
check_is_over()
pygame.display.update()