georgewalton / Brains-and-Bones

A fun two-player snake-based game
0 stars 0 forks source link

python #1

Open navaneethsigmachad opened 1 month ago

navaneethsigmachad commented 1 month ago

import pygame import math import random import time

Initialize Pygame

pygame.init()

Set up the display

screen = pygame.display.set_mode((800, 600))

Set caption and icon

pygame.display.set_caption("Space Invaders") icon = pygame.image.load('D:\navaneeth\photoshop\space invaders logo\logo copy.png') pygame.display.set_icon(icon)

Load images

player_image = pygame.image.load('D:\navaneeth\photoshop\player\playergg.png') enemy_images = [ pygame.image.load('D:\navaneeth\photoshop\player\enemy copygg.png'), pygame.image.load('D:\navaneeth\photoshop\player\sss.png') ] asteroid_image = pygame.image.load('D:\navaneeth\photoshop\player\gg.png')

Player variables

playerX = 370 playerY = 480 player_angle = 0 player_health = 150 player_speed = 0.3 player_rotation_speed = 4 player_last_regen = time.time() player_invulnerable = False player_invulnerability_time = 2

Enemy variables

enemy_positions = [ [random.randint(0, 736), random.randint(50, 150)], [random.randint(0, 736), random.randint(50, 150)] ] enemy_health = [150, 150] enemy_changes = [ [0.1, 40], [0.1, 40] ] enemy_angles = [0, 0] enemy_last_regen = [time.time(), time.time()] enemy_invulnerable = [False, False] enemy_invulnerability_time = 2

Asteroid variables

asteroids = [[random.randint(0, 736), random.randint(0, 150), 100] for _ in range(3)]

Laser variables

lasers = [] laser_colors = [(0, 0, 255), (255, 255, 0), (255, 0, 0)] laser_damage = [35, 65, 100] laser_cooldown = 500 last_shot = pygame.time.get_ticks()

Score variable

score = 0 font = pygame.font.Font(None, 36)

Function to draw the player

def player(x, y, angle): rotated_image = pygame.transform.rotate(player_image, angle) new_rect = rotated_image.get_rect(center=player_image.get_rect(topleft=(x, y)).center) screen.blit(rotated_image, new_rect.topleft)

Function to draw an enemy

def enemy(x, y, angle, i): rotated_image = pygame.transform.rotate(enemy_images[i], angle) new_rect = rotated_image.get_rect(center=enemy_images[i].get_rect(topleft=(x, y)).center) screen.blit(rotated_image, new_rect.topleft)

Function to draw an asteroid

def asteroid(x, y): screen.blit(asteroid_image, (x, y))

Function to draw lasers

def draw_lasers(): for laser in lasers: pygame.draw.rect(screen, laser[2], pygame.Rect(laser[0], laser[1], 5, 20))

Function to draw health bars

def draw_health_bar(x, y, health): pygame.draw.rect(screen, (255, 0, 0), (x, y, 100, 10)) pygame.draw.rect(screen, (0, 255, 0), (x, y, health, 10))

Function to handle shooting

def shoot_laser(x, y, angle, color, damage): rad_angle = math.radians(angle) laserX = x + 16 + 20 math.cos(rad_angle) laserY = y + 16 - 20 math.sin(rad_angle) lasers.append([laserX, laserY, color, damage, rad_angle])

Function to update laser positions

def update_lasers(): for laser in lasers: laser[0] += 0.5 math.cos(laser[4]) laser[1] -= 0.5 math.sin(laser[4]) if laser[0] < 0 or laser[0] > 800 or laser[1] < 0 or laser[1] > 600: lasers.remove(laser)

Function to check collisions

def is_collision(x1, y1, x2, y2, distance): return math.sqrt((x2 - x1)2 + (y2 - y1)2) < distance

Function to find the nearest target

def find_nearest_target(x, y, targets): nearest_target = None min_distance = float('inf') for target in targets: distance = math.sqrt((target[0] - x)2 + (target[1] - y)2) if distance < min_distance: min_distance = distance nearest_target = target return nearest_target

Function to draw the "How to Play" screen

def draw_how_to_play(): font = pygame.font.Font(None, 36) text_lines = [ "How to Play", "A & D or Left & Right Arrows: Rotate Clockwise & Anti-Clockwise", "Mouse: Move Ship", "Left Click: Shoot", "Press 'P' to Play Now" ] y_offset = 150 for line in text_lines: text = font.render(line, True, (255, 255, 255)) screen.blit(text, (200, y_offset)) y_offset += 50

Function to respawn asteroids

def respawnasteroids(): global asteroids asteroids = [[random.randint(0, 736), random.randint(0, 150), 100] for in range(3)]

Function to show the score

def show_score(): score_text = font.render(f"Score: {score}", True, (255, 255, 255)) screen.blit(score_text, (10, 10))

Function to restart the game

def restart_game(): global playerX, playerY, player_angle, player_health, enemy_positions, enemy_health, asteroids, lasers, score playerX = 370 playerY = 480 player_angle = 0 player_health = 150 enemy_positions = [ [random.randint(0, 736), random.randint(50, 150)], [random.randint(0, 736), random.randint(50, 150)] ] enemyhealth = [150, 150] asteroids = [[random.randint(0, 736), random.randint(0, 150), 100] for in range(3)] lasers = [] score = 0

Function to handle game over

def game_over(): global running font = pygame.font.Font(None, 74) game_over_text = font.render("Game Over", True, (255, 255, 255)) screen.blit(game_over_text, (250, 250)) pygame.display.update() time.sleep(2) font = pygame.font.Font(None, 36) restart_text = font.render("Press R to Restart or Q to Quit", True, (255, 255, 255)) screen.blit(restart_text, (200, 300))

navaneethsigmachad commented 1 month ago

thanks