-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflying3.py
More file actions
168 lines (113 loc) · 8.73 KB
/
Copy pathflying3.py
File metadata and controls
168 lines (113 loc) · 8.73 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import pygame # import modules
import random
from pygame.locals import *
screen_width = 800 # constants for screen size
screen_height = 800
class Player(pygame.sprite.Sprite): # defines the Player class object that inherits the Sprite base info # inside the class parentheses (no parameters) but inheritance of a pre defined objects base info gets passed
def __init__(self): # Constructor - constructs the object this is the function(def) that creates this object
super(Player, self).__init__() # no we tell the game and constructor to inherit the information from pygame.sprite.Sprite info to create the Player object with the basic information from Sprite with the keyword SUPER
self.surface = pygame.image.load('assets/jet.png') # self here is the object we are creating as we are in the class that defines it, like you are a class and self refers to yourself wich is the class here
self.rect = self.surface.get_rect() # sets the rectangle box around the image
def update(self, pressed_keys): # move the sprite based on keyboard input ... parameters in the fucntion are self wich refers to the player class .. pressed_keys for the keyboard input
if pressed_keys[K_UP]: # checks wich key is pressed
self.rect.move_ip(0,-5) # object/self gets grabbed by its boundary rectangle and moved in place where (x parameter, y parameter)
if pressed_keys[K_DOWN]:
self.rect.move_ip(0, 5)
if pressed_keys[K_LEFT]:
self.rect.move_ip(-5, 0)
if pressed_keys[K_RIGHT]:
self.rect.move_ip(5, 0)
# keeps the objects on the screen
if self.rect.left < 0: # checks if the left boundrie of the rectangle around the object is less then 0 on the x axis
self.rect.left = 0
if self.rect.right > 800:
self.rect.right = 800
if self.rect.top < 0:
self.rect.top = 0
if self.rect.bottom > 800:
self.rect.bottom = 800
class Enemy(pygame.sprite.Sprite): # enemy object based on extending the pygame Sprite
def __init__(self): # we are creating our self here inside the class // based on the Sprite info and additional
super(Enemy, self).__init__()
self.surface = pygame.image.load('assets/missile.png')
self.rect = self.surface.get_rect(
center = (
random.randint(screen_width + 20, screen_width + 100), # x value give an x value so that the missile spawns offscreen so anything above 800 as that is the width of the screen
random.randint(10, screen_height - 10) # y value
)
)
self.speed = random.randint(5,20)
def update(self): # this function moves the missele and then removes them once they go offscreen or x < 0
self.rect.move_ip(-self.speed, 0) # update the missile so that it moves to the left
if self.rect.right < 0:
self.kill() # when the missile goes ofscreen remove the missile so we do need to track its position offsdcreen by the pc
class Clouds(pygame.sprite.Sprite):
def __init__(self):
super(Clouds, self).__init__()
self.surface = pygame.image.load('assets/cloud.png')
self.rect = self.surface.get_rect(
center = (
random.randint(screen_width + 50, screen_width + 150),
random.randint(20 , screen_height - 20)
)
)
self.speed = random.randint(3,9)
def update(self):
self.rect.move_ip(-self.speed, 0)
if self.rect.right < 0:
self.kill()
pygame.init() # intiialze all imported pygame modules
pygame.mixer.init() # inits the pygame mixer to add sound
clock = pygame.time.Clock() # set the clock for use with frame rate etc
screen = pygame.display.set_mode((screen_width, screen_height)) # create the screen object
# create custom event for adding a new enemy
Addcloud = pygame.USEREVENT + 2
pygame.time.set_timer(Addcloud, 1000)
Addenemy = pygame.USEREVENT + 1 # custom event for adding a new enemy every time we add an enemy we add a user event
pygame.time.set_timer(Addenemy, 250) # set how many missele are spawned, here its value used is in ms so 1000ms is one missile a second
player = Player() # creates the player aka it creates an object from the Player class and stores it in the player variable
enemies = pygame.sprite.Group() # creates the enemy object goup to uptdate all the enemies at once
clouds = pygame.sprite.Group()
all_sprites = pygame.sprite.Group() # create groups to hold our objects/sprites
all_sprites.add(player) # with the .add function we can add objects to our group
pygame.mixer.music.load('assets/Flight_Music.wav') #loading the music
pygame.mixer.music.play(loops= -1) # accessing our music file in the mixer and play it
collision_sound = pygame.mixer.Sound('assets/collision.wav') # load an asset
explosion = pygame.image.load('assets/explosion.png')
keeprunning = True # set a variable as boolean to keep the main loop alive
score = 0 # sets score variable to zero
# main loop
while keeprunning: # while keeprunning == True
for event in pygame.event.get(): # check every event in the game
if event.type == QUIT: # if the close window x is clicked call the quit function
pygame.quit()
elif event.type == Addenemy: # check for the event type Addenemy
new_enemy = Enemy() # using the Enemy class , create a new sprite and store it in new_enemy, then add your new created enemy in the all_sprites group
enemies.add(new_enemy)
all_sprites.add(new_enemy)
elif event.type == Addcloud:
new_cloud = Clouds()
clouds.add(new_cloud)
all_sprites.add(new_cloud)
# get the set of keys pressed and check for user input and move the jet
pressed_keys = pygame.key.get_pressed()
player.update(pressed_keys)
# update the enemy position
enemies.update() # handles all the enemies updates all at once
clouds.update()
# fill the screen
screen.fill((136,205,249)) # 2 parentheses / 1 for parameter of color and 1 for the holding opthe color fill the screen with a blue collor according to rgb standard this also eliminated the previously drawn objects by coloring them
if pygame.sprite.spritecollideany(player, enemies): # check for the enemy being collided with the player,, with .sprite you acces al the sprite properties in pygame
if player.alive:
collision_sound.play() # play
screen.blit(explosion, (player.rect) )
player.kill()
for entity in all_sprites: # runs for every object/sprite in the all_sprites variable
screen.blit(entity.surface, entity.rect) # with the .blit() function builtin function of pygame we draw each sprite/object in the all_sprites variable
if player.alive(): #update player score
score += 1
font = pygame.font.SysFont(None, 25) #draw the score
text = font.render('Score : ' + str(score), True , [0,0,0])
screen.blit(text, (0,0))
pygame.display.flip() # updates screen
clock.tick(30) # sets the clock at 30 fps so the update runs 1/30 sec