-
Notifications
You must be signed in to change notification settings - Fork 1
/
first_basic_game.py
190 lines (190 loc) · 6.53 KB
/
first_basic_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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import pygame
import random
# import basic keystroke inputs that you want to use:
from pygame.locals import (
K_UP,K_DOWN,K_LEFT,K_RIGHT,K_a,K_s,K_d,K_w,K_ESCAPE,K_SPACE,KEYDOWN,KEYUP,QUIT,RLEACCEL,
)
FPS = 60
if FPS == 60:
s = 35
e_s = 15
"""
elif FPS == 30:
s = 60
e_s = 20
"""
init_lives = 3
init_viral_load = 100
# define screen dimensions:
SCREEN_WIDTH = 1600
SCREEN_HEIGHT = 1000
# create counting variables:
hit_count = 0
fail_count = 0
kill_count = 0 # will use for levels initially.
"""CLASSES AND FUNCTIONS"""
# define the user clas (see: https://realpython.com/python-super/ for information on use of super() inheritance)
class User(pygame.sprite.Sprite):
def __init__(self):
super(User,self).__init__()
self.obj = pygame.image.load('Images/doctor copy.jpg')
self.obj = pygame.transform.scale(self.obj,(100,140)).convert()
self.obj.set_colorkey((254,254,254),RLEACCEL)
self.rect = self.obj.get_rect(center = (SCREEN_WIDTH/2,SCREEN_HEIGHT/2)) #starts at approximately center.
def update(self, pressed_keys):
# pressed_keys is a dict:
if pressed_keys[K_UP]:
self.rect.move_ip(0, -s)
if pressed_keys[K_DOWN]:
self.rect.move_ip(0, s)
if pressed_keys[K_LEFT]:
self.rect.move_ip(-s, 0)
if pressed_keys[K_RIGHT]:
self.rect.move_ip(s, 0)
if pressed_keys[K_w]:
self.rect.move_ip(0, -s)
if pressed_keys[K_s]:
self.rect.move_ip(0, s)
if pressed_keys[K_a]:
self.rect.move_ip(-s, 0)
if pressed_keys[K_d]:
self.rect.move_ip(s, 0)
if pressed_keys[K_SPACE]:
bul = Bullet()
all_spr.add(bul)
bullet_spr.add(bul)
# Keep player player on the screen by appearing out of opposite side...
if self.rect.left < 0:
self.rect.left = 0
if self.rect.right > SCREEN_WIDTH:
self.rect.right = SCREEN_WIDTH
if self.rect.top < 0:
self.rect.top = 0
if self.rect.bottom > SCREEN_HEIGHT:
self.rect.bottom = SCREEN_HEIGHT
# define the enemy class:
class Enemy(pygame.sprite.Sprite):
def __init__(self):
super(Enemy,self).__init__()
self.obj = pygame.image.load('Images/corona_blue.jpg').convert()
self.obj.set_colorkey((2,2,2), RLEACCEL)
self.obj = pygame.transform.scale(self.obj,(100,100))
self.rect = self.obj.get_rect(
center = (
SCREEN_WIDTH,
random.randint(50,SCREEN_HEIGHT)
)
)
self.speed = random.randint(e_s,2*e_s)
def update(self):
self.rect.move_ip(-self.speed,0)
if self.rect.right < 0:
self.kill()
return 1
# make a 'bullet' class:
class Bullet(pygame.sprite.Sprite):
def __init__(self):
super(Bullet,self).__init__()
self.obj = pygame.image.load('Images/raindrop-clipart-rainfall.jpg').convert()
self.obj.set_colorkey((255,255,255))
self.obj = pygame.transform.scale(self.obj,(20,20))
self.rect = self.obj.get_rect(
center = (
usr.rect.right-20,
usr.rect.top+16
)
)
self.speed = 40
def update(self):
self.rect.move_ip(self.speed,0)
if self.rect.left > SCREEN_WIDTH:
self.kill()
def lives(N,h_count):
n = N - h_count
lives_font = pygame.font.SysFont(None,25)
lives_txt = lives_font.render('Lives: '+ str(n),True,(0,0,0))
screen.blit(lives_txt,(1300,50))
return n
def v_load(N,f_count):
n = N - f_count
lives_font = pygame.font.SysFont(None, 25)
lives_txt = lives_font.render('Viral Load: ' + str(n), True, (0, 0, 0))
screen.blit(lives_txt, (100, 50))
return n
def level_bar(k_count):
l = k_count//50
level_font = pygame.font.SysFont(None,25)
level_txt = level_font.render('Level: ' + str(l),True, (0,0,0))
screen.blit(level_txt,(800,50))
"""GAME VARIABLES AND INITIALISE"""
# create a clock to manage FPS:
clock = pygame.time.Clock()
# have to initialise pygame to get the library running:
pygame.init()
# create the screen:
screen = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT),pygame.RESIZABLE)
# initiate the user:
usr = User()
# create event for adding enemies:
ADDENEMY = pygame.USEREVENT + 1
pygame.time.set_timer(ADDENEMY,250)
# create two Sprite groups: one for all sprites, one for just the enemies.
all_spr = pygame.sprite.Group()
enemy_spr = pygame.sprite.Group()
bullet_spr = pygame.sprite.Group()
all_spr.add(usr)
# get the game loop running and create the code to enable the user to quit the game. In this case the 'esc' key.
pygame.display.flip()
"""GAME LOOP"""
running = True
while running == True:
"""game variables"""
# loop through the elements in the event queue:
for event in pygame.event.get():
# did the user press a key? act on this first...
if event.type == KEYDOWN:
# create if statements for esc key press:
if event.key == K_ESCAPE:
running = False
elif event.type == QUIT: # this is closing the window.
running = False
elif event.type == ADDENEMY:# check to see if any new enemies must be added.
enemy = Enemy()
enemy_spr.add(enemy)
all_spr.add(enemy)
screen.fill((255,255,255))
number_lives = lives(init_lives,hit_count)
if number_lives <= 0:
running = False
viral_load = v_load(init_viral_load,fail_count)
if viral_load <= 0:
running = False
"""levels"""
current_level = level_bar(kill_count)
"""usr variables"""
#update user movement:
pressed_keys = pygame.key.get_pressed()
usr.update(pressed_keys)
# update enemy movements:
for sprite in enemy_spr:
s_up = sprite.update()
if s_up == 1:
fail_count += 1
# update bullet movements:
for sprite in bullet_spr:
sprite.update()
# update the display with all sprite movements:
for sprite in all_spr:
screen.blit(sprite.obj,sprite.rect)
pygame.display.flip()
# check collisions (always after screen update)
killed_enemies = pygame.sprite.groupcollide(enemy_spr,bullet_spr,dokilla=True,dokillb=True) #returns dict where enemy sprites are keys.
kill_count += len(killed_enemies)
collided_enemy = pygame.sprite.spritecollideany(usr,enemy_spr)
if collided_enemy != None:
collided_enemy.kill()
hit_count += 1
clock.tick(FPS)
# quit pygame and the python application:
pygame.quit()
quit()