added target generation, fixed bugs
This commit is contained in:
parent
171a18b946
commit
a20a1d361d
1 changed files with 55 additions and 18 deletions
71
game.py
71
game.py
|
@ -6,13 +6,15 @@ from player import Shooter
|
||||||
from textures import Textures
|
from textures import Textures
|
||||||
from target import Target
|
from target import Target
|
||||||
|
|
||||||
|
|
||||||
PLAYING_GAME = False
|
PLAYING_GAME = False
|
||||||
WINDOW_SIZE = (640,480)
|
WINDOW_SIZE = (640,480)
|
||||||
FPS = 120
|
FPS = 120
|
||||||
|
|
||||||
def rounding(x, base): return int(base * round(float(x)/base))
|
def rounding(x, base): return int(base * round(float(x)/base))
|
||||||
|
|
||||||
|
def update_score(window, score):
|
||||||
|
font = pygame.font.SysFont(None, 30, bold=False)
|
||||||
|
window.blit(font.render("Score: {}".format(int(score)), True, (12,123,190)), (50, 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)
|
||||||
|
@ -20,29 +22,31 @@ def initialise(menu, options):
|
||||||
window = pygame.display.set_mode(WINDOW_SIZE)
|
window = pygame.display.set_mode(WINDOW_SIZE)
|
||||||
|
|
||||||
exit_code = play(window) # Run main game loop
|
exit_code = play(window) # Run main game loop
|
||||||
if exit_code =
|
|
||||||
pygame.quit()
|
if exit_code == "QUIT":
|
||||||
# Reshow the main menu
|
pygame.quit()
|
||||||
|
|
||||||
|
menu.deiconify()
|
||||||
|
return exit_code
|
||||||
|
|
||||||
|
|
||||||
def generate_targets():
|
def generate_targets():
|
||||||
group = pygame.sprite.Group()
|
group = pygame.sprite.Group()
|
||||||
target_object = Target()
|
for i in range(5): # Number of rows
|
||||||
for i in range(10,150,50):
|
i *= 25
|
||||||
for j in range(10, 150, 50):
|
for j in range(50, WINDOW_SIZE[0] - 70, 30):
|
||||||
temp = Target()
|
#logging.debug("Target generated with position ({},{})".format(j,i))
|
||||||
logging.debug("Target generated with position ({},{})".format(j,i))
|
temp = Target(x=j,y=i)
|
||||||
temp.set_position(j,i)
|
group.add(temp)
|
||||||
group.add(temp)
|
del temp
|
||||||
del temp
|
return group
|
||||||
return group
|
|
||||||
|
|
||||||
|
|
||||||
def play(window):
|
def play(window):
|
||||||
window_rect = window.get_rect()
|
window_rect = window.get_rect()
|
||||||
|
|
||||||
player = Shooter(window=window)
|
player = Shooter(window=window)
|
||||||
player.set_position(WINDOW_SIZE[0]/2, WINDOW_SIZE[1]*0.93)
|
player.set_position(WINDOW_SIZE[0]/2, WINDOW_SIZE[1]*0.83)
|
||||||
player_group = pygame.sprite.Group()
|
player_group = pygame.sprite.Group()
|
||||||
player_group.add(player)
|
player_group.add(player)
|
||||||
player_group.draw(window)
|
player_group.draw(window)
|
||||||
|
@ -52,11 +56,14 @@ def play(window):
|
||||||
|
|
||||||
clock = pygame.time.Clock()
|
clock = pygame.time.Clock()
|
||||||
PLAYING_GAME = True
|
PLAYING_GAME = True
|
||||||
|
target_movement_timeout_default = FPS * 0.5
|
||||||
|
target_movement_timeout = target_movement_timeout_default
|
||||||
|
|
||||||
while PLAYING_GAME:
|
while PLAYING_GAME:
|
||||||
window.fill((0,0,0))
|
window.fill((0,0,0))
|
||||||
player_group.update()
|
player_group.update()
|
||||||
|
|
||||||
|
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
logging.critical("Exiting Game...")
|
logging.critical("Exiting Game...")
|
||||||
|
@ -67,6 +74,7 @@ def play(window):
|
||||||
temp = Bullet(player)
|
temp = Bullet(player)
|
||||||
bullet_group.add(temp)
|
bullet_group.add(temp)
|
||||||
|
|
||||||
|
|
||||||
keys = pygame.key.get_pressed()
|
keys = pygame.key.get_pressed()
|
||||||
if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
|
if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
|
||||||
player.move(player.speed)
|
player.move(player.speed)
|
||||||
|
@ -74,30 +82,59 @@ def play(window):
|
||||||
if keys[pygame.K_LEFT] or keys[pygame.K_a]:
|
if keys[pygame.K_LEFT] or keys[pygame.K_a]:
|
||||||
player.move(-player.speed)
|
player.move(-player.speed)
|
||||||
|
|
||||||
if keys[pygame.K_KP4] and keys[pygame.K_KP5] and keys[pygame.K_KP6]:
|
if keys[pygame.K_KP4] and keys[pygame.K_KP5] and keys[pygame.K_KP6] and player.OP:
|
||||||
temp = Bullet(player)
|
temp = Bullet(player)
|
||||||
bullet_group.add(temp)
|
bullet_group.add(temp)
|
||||||
|
|
||||||
|
|
||||||
for sprite in bullet_group:
|
for sprite in bullet_group:
|
||||||
if not sprite.at_top():
|
if not sprite.at_top():
|
||||||
sprite.update()
|
sprite.update()
|
||||||
if sprite.rect.y < 0:
|
if sprite.rect.y < 0:
|
||||||
bullet_group.remove(sprite)
|
bullet_group.remove(sprite)
|
||||||
|
|
||||||
|
|
||||||
for bullet in bullet_group:
|
for bullet in bullet_group:
|
||||||
hit_list = pygame.sprite.spritecollide(bullet, target_group, True)
|
hit_list = pygame.sprite.spritecollide(bullet, target_group, True)
|
||||||
for target in hit_list:
|
for target in hit_list:
|
||||||
target_group.remove(target)
|
target_group.remove(target)
|
||||||
bullet_group.remove(bullet)
|
bullet_group.remove(bullet)
|
||||||
logging.info("Hit Target!")
|
logging.info("Hit Target!")
|
||||||
|
player.score += 1
|
||||||
|
|
||||||
|
|
||||||
|
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))
|
||||||
|
return "player collision"
|
||||||
|
|
||||||
|
if drop_targets:
|
||||||
|
logging.debug("drop")
|
||||||
|
for target in target_group:
|
||||||
|
target.drop()
|
||||||
|
|
||||||
|
target_movement_timeout = target_movement_timeout_default
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
update_score(window, player.score)
|
||||||
player_group.update()
|
player_group.update()
|
||||||
bullet_group.draw(window)
|
bullet_group.draw(window)
|
||||||
target_group.draw(window)
|
target_group.draw(window)
|
||||||
player_group.draw(window)
|
player_group.draw(window)
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
|
target_movement_timeout -= 1
|
||||||
clock.tick(FPS)
|
clock.tick(FPS)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
logging.basicConfig(format="%(levelname)s:%(message)s", level=logging.DEBUG)
|
||||||
initialise(None, None)
|
initialise(None, None)
|
||||||
|
|
Reference in a new issue