import pygame
import os
[Link]()
WIDTH, HEIGHT = (900, 500)
WIN = [Link].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 = [Link]('comicsans', 30)
WINNER_FONT = [Link]('comicsans', 100)
VEL = 6
WIDTH2, HEIGHT2 = (55, 40)
WIDTH3, HEIGHT3 = (200, 200)
BULLET_VEL = 2
MAX_BULLETS = 7
FPS = 60
RED_HIT = [Link] + 1
BLUE_HIT = [Link] + 2
TREE1_HIT = [Link] +3
TREE2_HIT = [Link] +4
TREE3_HIT = [Link] +5
FENCE = [Link](
[Link]('assets', '[Link]'))
FARMER = [Link](
[Link]('assets', '[Link]'))
FARMER2 = [Link](
[Link]('assets', '[Link]'))
GRASS = [Link](
[Link]('assets', 'grass_14.png'))
GRASS = [Link](
GRASS, (900, 500))
TREE1 = [Link](
[Link]('assets', '[Link]'))
TREE1 = [Link](
TREE1, (200, 200))
TREE2 = [Link](
[Link]('assets', '[Link]'))
TREE2 = [Link](
TREE2, (200, 200))
TREE3 = [Link](
[Link]('assets', '[Link]'))
TREE3 = [Link](
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):
[Link](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)
[Link](blue_health_text, (WIDTH - blue_health_text.get_width() - 10, 10))
[Link](plant1_health_text, (80, 450))
[Link](plant2_health_text, (370, 450))
[Link](plant3_health_text, (695, 450))
[Link](FARMER, (red.x, red.y))
[Link](FARMER2, (blue.x, blue.y))
[Link](FENCE, (-10, 130))
[Link](TREE1, (plant1.x, plant1.y))
[Link](TREE2, (plant2.x, plant2.y))
[Link](TREE3, (plant3.x, plant3.y))
for bullet in red_bullets:
[Link](WIN, RED, bullet)
for bullet in blue_bullets:
[Link](WIN, BLUE, bullet)
[Link]()
def handle_bullets(blue_bullets, red_bullets, red, blue, TREE1, TREE2, TREE3):
for bullet in red_bullets:
bullet.y -= BULLET_VEL
if [Link](bullet):
[Link]([Link](BLUE_HIT))
red_bullets.remove(bullet)
elif bullet.y < 0:
red_bullets.remove(bullet)
for bullet in blue_bullets:
bullet.y += BULLET_VEL
if [Link](bullet):
blue_bullets.remove(bullet)
elif [Link](bullet):
[Link]([Link](TREE1_HIT))
blue_bullets.remove(bullet)
elif [Link](bullet):
[Link]([Link](TREE2_HIT))
blue_bullets.remove(bullet)
elif [Link](bullet):
[Link]([Link](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)
[Link](draw_text, (WIDTH/2 - draw_text.get_width() /
2, HEIGHT/2 - draw_text.get_height()/2))
[Link]()
[Link](5000)
def main():
# red = player 1, blue = player 2
red = [Link](400, 200, WIDTH2, HEIGHT2)
blue = [Link](400, 50, WIDTH2, HEIGHT2)
plant1 = [Link](60, 300, WIDTH3, HEIGHT3)
plant2 = [Link](350, 300, WIDTH3, HEIGHT3)
plant3 = [Link](675, 300, WIDTH3, HEIGHT3)
red_bullets = []
blue_bullets = []
red_health = 30
blue_health = 10
plant1_health = 10
plant2_health = 10
plant3_health = 10
clock = [Link]()
run = True
while run:
[Link](FPS)
for event in [Link]():
if [Link] == [Link]:
run = False
[Link]()
if [Link] == [Link]:
if [Link] == pygame.K_LCTRL and len(red_bullets) < MAX_BULLETS:
bullet = [Link](
red.x + [Link] // 2 , red.y - [Link] // 2 - 2, 10, 5)
red_bullets.append(bullet)
# BULLET_FIRE_SOUND.play()
if [Link] == pygame.K_l and len(blue_bullets) < MAX_BULLETS:
bullet = [Link](
blue.x + [Link], blue.y + [Link] // 2 - 2, 10, 5)
blue_bullets.append(bullet)
# BULLET_FIRE_SOUND.play()
#if [Link] == RED_HIT:
#red_health -= 1
if [Link] == BLUE_HIT:
blue_health -= 1
if [Link] == TREE1_HIT:
plant1_health -= 1
if [Link] == TREE2_HIT:
plant2_health -= 1
if [Link] == 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 = [Link].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()