Update main.py
parent
6354532665
commit
73047b9406
55
main.py
55
main.py
|
@ -9,35 +9,35 @@ import os.path
|
||||||
pygame.init()
|
pygame.init()
|
||||||
screen = pygame.display.set_mode((1000, 700))
|
screen = pygame.display.set_mode((1000, 700))
|
||||||
pygame.display.set_caption("Space Invaders")
|
pygame.display.set_caption("Space Invaders")
|
||||||
icon = pygame.image.load("ICON.png")
|
icon = pygame.image.load("images/ICON.png")
|
||||||
pygame.display.set_icon(icon)
|
pygame.display.set_icon(icon)
|
||||||
|
|
||||||
|
|
||||||
space = pygame.image.load('SPACE.jpg')
|
space = pygame.image.load('images/SPACE.jpg')
|
||||||
hangar = pygame.image.load('HANGAR.jpg')
|
hangar = pygame.image.load('images/HANGAR.jpg')
|
||||||
|
|
||||||
|
|
||||||
mixer.music.load("music/background.mp3")
|
mixer.music.load("music/background.mp3")
|
||||||
mixer.music.play(-1)
|
mixer.music.play(-1)
|
||||||
|
|
||||||
|
|
||||||
font = pygame.font.Font("KodeMono-Medium.ttf", 32)
|
font = pygame.font.Font("font/KodeMono-Medium.ttf", 32)
|
||||||
smaller_font = pygame.font.Font("KodeMono-Medium.ttf", 24)
|
smaller_font = pygame.font.Font("font/KodeMono-Medium.ttf", 24)
|
||||||
over_font = pygame.font.Font("KodeMono-Medium.ttf", 96)
|
over_font = pygame.font.Font("font/KodeMono-Medium.ttf", 96)
|
||||||
|
|
||||||
TEXT_COL = (255, 255, 255)
|
TEXT_COL = (255, 255, 255)
|
||||||
|
|
||||||
SCORE_FILE_NAME = "score.scr"
|
SCORE_FILE_NAME = "score.scr"
|
||||||
|
|
||||||
|
|
||||||
new_game_img = pygame.image.load("new_game.png")
|
new_game_img = pygame.image.load("images/new_game.png")
|
||||||
difficulty_img = pygame.image.load("difficulty.png")
|
difficulty_img = pygame.image.load("images/difficulty.png")
|
||||||
best_score_img = pygame.image.load("best_score.png")
|
best_score_img = pygame.image.load("images/best_score.png")
|
||||||
back_img = pygame.image.load("back.png")
|
back_img = pygame.image.load("images/back.png")
|
||||||
difficulty_easy_img = pygame.image.load("difficulty_easy.png")
|
difficulty_easy_img = pygame.image.load("images/difficulty_easy.png")
|
||||||
difficulty_medium_img = pygame.image.load("difficulty_medium.png")
|
difficulty_medium_img = pygame.image.load("images/difficulty_medium.png")
|
||||||
difficulty_hard_img = pygame.image.load("difficulty_hard.png")
|
difficulty_hard_img = pygame.image.load("images/difficulty_hard.png")
|
||||||
difficulty_insane_img = pygame.image.load("difficulty_insane.png")
|
difficulty_insane_img = pygame.image.load("images/difficulty_insane.png")
|
||||||
|
|
||||||
|
|
||||||
new_game_button = button.Button(400, 320, new_game_img, 1)
|
new_game_button = button.Button(400, 320, new_game_img, 1)
|
||||||
|
@ -148,9 +148,8 @@ def read_score_table():
|
||||||
def add_score(score):
|
def add_score(score):
|
||||||
current = read_score_table()
|
current = read_score_table()
|
||||||
current.append(score)
|
current.append(score)
|
||||||
current.sort()
|
current.sort(reverse=True) # Sort in descending order
|
||||||
updated = current[1:5]
|
updated = current[:4] # Keep only the top 4 scores
|
||||||
print(score, updated)
|
|
||||||
update_score_table(updated)
|
update_score_table(updated)
|
||||||
|
|
||||||
|
|
||||||
|
@ -162,18 +161,18 @@ textY = 10
|
||||||
#player data
|
#player data
|
||||||
playerX = 450
|
playerX = 450
|
||||||
playerY = 580
|
playerY = 580
|
||||||
player_image = pygame.image.load("SPACESHIP.png")
|
player_image = pygame.image.load("images/SPACESHIP.png")
|
||||||
playerX_change = 0
|
playerX_change = 0
|
||||||
|
|
||||||
#bullet data
|
#bullet data
|
||||||
bullet_image = pygame.image.load("BULLET.png")
|
bullet_image = pygame.image.load("images/BULLET.png")
|
||||||
bulletX = 0
|
bulletX = 0
|
||||||
bulletY = 560
|
bulletY = 560
|
||||||
bulletY_change = 20
|
bulletY_change = 20
|
||||||
bullet_state = "ready"
|
bullet_state = "ready"
|
||||||
|
|
||||||
#enemy data
|
#enemy data
|
||||||
enemy_image = pygame.image.load("ENEMY.png")
|
enemy_image = pygame.image.load("images/ENEMY.png")
|
||||||
enemies = reset_enemies(difficulty)
|
enemies = reset_enemies(difficulty)
|
||||||
num_of_enemies = len(enemies)
|
num_of_enemies = len(enemies)
|
||||||
|
|
||||||
|
@ -234,7 +233,7 @@ while running:
|
||||||
score_val = 0
|
score_val = 0
|
||||||
#if game is lost, print some text like wanna start again? press R to continue
|
#if game is lost, print some text like wanna start again? press R to continue
|
||||||
game_lost = True
|
game_lost = True
|
||||||
draw_text('Press "R" to return to menu', font, TEXT_COL, 270, 600)
|
draw_text('Press "TAB" to return to menu', font, TEXT_COL, 270, 600)
|
||||||
break
|
break
|
||||||
|
|
||||||
# enemy movement
|
# enemy movement
|
||||||
|
@ -250,7 +249,7 @@ while running:
|
||||||
#Collision check
|
#Collision check
|
||||||
collision = is_collision(enem["X"], enem["Y"], bulletX, bulletY)
|
collision = is_collision(enem["X"], enem["Y"], bulletX, bulletY)
|
||||||
if collision:
|
if collision:
|
||||||
explosion_Sound = mixer.Sound("explosion.wav")
|
explosion_Sound = mixer.Sound("music/explosion.wav")
|
||||||
explosion_Sound.play()
|
explosion_Sound.play()
|
||||||
bulletY = 560
|
bulletY = 560
|
||||||
bullet_state = "ready"
|
bullet_state = "ready"
|
||||||
|
@ -265,10 +264,10 @@ while running:
|
||||||
bullet_state = "ready"
|
bullet_state = "ready"
|
||||||
if bullet_state == "fire":
|
if bullet_state == "fire":
|
||||||
shoot_bullet(bulletX, bulletY)
|
shoot_bullet(bulletX, bulletY)
|
||||||
bulletY -= 2.5
|
bulletY -= 4.5
|
||||||
|
|
||||||
# score, enemy and player update
|
# score, enemy and player update
|
||||||
draw_text("Press TAB to pause", smaller_font, TEXT_COL, 750, 10)
|
draw_text("Press R to pause", smaller_font, TEXT_COL, 750, 10)
|
||||||
show_score(textX, textY)
|
show_score(textX, textY)
|
||||||
for i, enem in enumerate(enemies):
|
for i, enem in enumerate(enemies):
|
||||||
enemy(enem["X"], enem["Y"])
|
enemy(enem["X"], enem["Y"])
|
||||||
|
@ -285,7 +284,7 @@ while running:
|
||||||
case 3:
|
case 3:
|
||||||
temp = "insane"
|
temp = "insane"
|
||||||
print_best_score(read_score_table())
|
print_best_score(read_score_table())
|
||||||
draw_text("Press TAB to resume", smaller_font, TEXT_COL, 350, 550)
|
draw_text("Press R to resume", smaller_font, TEXT_COL, 350, 550)
|
||||||
case _:
|
case _:
|
||||||
screen.blit(space, (0, 0))
|
screen.blit(space, (0, 0))
|
||||||
#event loop
|
#event loop
|
||||||
|
@ -293,13 +292,13 @@ while running:
|
||||||
if scene == "game":
|
if scene == "game":
|
||||||
# player movement with left and right arrows. When key is unpressed, stop the movement
|
# player movement with left and right arrows. When key is unpressed, stop the movement
|
||||||
if event.type == pygame.KEYDOWN:
|
if event.type == pygame.KEYDOWN:
|
||||||
if event.key == pygame.K_TAB:
|
if event.key == pygame.K_r:
|
||||||
if game_paused:
|
if game_paused:
|
||||||
game_paused = False
|
game_paused = False
|
||||||
else:
|
else:
|
||||||
game_paused = True
|
game_paused = True
|
||||||
# if game is over, then you can restart it by pressing the R key
|
# if game is over, then you can restart it by pressing the R key
|
||||||
if game_lost and event.key == pygame.K_r:
|
if game_lost and event.key == pygame.K_TAB:
|
||||||
scene = "menu"
|
scene = "menu"
|
||||||
game_lost = False
|
game_lost = False
|
||||||
if event.key == pygame.K_LEFT:
|
if event.key == pygame.K_LEFT:
|
||||||
|
@ -308,7 +307,7 @@ while running:
|
||||||
playerX_change = 1.2
|
playerX_change = 1.2
|
||||||
if event.key == pygame.K_SPACE:
|
if event.key == pygame.K_SPACE:
|
||||||
if bullet_state == "ready":
|
if bullet_state == "ready":
|
||||||
bullet_sound = mixer.Sound("Laser.wav")
|
bullet_sound = mixer.Sound("music/Laser.wav")
|
||||||
bullet_sound.play()
|
bullet_sound.play()
|
||||||
bulletX = playerX
|
bulletX = playerX
|
||||||
shoot_bullet(bulletX, bulletY)
|
shoot_bullet(bulletX, bulletY)
|
||||||
|
|
Loading…
Reference in New Issue