-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
147 lines (113 loc) · 3.56 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
144
145
146
147
import pygame
import random
import math
# Intialize the pygame (Without this pygame can't work)
pygame.init()
# Create a screen
screen = pygame.display.set_mode((800, 600))
# Background
background = pygame.image.load("Images/Spaceship_background_800x600.jpg")
# Title and Icon
pygame.display.set_caption("Space Invaders")
icon = pygame.image.load('Images/ufo.png')
pygame.display.set_icon(icon)
# Player
playerImg = pygame.image.load('Images/space-invaders.png')
playerX = 370
playerY = 480
playerX_change = 0
def player(x, y):
screen.blit(playerImg, (x, y))
# Bullet
# Ready - You can't see the bullet on the screen
# Fire - The bullet is currently moving
bulletImg = pygame.image.load('Images/bullet.png')
bulletX = 0
bulletY = 480
bulletX_change = 0
bulletY_change = 0.5
bullet_state = "ready"
def fire_bullet(x, y):
global bullet_state
bullet_state = "fire"
screen.blit(bulletImg, (x + 16, y + 10))
# Enemy
enemyImg = pygame.image.load('Images/alien.png')
enemyX = random.randint(0, 736)
# Makes the enemy spawn randomly - First parameter is starting value and second is ending
# Remember to import 'random'
enemyY = random.randint(50, 150)
enemyX_change = 0.2
enemyY_change = 40
def enemy(x, y):
screen.blit(enemyImg, (x, y))
# Collision
def isCollison(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
# Player score
score = 0
# Game loop (so the game can stay on and not end straight after it starts)
running = True
while running:
# RGB - Red, Green, Blue - Changing background colour
screen.fill((0, 0, 0))
# Background Image
screen.blit(background, (0, 0))
# A event is anything that happens in the loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# If keystroke is pressed check whether it's right or left
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -0.2
if event.key == pygame.K_RIGHT:
playerX_change = 0.2
if event.key == pygame.K_SPACE:
if bullet_state == "ready":
bulletX = playerX
fire_bullet(bulletX, bulletY)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
# Adds player control
playerX += playerX_change
# Adds player boundaries
if playerX <= 0:
playerX = 0
elif playerX >= 736:
playerX = 736
# Adds enemy control
enemyX += enemyX_change
# Adds enemy movement
if enemyX <= 0:
enemyX_change = 0.2
enemyY += enemyY_change
elif enemyX >= 736:
enemyX_change = -0.2
enemyY += enemyY_change
# Adds Bullet Movement
if bulletY <= 0:
bulletY = 480
bullet_state = "ready"
if bullet_state == "fire":
fire_bullet(bulletX, bulletY)
bulletY -= bulletY_change
# Collision
collision = isCollison(enemyX, enemyY, bulletX, bulletY)
if collision:
bulletY = 480
bullet_state = "ready"
score += 1
print(score)
enemyX = random.randint(0, 736)
enemyY = random.randint(50, 150)
# Adding player image & enemy
player(playerX, playerY)
enemy(enemyX, enemyY)
# Everything about the loop should be before this line because this updates the game screen/window
pygame.display.update()