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)
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("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
player = Player ("Potiskaaf",player_x,player_y, player_size, player_size, WHITE)
monster1 = Monster("Monster", 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
screen.fill(back)
player.rect.topleft = (player_x, player_y)
player.fill()
monster1.rect.topleft = (monster_x,monster_y)
monster1.fill()
show_score(10, 10)
pygame.display.update()
clock.tick(30)
pygame.quit()
очень срочно программа не работает. Нужно что бы игра заработала.
Ответы
Я думаю это должно заработать я исправил некоторые ваши ошибки в коде:
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)
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
player = Player ("Potiskaaf",player_x,player_y, player_size, player_size, WHITE)
monster1 = Monster("Monster", 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