tabs vs spaces, and updated logic to make it work (from last nights editing)
This commit is contained in:
parent
e34503d2bc
commit
03de948eb4
1 changed files with 155 additions and 152 deletions
303
game.py
303
game.py
|
@ -14,196 +14,199 @@ FPS = 120
|
||||||
|
|
||||||
|
|
||||||
def update_score(window, score):
|
def update_score(window, score):
|
||||||
font = pygame.font.SysFont(None, 30, bold=False)
|
font = pygame.font.SysFont(None, 30, bold=False)
|
||||||
window.blit(font.render("Score: {}".format(int(score)), True, (255,0,0)), (25, WINDOW_SIZE[1] - 30))
|
window.blit(font.render("Score: {}".format(int(score)), True, (255,0,0)), (25, WINDOW_SIZE[1] - 30))
|
||||||
|
|
||||||
def update_level(window, level):
|
def update_level(window, level):
|
||||||
font = pygame.font.SysFont(None, 25, bold=True)
|
font = pygame.font.SysFont(None, 25, bold=True)
|
||||||
window.blit(font.render("Level: {}".format(int(level)+1), True, (0,255,0)), (8.7*WINDOW_SIZE[0]/10, WINDOW_SIZE[1] - 30))
|
window.blit(font.render("Level: {}".format(int(level)+1), True, (0,255,0)), (8.7*WINDOW_SIZE[0]/10, WINDOW_SIZE[1] - 30))
|
||||||
|
|
||||||
def update_lives(window, lives):
|
def update_lives(window, lives):
|
||||||
font = pygame.font.SysFont(None, 30, bold=False)
|
font = pygame.font.SysFont(None, 30, bold=False)
|
||||||
window.blit(font.render("Lives Remaining: {}".format(int(lives)), True, (0,0,255)), (4.5*WINDOW_SIZE[0]/10, WINDOW_SIZE[1] - 30))
|
window.blit(font.render("Lives Remaining: {}".format(int(lives)), True, (0,0,255)), (4.5*WINDOW_SIZE[0]/10, WINDOW_SIZE[1] - 30))
|
||||||
|
|
||||||
def initialise(menu, options):
|
def initialise(menu, options):
|
||||||
pygame.mixer.pre_init(44100, -16, 2, 2048)
|
pygame.mixer.pre_init(44100, -16, 2, 2048)
|
||||||
pygame.init()
|
pygame.init()
|
||||||
init_sounds()
|
init_sounds()
|
||||||
options["Difficulty"] = FPS
|
options["Difficulty"] = FPS
|
||||||
window = pygame.display.set_mode(WINDOW_SIZE)
|
window = pygame.display.set_mode(WINDOW_SIZE)
|
||||||
|
|
||||||
exit_code = play(window, options) # Run main game loop
|
exit_code = play(window, options) # Run main game loop
|
||||||
for key, value in Sounds.items():
|
for key, value in Sounds.items():
|
||||||
value.stop()
|
value.stop()
|
||||||
|
|
||||||
logging.debug("Game Exited with code {}".format(exit_code))
|
logging.debug("Game Exited with code {}".format(exit_code))
|
||||||
if exit_code != "QUIT": sleep(1)
|
if exit_code != "QUIT": sleep(1)
|
||||||
|
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
menu.deiconify()
|
menu.deiconify()
|
||||||
return exit_code
|
return exit_code
|
||||||
|
|
||||||
|
|
||||||
def generate_targets(player):
|
def generate_targets(player):
|
||||||
sprite_list = []
|
sprite_list = []
|
||||||
group = pygame.sprite.Group()
|
group = pygame.sprite.Group()
|
||||||
if player.level > len(Levels)-1:
|
if player.level > len(Levels)-1:
|
||||||
level = generate_random_level()
|
level = generate_random_level()
|
||||||
else: level = Levels[player.level]
|
else: level = Levels[player.level]
|
||||||
logging.debug("Generating Level: " + str(level))
|
logging.debug("Generating Level: " + str(level))
|
||||||
|
|
||||||
for i in range(level.rows):
|
for i in range(level.rows):
|
||||||
i *= level.padding + 8
|
i *= level.padding + 8
|
||||||
for j in range(40, WINDOW_SIZE[0] - 40, level.padding + 8):
|
for j in range(40, WINDOW_SIZE[0] - 40, level.padding + 8):
|
||||||
temp = Target(x=j,y=i, player.options["Textures"])
|
temp = Target(x=j,y=i, textures=player.options["Textures"])
|
||||||
sprite_list.append(temp)
|
sprite_list.append(temp)
|
||||||
del temp
|
del temp
|
||||||
|
|
||||||
for i in range(level.firebacks):
|
for i in range(level.firebacks):
|
||||||
changed = False
|
changed = False
|
||||||
while not changed:
|
while not changed:
|
||||||
index = randint(0, len(sprite_list)-1)
|
index = randint(0, len(sprite_list)-1) if len(sprite_list) - 1 != 0 else 0
|
||||||
if sprite_list[index].type != "SHOOTER":
|
if sprite_list[index].type != "SHOOTER":
|
||||||
sprite_list[index].type = "SHOOTER"
|
sprite_list[index].type = "SHOOTER"
|
||||||
sprite_list[index].image = pygame.transform.scale(player.options["Textures"].get_texture("SHOOTER"), (sprite_list[index].width, sprite_list[index].height))
|
sprite_list[index].image = pygame.transform.scale(player.options["Textures"].get_texture("SHOOTER"), (sprite_list[index].width, sprite_list[index].height))
|
||||||
x,y = sprite_list[index].rect.x, sprite_list[index].rect.y
|
x,y = sprite_list[index].rect.x, sprite_list[index].rect.y
|
||||||
sprite_list[index].rect = sprite_list[index].image.get_rect()
|
sprite_list[index].rect = sprite_list[index].image.get_rect()
|
||||||
sprite_list[index].set_position(x,y, center=False) #Already Centered!
|
sprite_list[index].set_position(x,y, center=False) #Already Centered!
|
||||||
changed = True
|
changed = True
|
||||||
|
|
||||||
for sprite in sprite_list: #Because sprite groups dont support indexing!
|
for sprite in sprite_list: #Because sprite groups dont support indexing!
|
||||||
group.add(sprite)
|
group.add(sprite)
|
||||||
return group
|
return group
|
||||||
|
|
||||||
|
|
||||||
def play(window, options):
|
def play(window, options):
|
||||||
window_rect = window.get_rect()
|
window_rect = window.get_rect()
|
||||||
|
options["Textures"].load_texture_pack(options["Textures"].pack)
|
||||||
|
player = Shooter(window=window, texture=options["Textures"])
|
||||||
|
player.set_position(WINDOW_SIZE[0]/2, WINDOW_SIZE[1]*0.83)
|
||||||
|
player.options = options
|
||||||
|
player_group = pygame.sprite.Group()
|
||||||
|
player_group.add(player)
|
||||||
|
|
||||||
player = Shooter(window=window)
|
target_group = generate_targets(player)
|
||||||
player.set_position(WINDOW_SIZE[0]/2, WINDOW_SIZE[1]*0.83)
|
bullet_group = pygame.sprite.Group()
|
||||||
player.options = options
|
|
||||||
player_group = pygame.sprite.Group()
|
|
||||||
player_group.add(player)
|
|
||||||
|
|
||||||
target_group = generate_targets(player)
|
clock = pygame.time.Clock()
|
||||||
bullet_group = pygame.sprite.Group()
|
|
||||||
|
|
||||||
clock = pygame.time.Clock()
|
timeouts = {
|
||||||
|
"Target Movement":[FPS*0.5,FPS*0.5],
|
||||||
|
"Powerup":[FPS*100, FPS*100]
|
||||||
|
}
|
||||||
|
init_sounds()
|
||||||
|
Sounds["main"].play(loops=-1) #Start background music
|
||||||
|
|
||||||
timeouts = {
|
logging.info("Game Started.")
|
||||||
"Target Movement":[FPS*0.5,FPS*0.5],
|
PLAYING_GAME = True
|
||||||
"Powerup":[FPS*100, FPS*100]
|
while PLAYING_GAME:
|
||||||
}
|
window.fill((0,0,0))
|
||||||
Sounds["main"].play(loops=-1) #Start background music
|
|
||||||
|
|
||||||
logging.info("Game Started.")
|
for event in pygame.event.get():
|
||||||
PLAYING_GAME = True
|
if event.type == pygame.QUIT:
|
||||||
while PLAYING_GAME:
|
logging.critical("Exiting Game...")
|
||||||
window.fill((0,0,0))
|
PLAYING_GAME = False
|
||||||
|
return "QUIT"
|
||||||
|
|
||||||
for event in pygame.event.get():
|
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
|
||||||
if event.type == pygame.QUIT:
|
temp = Bullet(player, player.options["Textures"])
|
||||||
logging.critical("Exiting Game...")
|
Sounds["shot"].play()
|
||||||
PLAYING_GAME = False
|
bullet_group.add(temp)
|
||||||
return "QUIT"
|
|
||||||
|
|
||||||
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
|
if event.type == pygame.KEYDOWN and event.key == pygame.K_KP_PLUS:
|
||||||
temp = Bullet(player)
|
player.OP = True
|
||||||
bullet_group.add(temp)
|
Sounds["main"].stop()
|
||||||
|
Sounds["OP"].play(loops=-1)
|
||||||
|
|
||||||
if event.type == pygame.KEYDOWN and event.key == pygame.K_KP_PLUS:
|
keys = pygame.key.get_pressed()
|
||||||
player.OP = not player.OP
|
if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
|
||||||
logging.info("Player is now OP!")
|
if player.powerup == "SPEED": player.move(player.speed * 1.5)
|
||||||
|
else: player.move(player.speed)
|
||||||
|
|
||||||
keys = pygame.key.get_pressed()
|
if keys[pygame.K_LEFT] or keys[pygame.K_a]:
|
||||||
if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
|
if player.powerup == "SPEED": player.move(-player.speed*1.5)
|
||||||
if player.powerup == "SPEED": player.move(player.speed * 1.5)
|
else: player.move(-player.speed)
|
||||||
else: player.move(player.speed)
|
|
||||||
|
|
||||||
if keys[pygame.K_LEFT] or keys[pygame.K_a]:
|
if keys[pygame.K_KP4] and keys[pygame.K_KP5] and keys[pygame.K_KP6] and player.OP:
|
||||||
if player.powerup == "SPEED": player.move(-player.speed*1.5)
|
temp = Bullet(player, player.options["Textures"])
|
||||||
else: player.move(-player.speed)
|
bullet_group.add(temp)
|
||||||
|
|
||||||
if keys[pygame.K_KP4] and keys[pygame.K_KP5] and keys[pygame.K_KP6] and player.OP:
|
if keys[pygame.K_r] and [pygame.K_9] and [pygame.K_k] and player.OP:
|
||||||
temp = Bullet(player)
|
eggs.r9k(window)
|
||||||
bullet_group.add(temp)
|
|
||||||
|
|
||||||
if keys[pygame.K_r] and [pygame.K_9] and [pygame.K_k] and player.OP:
|
for sprite in bullet_group:
|
||||||
eggs.r9k(window)
|
sprite.update()
|
||||||
|
if sprite.rect.y < 0 or (sprite.rect.y > player.rect.y and sprite.type == "TARGET"):
|
||||||
|
bullet_group.remove(sprite)
|
||||||
|
|
||||||
for sprite in bullet_group:
|
for bullet in bullet_group:
|
||||||
sprite.update()
|
hit_list = pygame.sprite.spritecollide(bullet, target_group, False)
|
||||||
if sprite.rect.y < 0 or (sprite.rect.y > player.rect.y and sprite.type == "TARGET"):
|
for target in hit_list:
|
||||||
bullet_group.remove(sprite)
|
if bullet.type != "TARGET":
|
||||||
|
target_group.remove(target)
|
||||||
|
bullet_group.remove(bullet)
|
||||||
|
player.score += 1
|
||||||
|
|
||||||
for bullet in bullet_group:
|
hit_list = pygame.sprite.spritecollide(bullet, player_group, False)
|
||||||
hit_list = pygame.sprite.spritecollide(bullet, target_group, False)
|
for player in hit_list:
|
||||||
for target in hit_list:
|
if bullet.type == "TARGET":
|
||||||
if bullet.type != "TARGET":
|
bullet_group.remove(bullet)
|
||||||
target_group.remove(target)
|
logging.info("You were hit by a target's bullet!")
|
||||||
bullet_group.remove(bullet)
|
if not player.OP: player.lives -= 1
|
||||||
player.score += 1
|
if player.lives <= 0:
|
||||||
|
return "LIVES"
|
||||||
|
|
||||||
hit_list = pygame.sprite.spritecollide(bullet, player_group, False)
|
if timeouts["Target Movement"][0] <=0:
|
||||||
for player in hit_list:
|
drop_targets = False
|
||||||
if bullet.type == "TARGET":
|
for target in target_group:
|
||||||
bullet_group.remove(bullet)
|
target.move()
|
||||||
logging.info("You were hit by a target's bullet!")
|
if target.rect.x <= 30 or target.rect.x >=WINDOW_SIZE[0] - 30:
|
||||||
player.lives -= 1
|
drop_targets = True
|
||||||
if player.lives <= 0:
|
|
||||||
return "LIVES"
|
|
||||||
|
|
||||||
if timeouts["Target Movement"][0] <=0:
|
if target.rect.y >= player.rect.y + 35:
|
||||||
drop_targets = False
|
PLAYING_GAME = False
|
||||||
for target in target_group:
|
return "PLAYER COLLISION"
|
||||||
target.move()
|
|
||||||
if target.rect.x <= 30 or target.rect.x >=WINDOW_SIZE[0] - 30:
|
|
||||||
drop_targets = True
|
|
||||||
|
|
||||||
if target.rect.y >= player.rect.y + 20:
|
if drop_targets:
|
||||||
PLAYING_GAME = False
|
logging.debug("The targets are moving down a level!")
|
||||||
return "PLAYER COLLISION"
|
for target in target_group:
|
||||||
|
target.drop()
|
||||||
|
|
||||||
if drop_targets:
|
timeouts["Target Movement"][0] = timeouts["Target Movement"][1]
|
||||||
logging.debug("The targets are moving down a level!")
|
|
||||||
for target in target_group:
|
|
||||||
target.drop()
|
|
||||||
|
|
||||||
timeouts["Target Movement"][0] = timeouts["Target Movement"][1]
|
for target in target_group:
|
||||||
|
if target.type == "SHOOTER":
|
||||||
|
if randint(0,375) > 1: continue
|
||||||
|
temp = Bullet(target, player.options["Textures"])
|
||||||
|
temp.type="TARGET"
|
||||||
|
temp.image = pygame.transform.scale(player.options["Textures"].get_texture("TARGET_BULLET"), (temp.width, temp.height))
|
||||||
|
x,y = temp.rect.x, temp.rect.y
|
||||||
|
temp.rect = temp.image.get_rect()
|
||||||
|
temp.set_position(x,y)
|
||||||
|
temp.speed = -3 #So it shoots down!
|
||||||
|
bullet_group.add(temp)
|
||||||
|
|
||||||
for target in target_group:
|
if len(target_group) == 0: #If all current players are gone.
|
||||||
if target.type == "SHOOTER":
|
player.level += 1
|
||||||
if randint(0,375) > 1: continue
|
target_group = generate_targets(player)
|
||||||
temp = Bullet(target)
|
target_group.draw(window)
|
||||||
temp.type="TARGET"
|
bullet_group.empty()
|
||||||
temp.image = pygame.transform.scale(textures.get_texture("TARGET_BULLET"), (temp.width, temp.height))
|
pygame.display.update()
|
||||||
x,y = temp.rect.x, temp.rect.y
|
sleep(1.5)
|
||||||
temp.rect = temp.image.get_rect()
|
|
||||||
temp.set_position(x,y)
|
|
||||||
temp.speed = -3 #So it shoots down!
|
|
||||||
bullet_group.add(temp)
|
|
||||||
|
|
||||||
if len(target_group) == 0: #If all current players are gone.
|
if player.OP:
|
||||||
player.level += 1
|
player.change_colour((255,96,0)) #override colour change if changed before
|
||||||
target_group = generate_targets(player)
|
|
||||||
player.change_colour(player.colours[player.level])
|
|
||||||
target_group.draw()
|
|
||||||
pygame.display.update()
|
|
||||||
sleep(1.5)
|
|
||||||
|
|
||||||
if player.OP:
|
update_score(window, player.score)
|
||||||
player.change_colour((255,96,0)) #override colour change if changed before
|
update_level(window, player.level)
|
||||||
|
update_lives(window, player.lives)
|
||||||
|
|
||||||
update_score(window, player.score)
|
bullet_group.draw(window)
|
||||||
update_level(window, player.level)
|
target_group.draw(window)
|
||||||
update_lives(window, player.lives)
|
player_group.draw(window)
|
||||||
|
|
||||||
bullet_group.draw(window)
|
for key, value in timeouts.items():
|
||||||
target_group.draw(window)
|
value[0] -= 1
|
||||||
player_group.draw(window)
|
|
||||||
|
|
||||||
for key, value in timeouts.items():
|
pygame.display.update()
|
||||||
value[0] -= 1
|
clock.tick(FPS)
|
||||||
|
|
||||||
pygame.display.update()
|
|
||||||
clock.tick(FPS)
|
|
Reference in a new issue