-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
134 lines (103 loc) · 3.36 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
import math
import random
import pygame
pygame.init()
display_width = 800
display_height = 600
display = pygame.display.set_mode((display_width, display_height))
# игрок
playerImg = pygame.image.load("spaceship.png")
playerX = 340
playerY = 470
playerX_change = 0
# пули
bulletImg = pygame.image.load('bullet.png')
bulletX = 0
bulletY = 480
bulletX_change = 0
bulletY_change = 10
enemyImg = []
enemyX = []
enemyY = []
enemyX_change = []
enemyY_change = []
num_of_enemies = 10
# счет игрока
global score_value
score_value = 0
font = pygame.font.Font('freesansbold.ttf', 25)
textX = 15
textY = 15
for i in range(num_of_enemies):
enemyImg.append(pygame.image.load("ufo.png"))
enemyX.append(random.randint(0, 736))
enemyY.append(random.randint(50, 150))
enemyX_change.append(0.05)
enemyY_change.append(20)
def show_score(x, y):
score = font.render("Счет: " + str(score_value), True, (255, 255, 255))
display.blit(score, (x, y))
def fire_bullet(x, y):
display.blit(bulletImg, (x + 16, y + 10))
def player(x, y):
display.blit(playerImg, (x, y))
def enemy(x, y, i):
display.blit(enemyImg[i], (x, y))
def isCollision(enemyX, enemyY, bulletX, bulletY):
distance = math.sqrt(math.pow(enemyX - bulletX, 2) + (math.pow(enemyY - bulletY, 2)))
if distance < 27:
return True
else:
return False
def run_game(playerX, playerY, playerX_change, bulletX, bulletY):
global score_value
game = True
while game:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -0.8
if event.key == pygame.K_RIGHT:
playerX_change = 0.8
if event.key == pygame.K_SPACE:
bulletX = playerX
bulletY = playerY
fire_bullet(bulletX, bulletY)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
display.fill((5, 5, 10))
if playerX <= 0:
playerX = 0
elif playerX >= 736:
playerX = 736
for i in range(num_of_enemies):
if enemyY[i] > 440:
for j in range(num_of_enemies):
enemyY[j] = 2000
break
enemyX[i] += enemyX_change[i]
if enemyX[i] <= 0:
enemyX_change[i] = 0.5
enemyY[i] += enemyY_change[i]
elif enemyX[i] >= 736:
enemyX_change[i] = -0.5
enemyY[i] += enemyY_change[i]
collision = isCollision(enemyX[i], enemyY[i], bulletX, bulletY)
if collision:
score_value += 1
bulletY = 480
bullet_state = "ready"
enemyX[i] = random.randint(0, 736)
enemyY[i] = random.randint(50, 150)
enemy(enemyX[i], enemyY[i], i)
fire_bullet(bulletX, bulletY)
bulletY -= bulletY_change
playerX += playerX_change
player(playerX, playerY)
show_score(textX, textY)
pygame.display.update()
run_game(playerX, playerY, playerX_change, bulletX, bulletY)