Copy this code and paste it into your code editor, make sure Python is installed, then save it as “AlienHunter.py” in a new folder, then Download all the files from above and save them also in the same folder…
If you face any problem… feel free to contact me…
import pygame # Import the Pygame library for game development
import random # Import the random module for generating random numbers
import math # Import the math module for mathematical functions
from pygame import mixer # Import the mixer module for handling sounds
pygame.init() # Initialize Pygame
score_value = 0 # Initialize the score value
font = pygame.font.Font('freesansbold.ttf', 32) # Define font for rendering text
textX = 10 # Set the X coordinate for displaying score
textY = 10 # Set the Y coordinate for displaying score
over_font = pygame.font.Font('freesansbold.ttf', 64) # Define font for rendering game over text
# Function to display the score on the screen
def show_score(x, y):
score = font.render("Score::" + str(score_value), True, (255, 255, 255))
screen.blit(score, (x, y))
# Function to display the game over text on the screen
def game_over_text():
over_text = over_font.render("GAME OVER", True, (10, 200, 255))
screen.blit(over_text, (200, 250))
# Set up the screen
screen = pygame.display.set_mode((800, 600))
# Load background image
background = pygame.image.load('bg.png')
# Load background music and play it on loop
mixer.music.load('bgm.wav')
mixer.music.play(-1)
# Set window title
pygame.display.set_caption("Alien Hunter")
# Load game icon
icon = pygame.image.load('galaxy.png')
pygame.display.set_icon(icon)
# Player setup
playerimg = pygame.image.load('player.png') # Load player image
playerX = 370 # Set initial X coordinate for player
playerY = 480 # Set initial Y coordinate for player
playerX_change = 0 # Set initial change in X coordinate for player
# Function to draw the player on the screen
def player(x, y):
screen.blit(playerimg, (x, y))
# Enemy setup
enemyimg = [] # List to store enemy images
enemyX = [] # List to store X coordinates of enemies
enemyY = [] # List to store Y coordinates of enemies
enemyX_change = [] # List to store change in X coordinates of enemies
enemyY_change = [] # List to store change in Y coordinates of enemies
num_of_enemies = 6 # Number of enemies
# Load enemy images and set initial positions and movement speeds
for i in range(num_of_enemies):
enemyimg.append(pygame.image.load('enemy1.png'))
enemyX.append(random.randint(0, 736))
enemyY.append(random.randint(50, 150))
enemyX_change.append(20)
enemyY_change.append(40)
# Function to draw an enemy on the screen
def enemy(x, y, i):
screen.blit(enemyimg[i], (x, y))
# Bullet setup
bulletimg = pygame.image.load('bullet.png') # Load bullet image
bulletX = 0 # Set initial X coordinate for bullet
bulletY = 480 # Set initial Y coordinate for bullet
bulletX_change = 0 # Set initial change in X coordinate for bullet
bulletY_change = 10 # Set change in Y coordinate for bullet
bullet_state = "ready" # Set bullet state to ready
# Function to fire a bullet
def fire_bullet(x, y):
global bullet_state
bullet_state = "fire"
screen.blit(bulletimg, (x + 23, y))
# Function to check for collisions between enemy and bullet
def isCollision(eX, eY, bX, bY):
distance = math.sqrt(math.pow(eX - bX, 2) + math.pow(eY - bY, 2))
if distance < 27:
return True
else:
return False
running = True # Flag to control the game loop
while running:
screen.fill((0, 0, 0)) # Fill the screen with black color
screen.blit(background, (0, 0)) # Draw the background image
for event in pygame.event.get(): # Event handling loop
if event.type == pygame.QUIT: # Check if user clicked close button
running = False # Exit the game loop
if event.type == pygame.KEYDOWN: # Check if user pressed a key
if event.key == pygame.K_LEFT: # Check if user pressed left arrow key
playerX_change = -22 # Move player to the left
if event.key == pygame.K_RIGHT: # Check if user pressed right arrow key
playerX_change = 22 # Move player to the right
if event.key == pygame.K_SPACE: # Check if user pressed spacebar
if bullet_state == "ready": # Check if bullet is ready to be fired
bullet_sound = mixer.Sound('laser.wav') # Load bullet firing sound
bullet_sound.play() # Play bullet firing sound
bulletX = playerX # Set bullet X coordinate to player X coordinate
fire_bullet(bulletX, bulletY) # Fire the bullet
if event.type == pygame.KEYUP: # Check if user released a key
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT: # Check if user released left or right arrow key
playerX_change = 0 # Stop player movement
playerX += playerX_change # Update player X coordinate
# Keep player within the screen boundaries
if playerX <= 0:
playerX = 0
elif playerX >= 736:
playerX = 736
# Update enemy positions and check for collisions
for i in range(num_of_enemies): # Iterate through each enemy
if enemyY[i] > 440: # Check if enemy has crossed the bottom boundary
for i in range(num_of_enemies): # Reset positions of all enemies
enemyY[i] = 2000 # Move enemy off-screen
gameover_sound = mixer.Sound('gameover.wav') # Load game over sound
gameover_sound.play() # Play game over sound
game_over_text() # Display game over text
break # Exit the loop
# Move the enemy horizontally
enemyX[i] += enemyX_change[i]
# Check if enemy hits left boundary, then change direction and move down
if enemyX[i] <= 0:
enemyX_change[i] = 4
enemyY[i] += enemyY_change[i]
# Check if enemy hits right boundary, then change direction and move down
elif enemyX[i] >= 736:
enemyX_change[i] = -4
enemyY[i] += enemyY_change[i]
# Check for collision between enemy and bullet
collision = isCollision(enemyX[i], enemyY[i], bulletX, bulletY)
if collision:
explosion_sound = mixer.Sound('explosion.wav') # Load explosion sound
explosion_sound.play() # Play explosion sound
bulletY = 480 # Reset bullet position
bullet_state = "ready" # Set bullet state to ready
score_value += 1 # Increment score
print(score_value) # Print score
# Respawn the enemy at a random position
enemyX[i] = random.randint(0, 736)
enemyY[i] = random.randint(50, 100)
# Draw the enemy on the screen
enemy(enemyX[i], enemyY[i], i)
# Check if bullet has reached the top boundary
if bulletY <= 0:
bullet_state = "ready" # Set bullet state to ready
bulletY = 480 # Reset bullet position
# Check if bullet is currently firing
if bullet_state == "fire":
fire_bullet(bulletX, bulletY) # Fire the bullet
bulletY -= bulletY_change # Move the bullet upwards
# Draw the player on the screen
player(playerX, playerY)
# Display the score on the screen
show_score(textX, textY)
# Update the display
pygame.display.update()
Leave a comment