Код:
import pygame
import random
initialize pygame
pygame.init()
create the screen
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
set the title of the game
pygame.display.set_caption("Herbivorous and Carnivorous Squares")
set the background color
bg_color = (255, 255, 255)
set the font for the score
font = pygame.font.SysFont(None, 30)
create the herbivore square
herbivore_size = 50
herbivore_rect = pygame.Rect(random.randint(0, screen_width - herbivore_size),
random.randint(0, screen_height - herbivore_size),
herbivore_size, herbivore_size)
herbivore_color = (50, 205, 50)
create the carnivore square
carnivore_size = 50
carnivore_rect = pygame.Rect(random.randint(0, screen_width - carnivore_size),
random.randint(0, screen_height - carnivore_size),
carnivore_size, carnivore_size)
carnivore_color = (255, 0, 0)
create the food items
food_size = 10
food_color = (50, 205, 50)
foods = []
for _ in range(10):
food_rect = pygame.Rect(random.randint(0, screen_width - food_size),
random.randint(0, screen_height - food_size),
food_size, food_size)
foods.append(food_rect)
set the speed of the squares
herbivore_speed = 5
carnivore_speed = 10
set the score
score = 0
running = True
while running:
# handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
code
# update the herbivore square
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and herbivore_rect.x > 0:
herbivore_rect.x -= herbivore_speed
elif keys[pygame.K_RIGHT] and herbivore_rect.x < screen_width - herbivore_size:
herbivore_rect.x += herbivore_speed
elif keys[pygame.K_UP] and herbivore_rect.y > 0:
herbivore_rect.y -= herbivore_speed
elif keys[pygame.K_DOWN] and herbivore_rect.y < screen_height - herbivore_size:
herbivore_rect.y += herbivore_speed
# update the carnivore square
if herbivore_rect.x < carnivore_rect.x:
carnivore_rect.x -= carnivore_speed
elif herbivore_rect.x > carnivore_rect.x:
carnivore_rect.x += carnivore_speed
elif herbivore_rect.y < carnivore_rect.y:
carnivore_rect.y -= carnivore_speed
elif herbivore_rect.y > carnivore_rect.y:
carnivore_rect.y += carnivore_speed
# check if the carnivore square eats the herbivore square
if carnivore_rect.colliderect(herbivore_rect):
running = False
print("Game Over! Carnivorous square ate herbivorous square")
# check if the herbivorous square eats a food item
for food in foods:
if herbivore_rect.colliderect(food):
foods.remove(food)
score += 1
# draw the screen
screen.fill(bg_color)
# draw the herbivorous square
pygame.draw.rect(screen, herbivore_color, herbivore_rect)
# draw the carnivorous square
pygame.draw.rect(screen, carnivore_color, carnivore_rect)
# draw the food items
for food in foods:
pygame.draw.circle(screen, food_color, (food.x + food_size // 2, food.y + food_size // 2), food_size // 2)
# draw the score
score_text = font.render("Score: {}".format(score), True, (0, 0, 0))
screen.blit(score_text, (10, 10))
pygame.display.update()
quit pygame
pygame.quit()