Mini Game Code
Mini Game Code
import os
pygame.font.init()
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))
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)
pygame.display.update()
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)
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.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
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)
main()
if __name__ == "__main__":
main()