0% found this document useful (0 votes)
34 views3 pages

Import Pygame

my code

Uploaded by

rawalpindi1895
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views3 pages

Import Pygame

my code

Uploaded by

rawalpindi1895
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import pygame

import random

# Initialize Pygame
pygame.init()

# Screen dimensions
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Battleground Game")

# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

# Player properties
PLAYER_SIZE = 50
PLAYER_SPEED = 5

# Enemy properties
ENEMY_SIZE = 50
ENEMY_SPEED = 3

# Health Bar
HEALTH_WIDTH = 50
HEALTH_HEIGHT = 10

# Player class
class Player(pygame.sprite.Sprite):
def _init_(self, x, y):
super()._init_()
self.image = pygame.Surface((PLAYER_SIZE, PLAYER_SIZE))
self.image.fill(GREEN)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.health = 100

def update(self, keys):


if keys[pygame.K_LEFT]:
self.rect.x -= PLAYER_SPEED
if keys[pygame.K_RIGHT]:
self.rect.x += PLAYER_SPEED
if keys[pygame.K_UP]:
self.rect.y -= PLAYER_SPEED
if keys[pygame.K_DOWN]:
self.rect.y += PLAYER_SPEED

def draw_health_bar(self, surface):


pygame.draw.rect(surface, RED, (self.rect.x, self.rect.y - HEALTH_HEIGHT -
2, HEALTH_WIDTH, HEALTH_HEIGHT))
pygame.draw.rect(surface, GREEN, (self.rect.x, self.rect.y - HEALTH_HEIGHT
- 2, HEALTH_WIDTH * (self.health / 100), HEALTH_HEIGHT))

# Enemy class
class Enemy(pygame.sprite.Sprite):
def _init_(self, x, y):
super()._init_()
self.image = pygame.Surface((ENEMY_SIZE, ENEMY_SIZE))
self.image.fill(RED)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y

def update(self, player):

if self.rect.x < player.rect.x:


self.rect.x += ENEMY_SPEED
if self.rect.x > player.rect.x:
self.rect.x -= ENEMY_SPEED
if self.rect.y < player.rect.y:
self.rect.y += ENEMY_SPEED
if self.rect.y > player.rect.y:
self.rect.y -= ENEMY_SPEED

# Function to check for collisions


def check_collision(player, enemies):
for enemy in enemies:
if pygame.sprite.collide_rect(player, enemy):
player.health -= 1
if player.health <= 0:
return True
return False

# Main game loop


def main():
clock = pygame.time.Clock()
player = Player(WIDTH // 2, HEIGHT // 2)
enemies = pygame.sprite.Group()

for _ in range(5):
x = random.randint(0, WIDTH - ENEMY_SIZE)
y = random.randint(0, HEIGHT - ENEMY_SIZE)
enemy = Enemy(x, y)
enemies.add(enemy)

all_sprites = pygame.sprite.Group()
all_sprites.add(player)
all_sprites.add(enemies)

running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

keys = pygame.key.get_pressed()
player.update(keys)
enemies.update(player)

if check_collision(player, enemies):
print("Game Over!")
running = False

screen.fill(WHITE)
all_sprites.draw(screen)
player.draw_health_bar(screen)
pygame.display.flip()
clock.tick(30)

pygame.quit()

if _name_ == "_main_":
main()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy