2015-04-22 09:51:44 +01:00
|
|
|
import pygame, logging
|
2015-05-07 19:58:43 +01:00
|
|
|
from time import time
|
|
|
|
from random import randint
|
2015-04-22 09:51:44 +01:00
|
|
|
import bullet, player, textures
|
2015-04-28 12:19:59 +01:00
|
|
|
from bullet import Bullet
|
|
|
|
from player import Shooter
|
2015-05-08 10:19:43 +01:00
|
|
|
from assets import Textures, Levels, generate_random_level
|
2015-04-28 12:19:59 +01:00
|
|
|
from target import Target
|
|
|
|
|
2015-05-07 20:04:43 +01:00
|
|
|
|
2015-04-28 12:19:59 +01:00
|
|
|
PLAYING_GAME = False
|
|
|
|
WINDOW_SIZE = (640,480)
|
2015-04-29 09:48:26 +01:00
|
|
|
FPS = 120
|
2015-04-28 12:19:59 +01:00
|
|
|
|
2015-04-22 09:51:44 +01:00
|
|
|
|
2015-05-06 09:51:26 +01:00
|
|
|
def update_score(window, score):
|
|
|
|
font = pygame.font.SysFont(None, 30, bold=False)
|
2015-05-07 20:04:43 +01:00
|
|
|
window.blit(font.render("Score: {}".format(int(score)), True, (255,0,0)), (25, WINDOW_SIZE[1] - 30))
|
|
|
|
|
|
|
|
def update_level(window, level):
|
|
|
|
font = pygame.font.SysFont(None, 20, 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))
|
|
|
|
|
|
|
|
def update_lives(window, lives):
|
|
|
|
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))
|
2015-04-22 09:51:44 +01:00
|
|
|
|
|
|
|
def initialise(menu, options):
|
2015-04-28 12:19:59 +01:00
|
|
|
pygame.mixer.pre_init(44100, -16, 2, 2048)
|
|
|
|
pygame.init()
|
|
|
|
window = pygame.display.set_mode(WINDOW_SIZE)
|
|
|
|
|
|
|
|
exit_code = play(window) # Run main game loop
|
2015-05-06 09:51:26 +01:00
|
|
|
|
|
|
|
if exit_code == "QUIT":
|
|
|
|
pygame.quit()
|
|
|
|
|
|
|
|
menu.deiconify()
|
|
|
|
return exit_code
|
2015-04-28 12:19:59 +01:00
|
|
|
|
|
|
|
|
2015-05-07 20:08:47 +01:00
|
|
|
def generate_targets(player):
|
2015-05-06 09:51:26 +01:00
|
|
|
group = pygame.sprite.Group()
|
2015-05-08 10:19:43 +01:00
|
|
|
if player.level > len(Levels)-1:
|
|
|
|
level = generate_random_level()
|
|
|
|
else: level = Levels[player.level]
|
2015-05-07 20:08:47 +01:00
|
|
|
|
|
|
|
for i in range(level.rows):
|
|
|
|
i *= level.padding
|
|
|
|
for j in range(50, WINDOW_SIZE[0] - 70, level.padding):
|
2015-05-06 09:51:26 +01:00
|
|
|
temp = Target(x=j,y=i)
|
2015-05-07 20:08:47 +01:00
|
|
|
if randint(0, )
|
2015-05-06 09:51:26 +01:00
|
|
|
group.add(temp)
|
2015-05-07 20:08:47 +01:00
|
|
|
del temp
|
|
|
|
|
|
|
|
for i in range(level.firebacks):
|
|
|
|
changed = False
|
|
|
|
while not changed:
|
2015-05-07 22:12:16 +01:00
|
|
|
index = randint(0, len(group)-1)
|
|
|
|
if group[index].type != "SHOOTER"
|
|
|
|
group[index].type == "SHOOTER"
|
|
|
|
group[index].image.fill((150,0,30))
|
|
|
|
group[index].rect = group[image].image.get_rect()
|
2015-05-07 20:08:47 +01:00
|
|
|
changed = True
|
|
|
|
else:
|
2015-05-07 22:12:16 +01:00
|
|
|
group[randint(0, len(group)-1)].type == "NORMAL"
|
2015-05-06 09:51:26 +01:00
|
|
|
return group
|
|
|
|
|
2015-04-28 12:19:59 +01:00
|
|
|
|
|
|
|
def play(window):
|
2015-04-29 09:48:26 +01:00
|
|
|
window_rect = window.get_rect()
|
|
|
|
|
|
|
|
player = Shooter(window=window)
|
2015-05-06 09:51:26 +01:00
|
|
|
player.set_position(WINDOW_SIZE[0]/2, WINDOW_SIZE[1]*0.83)
|
2015-04-28 12:19:59 +01:00
|
|
|
player_group = pygame.sprite.Group()
|
|
|
|
player_group.add(player)
|
2015-04-29 09:48:26 +01:00
|
|
|
player_group.draw(window)
|
2015-04-28 12:19:59 +01:00
|
|
|
|
2015-05-07 20:17:22 +01:00
|
|
|
target_group = generate_targets(player)
|
2015-04-28 12:19:59 +01:00
|
|
|
bullet_group = pygame.sprite.Group()
|
|
|
|
|
|
|
|
clock = pygame.time.Clock()
|
|
|
|
PLAYING_GAME = True
|
2015-05-06 09:51:26 +01:00
|
|
|
target_movement_timeout_default = FPS * 0.5
|
|
|
|
target_movement_timeout = target_movement_timeout_default
|
2015-04-28 12:19:59 +01:00
|
|
|
|
|
|
|
while PLAYING_GAME:
|
|
|
|
window.fill((0,0,0))
|
2015-04-29 09:48:26 +01:00
|
|
|
player_group.update()
|
2015-04-28 12:19:59 +01:00
|
|
|
|
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == pygame.QUIT:
|
|
|
|
logging.critical("Exiting Game...")
|
|
|
|
PLAYING_GAME = False
|
|
|
|
return "QUIT"
|
|
|
|
|
|
|
|
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
|
|
|
|
temp = Bullet(player)
|
|
|
|
bullet_group.add(temp)
|
|
|
|
|
2015-05-07 20:10:47 +01:00
|
|
|
if event.type == pygame.KEYDOWN and event.key == pygame.K_KP_PLUS:
|
|
|
|
player.OP = not player.OP
|
2015-05-06 09:51:26 +01:00
|
|
|
|
2015-04-29 09:48:26 +01:00
|
|
|
keys = pygame.key.get_pressed()
|
|
|
|
if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
|
2015-05-07 22:13:02 +01:00
|
|
|
if player.powerup == "SPEED": player.move(player.speed * 1.5)
|
|
|
|
else: player.move(player.speed)
|
2015-04-29 09:48:26 +01:00
|
|
|
|
|
|
|
if keys[pygame.K_LEFT] or keys[pygame.K_a]:
|
2015-05-07 22:13:02 +01:00
|
|
|
if player.powerup == "SPEED": player.move(-player.speed*1.5)
|
|
|
|
else: player.move(-player.speed)
|
2015-04-29 09:48:26 +01:00
|
|
|
|
2015-05-06 09:51:26 +01:00
|
|
|
if keys[pygame.K_KP4] and keys[pygame.K_KP5] and keys[pygame.K_KP6] and player.OP:
|
2015-04-29 09:48:26 +01:00
|
|
|
temp = Bullet(player)
|
|
|
|
bullet_group.add(temp)
|
|
|
|
|
|
|
|
for sprite in bullet_group:
|
2015-05-07 20:18:55 +01:00
|
|
|
if not sprite.at_bound():
|
2015-04-29 09:48:26 +01:00
|
|
|
sprite.update()
|
2015-05-07 20:18:55 +01:00
|
|
|
if sprite.rect.y < 0 or sprite.rect.y > player.rect.y:
|
2015-04-29 09:48:26 +01:00
|
|
|
bullet_group.remove(sprite)
|
|
|
|
|
|
|
|
for bullet in bullet_group:
|
|
|
|
hit_list = pygame.sprite.spritecollide(bullet, target_group, True)
|
|
|
|
for target in hit_list:
|
2015-05-07 21:01:51 +01:00
|
|
|
if bullet.type == "TARGET": continue
|
2015-04-29 09:48:26 +01:00
|
|
|
target_group.remove(target)
|
|
|
|
bullet_group.remove(bullet)
|
|
|
|
logging.info("Hit Target!")
|
2015-05-06 09:51:26 +01:00
|
|
|
player.score += 1
|
|
|
|
|
2015-05-07 20:10:16 +01:00
|
|
|
hit_list = pygame.sprite.spritecollide(bullet, player_group, True)
|
|
|
|
for player in hit_list:
|
|
|
|
if bullet.type != "TARGET" continue
|
|
|
|
bullet_group.remove(bullet)
|
|
|
|
logging.info("")
|
|
|
|
player.lives -= 1
|
|
|
|
if player.lives <= 0:
|
|
|
|
return "LIVES"
|
|
|
|
|
2015-05-06 09:51:26 +01:00
|
|
|
if target_movement_timeout <=0:
|
|
|
|
drop_targets = False
|
|
|
|
for target in target_group:
|
|
|
|
target.move()
|
|
|
|
if target.rect.x <= 20 or target.rect.x >=WINDOW_SIZE[0] - 20 + target.width:
|
|
|
|
drop_targets = True
|
|
|
|
|
|
|
|
if target.rect.y >= player.rect.y + 10:
|
|
|
|
PLAYING_GAME = False
|
|
|
|
for target in target_group:
|
|
|
|
target.image.fill((255,0,0))
|
2015-05-07 20:16:18 +01:00
|
|
|
return "PLAYER COLLISION"
|
2015-05-06 09:51:26 +01:00
|
|
|
|
|
|
|
if drop_targets:
|
2015-05-07 20:16:18 +01:00
|
|
|
logging.debug("The targets are moving down a level!")
|
2015-05-06 09:51:26 +01:00
|
|
|
for target in target_group:
|
|
|
|
target.drop()
|
|
|
|
|
|
|
|
target_movement_timeout = target_movement_timeout_default
|
|
|
|
|
2015-05-07 20:06:10 +01:00
|
|
|
for target in target_group:
|
|
|
|
if target.type == "SHOOTING":
|
2015-05-08 10:20:00 +01:00
|
|
|
if randint(0,100) > 1: continue
|
2015-05-07 20:06:10 +01:00
|
|
|
temp = Bullet(target)
|
|
|
|
temp.speed = -3 #So it shoots down!
|
|
|
|
bullet_group.add(temp)
|
2015-05-06 09:51:26 +01:00
|
|
|
|
|
|
|
update_score(window, player.score)
|
2015-05-07 20:17:02 +01:00
|
|
|
update_level(window, player.level)
|
2015-05-07 22:12:48 +01:00
|
|
|
update_lives(window, player.lives)
|
2015-05-07 21:01:51 +01:00
|
|
|
|
2015-04-29 09:48:26 +01:00
|
|
|
player_group.update()
|
|
|
|
bullet_group.draw(window)
|
|
|
|
target_group.draw(window)
|
|
|
|
player_group.draw(window)
|
|
|
|
pygame.display.update()
|
2015-05-07 21:01:51 +01:00
|
|
|
|
2015-05-06 09:51:26 +01:00
|
|
|
target_movement_timeout -= 1
|
2015-04-29 09:48:26 +01:00
|
|
|
clock.tick(FPS)
|
2015-04-28 12:19:59 +01:00
|
|
|
|
2015-05-07 21:01:51 +01:00
|
|
|
|
2015-04-28 12:19:59 +01:00
|
|
|
if __name__ == "__main__":
|
2015-05-06 09:51:26 +01:00
|
|
|
logging.basicConfig(format="%(levelname)s:%(message)s", level=logging.DEBUG)
|
2015-04-28 12:19:59 +01:00
|
|
|
initialise(None, None)
|