import pygame
import random
pygame.init()
WIDTH = 800
HEIGHT = 600
back = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Потискаф направляется к Титанику!")
class Monster():
def __init__(self, x=0, y=0, width=10, height=10, color=None):
self.rect = pygame.Rect(x, y, width, height)
self.fill_color = back
if color:
self.fill_color = color
def color(self, new_color):
self.fill_color = new_color
def fill(self):
pygame.draw.rect(mw, self.fill_color, self.rect)
def collidepoint(self, x, y):
return self.rect.collidepoint(x, y)
def colliderect(self, rect):
return self.rect.colliderect(rect)
class Player():
def __init__(self, x=0, y=0, width=10, height=10, color=None):
self.rect = pygame.Rect(x, y, width, height)
self.fill_color = back
if color:
self.fill_color = color
def color(self, new_color):
self.fill_color = new_color
def fill(self):
pygame.draw.rect(mw, self.fill_color, self.rect)
def collidepoint(self, x, y):
return self.rect.collidepoint(x, y)
def colliderect(self, rect):
return self.rect.colliderect(rect)
player = Player("Potiskaaf", 100, 10)
monster1 = Monster("Monster1", 50, 5)
monster2 = Monster("Monster2", 75, 8)
player_size = 50
player_x = WIDTH / 2 - player_size / 2
player_y = HEIGHT - player_size
player_speed = 5
monster_size = 50
monster_x = random.randint(0, WIDTH - monster_size)
monster_y = -monster_size
monster_speed = 2
score = 0
clock = pygame.time.Clock()
game_over = False
font = pygame.font.Font(None, 36)
def show_score(x, y):
score_text = font.render(f"Score: {score}", True, WHITE)
screen.blit(score_text, (x, y))
def collision(player_x, player_y, monster_x, monster_y):
distance = ((player_x - monster_x)**2 + (player_y - monster_y)**2)**0.5
if distance < player_size / 2 + monster_size / 2:
return True
return False
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player_x -= player_speed
elif event.key == pygame.K_RIGHT:
player_x += player_speed
monster_y += monster_speed
if collision(player_x, player_y, monster_x, monster_y):
score += 1
monster_x = random.randint(0, WIDTH - monster_size)
monster_y = -monster_size
player(player_x, player_y)
monster(monster_x, monster_y)
show_score(10, 10)
pygame.display.update()
clock.tick(60)
pygame.quit()
Срочно кто может исправить ошибки уже несколо часов не могу понять где ошибка.
Ответы
Ответ:
Объяснение:
import pygame
import random
pygame.init()
WIDTH = 800
HEIGHT = 600
back = (0, 0, 0)
WHITE = (255, 255, 255)
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Потискаф направляется к Титанику!")
class Monster():
def __init__(self, name, x=0, y=0, width=10, height=10, color=None):
self.name = name
self.rect = pygame.Rect(x, y, width, height)
self.fill_color = back
if color:
self.fill_color = color
def color(self, new_color):
self.fill_color = new_color
def fill(self):
pygame.draw.rect(screen, self.fill_color, self.rect)
def collidepoint(self, x, y):
return self.rect.collidepoint(x, y)
def colliderect(self, rect):
return self.rect.colliderect(rect)
class Player():
def __init__(self, name, x=0, y=0, width=10, height=10, color=None):
self.name = name
self.rect = pygame.Rect(x, y, width, height)
self.fill_color = back
if color:
self.fill_color = color
def color(self, new_color):
self.fill_color = new_color
def fill(self):
pygame.draw.rect(screen, self.fill_color, self.rect)
def collidepoint(self, x, y):
return self.rect.collidepoint(x, y)
def colliderect(self, rect):
return self.rect.colliderect(rect)
player_size = 50
player_x = WIDTH / 2 - player_size / 2
player_y = HEIGHT - player_size
player_speed = 5
monster_size = 50
monster_x = random.randint(0, WIDTH - monster_size)
monster_y = -monster_size
monster_speed = 2
score = 0
clock = pygame.time.Clock()
game_over = False
font = pygame.font.Font(None, 36)
def show_score(x, y):
score_text = font.render(f"Score: {score}", True, WHITE)
screen.blit(score_text, (x, y))
def collision(player_x, player_y, monster_x, monster_y):
distance = ((player_x - monster_x)**2 + (player_y - monster_y)**2)**0.5
if distance < player_size / 2 + monster_size / 2:
return True
return False
# Create player and monsters objects
player = Player("Potiskaaf", player_x, player_y, player_size, player_size, WHITE)
monster1 = Monster("Monster1", monster_x, monster_y, monster_size, monster_size, RED)
monster2 = Monster("Monster2", monster_x, monster_y, monster_size, monster_size, GREEN)
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player_x -= player_speed
elif event.key == pygame.K_RIGHT:
player_x += player_speed
monster_y += monster_speed
if collision(player_x, player_y, monster_x, monster_y):
score += 1
monster_x = random.randint(0, WIDTH - monster_size)
monster_y = -monster_size
# Clear the screen
screen.fill(back)
# Draw objects on the screen
player.rect.topleft = (player_x, player_y)
player.fill()
monster1.rect.topleft = (monster_x, monster_y)
monster1.fill()
# Draw the score
show_score(10, 10)
# Update the display
pygame.display.update()
# Limit frames per second
clock.tick(60)
# Uninitialize pygame
pygame.quit()