0% found this document useful (0 votes)
65 views

Mini Game Code

This document defines code for a 2D pygame game where two farmers (represented as rectangles) face off, moving around a game area and shooting bullets at each other. Trees are included that can be damaged by bullets. Health points are tracked for each farmer and tree. The code initializes the game window and assets, defines movement and shooting functions, handles collisions and health points, and includes a main game loop to continuously update and draw the game until a winner is determined.

Uploaded by

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

Mini Game Code

This document defines code for a 2D pygame game where two farmers (represented as rectangles) face off, moving around a game area and shooting bullets at each other. Trees are included that can be damaged by bullets. Health points are tracked for each farmer and tree. The code initializes the game window and assets, defines movement and shooting functions, handles collisions and health points, and includes a main game loop to continuously update and draw the game until a winner is determined.

Uploaded by

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

import pygame

import os
pygame.font.init()

WIDTH, HEIGHT = (900, 500)


WIN = pygame.display.set_mode((WIDTH, HEIGHT))

WHITE = (255, 255, 255)


BLACK = (0, 0, 0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
BLUE = (0, 0, 255)

HEALTH_FONT = pygame.font.SysFont('comicsans', 30)


WINNER_FONT = pygame.font.SysFont('comicsans', 100)

VEL = 6
WIDTH2, HEIGHT2 = (55, 40)
WIDTH3, HEIGHT3 = (200, 200)
BULLET_VEL = 2
MAX_BULLETS = 7
FPS = 60

RED_HIT = pygame.USEREVENT + 1

BLUE_HIT = pygame.USEREVENT + 2

TREE1_HIT = pygame.USEREVENT +3

TREE2_HIT = pygame.USEREVENT +4

TREE3_HIT = pygame.USEREVENT +5

FENCE = pygame.image.load(
os.path.join('assets', '17590870-pixel-fence-and-grass-removebg-preview.png'))

FARMER = pygame.image.load(
os.path.join('assets', 'FarmerMale.webp'))

FARMER2 = pygame.image.load(
os.path.join('assets', 'Farmer2Male.webp'))

GRASS = pygame.image.load(
os.path.join('assets', 'grass_14.png'))

GRASS = pygame.transform.scale(
GRASS, (900, 500))

TREE1 = pygame.image.load(
os.path.join('assets', 'Tree.png'))

TREE1 = pygame.transform.scale(
TREE1, (200, 200))

TREE2 = pygame.image.load(
os.path.join('assets', 'Tree.png'))
TREE2 = pygame.transform.scale(
TREE2, (200, 200))

TREE3 = pygame.image.load(
os.path.join('assets', 'Tree.png'))

TREE3 = pygame.transform.scale(
TREE3, (200, 200))

def red_handle_movement(keys_pressed, red):


if keys_pressed[pygame.K_a] and red.x - VEL > -10: # LEFT
red.x -= VEL
if keys_pressed[pygame.K_d] and red.x + VEL < 830: # RIGHT
red.x += VEL
if keys_pressed[pygame.K_w] and red.y - VEL > 200: # UP
red.y -= VEL
if keys_pressed[pygame.K_s] and red.y + VEL < 420: # DOWN
red.y += VEL

def blue_handle_movement(keys_pressed, blue):


if keys_pressed[pygame.K_LEFT] and blue.x - VEL > -10: # LEFT
blue.x -= VEL
if keys_pressed[pygame.K_RIGHT] and blue.x - VEL < 830: # RIGHT
blue.x += VEL
if keys_pressed[pygame.K_UP] and blue.y - VEL > 0: # UP
blue.y -= VEL
if keys_pressed[pygame.K_DOWN] and blue.y - VEL < 80: # DOWN
blue.y += VEL

def draw_window(red, blue, red_bullets, blue_bullets, blue_health,


plant1, plant2, plant3, plant1_health, plant2_health, plant3_health):

WIN.blit(GRASS, (0, 0))

blue_health_text = HEALTH_FONT.render(
"Health: " + str(blue_health), 1, WHITE)

plant1_health_text = HEALTH_FONT.render(
"Health: " + str(plant1_health), 1, WHITE)

plant2_health_text = HEALTH_FONT.render(
"Health: " + str(plant2_health), 1, WHITE)

plant3_health_text = HEALTH_FONT.render(
"Health: " + str(plant3_health), 1, WHITE)

WIN.blit(blue_health_text, (WIDTH - blue_health_text.get_width() - 10, 10))


WIN.blit(plant1_health_text, (80, 450))
WIN.blit(plant2_health_text, (370, 450))
WIN.blit(plant3_health_text, (695, 450))

WIN.blit(FARMER, (red.x, red.y))


WIN.blit(FARMER2, (blue.x, blue.y))
WIN.blit(FENCE, (-10, 130))
WIN.blit(TREE1, (plant1.x, plant1.y))
WIN.blit(TREE2, (plant2.x, plant2.y))
WIN.blit(TREE3, (plant3.x, plant3.y))

for bullet in red_bullets:


pygame.draw.rect(WIN, RED, bullet)

for bullet in blue_bullets:


pygame.draw.rect(WIN, BLUE, bullet)

pygame.display.update()

def handle_bullets(blue_bullets, red_bullets, red, blue, TREE1, TREE2, TREE3):


for bullet in red_bullets:
bullet.y -= BULLET_VEL
if red.colliderect(bullet):
pygame.event.post(pygame.event.Event(BLUE_HIT))
red_bullets.remove(bullet)
elif bullet.y < 0:
red_bullets.remove(bullet)

for bullet in blue_bullets:


bullet.y += BULLET_VEL
if blue.colliderect(bullet):
blue_bullets.remove(bullet)

elif TREE1.colliderect(bullet):
pygame.event.post(pygame.event.Event(TREE1_HIT))
blue_bullets.remove(bullet)

elif TREE2.colliderect(bullet):
pygame.event.post(pygame.event.Event(TREE2_HIT))
blue_bullets.remove(bullet)

elif TREE3.colliderect(bullet):
pygame.event.post(pygame.event.Event(TREE3_HIT))
blue_bullets.remove(bullet)

elif bullet.y > HEIGHT:


blue_bullets.remove(bullet)

def draw_winner(text):
draw_text = WINNER_FONT.render(text, 1, WHITE)
WIN.blit(draw_text, (WIDTH/2 - draw_text.get_width() /
2, HEIGHT/2 - draw_text.get_height()/2))
pygame.display.update()
pygame.time.delay(5000)

def main():
# red = player 1, blue = player 2
red = pygame.Rect(400, 200, WIDTH2, HEIGHT2)
blue = pygame.Rect(400, 50, WIDTH2, HEIGHT2)
plant1 = pygame.Rect(60, 300, WIDTH3, HEIGHT3)
plant2 = pygame.Rect(350, 300, WIDTH3, HEIGHT3)
plant3 = pygame.Rect(675, 300, WIDTH3, HEIGHT3)

red_bullets = []
blue_bullets = []

red_health = 30
blue_health = 10
plant1_health = 10
plant2_health = 10
plant3_health = 10

clock = pygame.time.Clock()
run = True
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()

if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LCTRL and len(red_bullets) < MAX_BULLETS:
bullet = pygame.Rect(
red.x + red.width // 2 , red.y - red.height // 2 - 2, 10, 5)
red_bullets.append(bullet)
# BULLET_FIRE_SOUND.play()

if event.key == pygame.K_l and len(blue_bullets) < MAX_BULLETS:


bullet = pygame.Rect(
blue.x + blue.width, blue.y + blue.height // 2 - 2, 10, 5)
blue_bullets.append(bullet)
# BULLET_FIRE_SOUND.play()
#if event.type == RED_HIT:
#red_health -= 1

if event.type == BLUE_HIT:
blue_health -= 1

if event.type == TREE1_HIT:
plant1_health -= 1

if event.type == TREE2_HIT:
plant2_health -= 1

if event.type == TREE3_HIT:
plant3_health -= 1

if plant1_health <= 0 and plant2_health <= 0 and plant3_health <= 0:


red_health -= 1

winner_text = ""
if red_health <= 0:
winner_text = "Blue Wins!"

if blue_health <= 0:
winner_text = "Red Wins!"

if winner_text != "":
draw_winner(winner_text)
break
keys_pressed = pygame.key.get_pressed()
red_handle_movement(keys_pressed, red)
blue_handle_movement(keys_pressed, blue)

handle_bullets(blue_bullets, red_bullets, blue, red, plant1, plant2, plant3)

draw_window(red, blue, red_bullets, blue_bullets, blue_health,


plant1, plant2, plant3, plant1_health, plant2_health, plant3_health)

main()

if __name__ == "__main__":
main()

You might also like