Ship Shooting
unknown
python
3 years ago
7.4 kB
11
Indexable
import cv2
import numpy as np
import keyboard
# Set the speed for each spaceship
spaceship1_speed = 10
spaceship2_speed = 20
spaceship3_speed = 30
# Ask the player to choose a spaceship
print("Choose a spaceship P1 (1, 2, or 3):")
choice = input()
# Load the selected spaceship image and speed
if choice == "1":
ship1 = cv2.imread("spaceship1.png")
speed = spaceship1_speed
elif choice == "2":
ship1 = cv2.imread("spaceship2.png")
speed = spaceship2_speed
elif choice == "3":
ship1 = cv2.imread("spaceship3.png")
speed = spaceship3_speed
# Ask the player to choose a spaceship
print("Choose a spaceship P2 (1, 2, or 3):")
choice = input()
# Load the selected spaceship image and speed
if choice == "1":
ship2 = cv2.imread("spaceship1.png")
speed = spaceship1_speed
elif choice == "2":
ship2 = cv2.imread("spaceship2.png")
speed = spaceship2_speed
elif choice == "3":
ship2 = cv2.imread("spaceship3.png")
speed = spaceship3_speed
#Load Laser assets
laser = cv2.imread('./assets/bullet.png')
# Load the background image
bg = cv2.imread('./assets/background.png')
# Create a window to display the game
cv2.namedWindow('Space Game', cv2.WINDOW_NORMAL)
# Define the starting position of the spaceship
ship1_x = 10
ship1_y = 200
# Define the starting position of the spaceship
ship2_x = 600
ship2_y = 200
# Define the key variable
key = None
def move_ship_up(ship, gravity, acceleration):
# Get the height and width of the ship image
height, width = ship.shape[:2]
# Calculate the new position of the ship
new_y = ship_y - acceleration + gravity
if new_y < 0:
new_y = 0
elif new_y + height > screen_height:
new_y = screen_height - height
# Update the ship's position
ship_y = new_y
# Return the updated ship image
return ship
def move_ship_down(ship, gravity, acceleration):
# Get the height and width of the ship image
height, width = ship.shape[:2]
# Calculate the new position of the ship
new_y = ship_y - acceleration + gravity
if new_y < 0:
new_y = 0
elif new_y + height > screen_height:
new_y = screen_height - height
# Update the ship's position
ship_y = new_y
# Return the updated ship image
return ship
def shoot_bullet(bullets, ship_x, ship_y):
# Load the bullet image
bullet_image = cv2.imread("assets/bullet.png")
# Create a new bullet object
bullet = {
"image": bullet_image,
"x": ship_x,
"y": ship_y,
"speed": 10
}
# Add the bullet to the list of bullets
bullets.append(bullet)
# Return the updated list of bullets
return bullets
def activate_shield(player, shield_image):
# Set the shield active flag to true
player["shield_active"] = True
# Set the start time of the shield
player["shield_start_time"] = time.time()
# Set the shield image for the player
player["shield_image"] = shield_image
def deactivate_shield(player):
# Set the shield active flag to false
player["shield_active"] = False
# Set the shield image for the player to None
player["shield_image"] = None
# Blur the player's ship while the shield is active
if player["shield_active"]:
player["image"] = cv2.blur(player["image"], (5, 5))
def update_bullets(bullets, player1, player2):
# Iterate over the bullets and update their positions
for bullet in bullets:
bullet["x"] += bullet["speed"]
# Check if the bullet has collided with the opponent's ship
if not player2["shield_active"]:
if bullet["x"] > player2["x"] and bullet["x"] < player2["x"] + player2["width"] and \
bullet["y"] > player2["y"] and bullet["y"] < player2["y"] + player2["height"]:
# Reduce the opponent's hitpoints
player2["hitpoints"] -= 1
# Check if the opponent's ship is destroyed
if player2["hitpoints"] <= 0:
# Show the winning message
print("Player 1 wins!")
# Remove the bullet
bullets.remove(bullet)
if not player1["shield_active"]:
if bullet["x"] > player1["x"] and bullet["x"] < player1["x"] + player1["width"] and \
bullet["y"] > player1["y"] and bullet["y"] < player1["y"] + player1["height"]:
# Reduce the opponent's hitpoints
player1["hitpoints"] -= 1
# Check if the opponent's ship is destroyed
if player1["hitpoints"] <= 0:
# Show the winning message
print("Player 2 wins!")
# Remove the bullet
bullets.remove(bullet)
# Return the updated list of bullets
return bullets
def explode(spaceship):
# Create a list to store the explosion animation frames
frames = []
# Scale the spaceship image up to create the initial "expanding" effect
explosion = cv2.resize(spaceship, (spaceship.shape[1] * 2, spaceship.shape[0] * 2))
# Add the first frame to the list of animation frames
frames.append(explosion)
# Apply image filters to create the explosion effect
for i in range(10):
explosion = cv2.GaussianBlur(explosion, (5, 5), 0)
explosion = cv2.addWeighted(explosion, 0.6, np.zeros(spaceship.shape, spaceship.dtype), 0.4, 0)
frames.append(explosion)
# Return the list of animation frames
return frames
# Generate the explosion animation
frames = explode(spaceship)
# Display the animation on the screen
for frame in frames:
cv2.imshow("Explosion", frame)
cv2.waitKey(100)
# Define the starting position of the spaceship
ship1_x = 10
ship1_y = 200
# Define the starting position of the spaceship
ship2_x = 600
ship2_y = 200
# Create a window to display the game
cv2.namedWindow('Space Game', cv2.WINDOW_NORMAL)
# Loop until the user closes the game window
while True:
# Check if the player 1 pressed the up arrow key
if keyboard.is_pressed('up'):
# Move the spaceship up
move_ship_up(ship1, gravity, acceleration)
ship1_y -= 10
# Check if the player 1 pressed the down arrow key
elif keyboard.is_pressed('down'):
# Move the spaceship down
move_ship_down(ship1, gravity, acceleration)
ship1_y += 10
# Check if the player 1 pressed the space key
elif keyboard.is_pressed('shift'):
# Shoot a bullet
shoot_bullet(bullets, ship1_x, ship1_y)
elif keyboard.is_pressed('x'):
# Shoot a bullet
activate_shield(ship2, shield_image)
# Check if the player 2 pressed the w key
if keyboard.is_pressed('w'):
# Move the spaceship up
move_ship_up(ship2, gravity, acceleration)
ship2_y -= 10
# Check if the player 2 pressed the s key
elif keyboard.is_pressed('s'):
# Move the spaceship down
move_ship_down(ship2, gravity, acceleration)
ship2_y += 10
# Check if the player 2 pressed the d key
elif keyboard.is_pressed('space'):
# Shoot a bullet
shoot_bullet(bullets, ship2_x, ship2_y)
elif keyboard.is_pressed('ctrl'):
# Shoot a bullet
activate_shield(ship2, shield_image)
# Update the game screen
update_game_screen(ship1, ship1_x, ship1_y, ship2, ship2_x, ship2_y, bullets, bg)
# Check if the user pressed the 'q' key to quit the game
if keyboard.is_pressed('q'):
break
Editor is loading...