173 lines
7.0 KiB
Python
173 lines
7.0 KiB
Python
import pygame
|
||
from time import time
|
||
from random import randint
|
||
import settings as s
|
||
from screens import Empty_domino
|
||
|
||
pygame.font.init()
|
||
my_font = pygame.font.SysFont('Calibri (Body)', 37)
|
||
|
||
|
||
class Domino(pygame.sprite.Sprite):
|
||
def __init__(self, x, y, values, index):
|
||
super().__init__()
|
||
self.type = "domino"
|
||
self.w = s.DOMINO_W
|
||
self.h = s.DOMINO_H
|
||
self.surf = pygame.Surface((self.w, self.h))
|
||
self.color = (0, 0, 0)
|
||
self.select_color = (100, 100, 100)
|
||
self.surf.fill(self.color)
|
||
self.rect = self.surf.get_rect(center=(x, y))
|
||
self.index = index
|
||
self.lives = 3
|
||
|
||
self.type_pos = "hand"
|
||
|
||
self.start_pos = [x - self.w / 2, y - self.h / 2]
|
||
|
||
self.values = values
|
||
|
||
self.damage = True
|
||
|
||
def blit_domino(self, screen, dominos):
|
||
|
||
movement = self.w + 10
|
||
i = 0
|
||
# передвигаем домино влево, если есть пустые места
|
||
for domino in dominos:
|
||
if i == 0:
|
||
|
||
domino.start_pos[0] -= domino.index * movement
|
||
domino.index = 0
|
||
|
||
else:
|
||
domino.start_pos[0] -= (domino.index - i) * movement
|
||
domino.index = i
|
||
|
||
i += 1
|
||
|
||
# блит домино с текстом
|
||
screen.blit(self.surf, self.rect)
|
||
|
||
v_up = my_font.render(f"{self.values[0][1]}", True, (255, 255, 255))
|
||
v_down = my_font.render(f"{self.values[1][1]}", True, (255, 255, 255))
|
||
# screen.blit(v_up,
|
||
# (self.rect.x + (self.w / 2), self.rect.y + (self.h / 4)))
|
||
|
||
screen.blit(v_up,
|
||
(self.rect.x + (self.w / 2) - v_up.get_width() / 2,
|
||
self.rect.y + (self.h / 4) - v_up.get_height() / 2))
|
||
|
||
# print(self.rect.x + (self.w / 2) - v_up.get_width(), self.rect.y + (self.h / 4) - v_up.get_height())
|
||
screen.blit(v_down, (
|
||
self.rect.x + (self.w / 2) - v_down.get_width() / 2,
|
||
self.rect.y + (self.h / 4) * 3 - v_down.get_height() / 2))
|
||
# screen.blit(v_up,
|
||
# (310, 16))
|
||
|
||
def update_color(self, mouse_x, mouse_y):
|
||
# check if mouse colide with domino
|
||
if self.rect.x <= mouse_x <= self.rect.x + self.w and self.rect.y <= mouse_y <= self.rect.y + self.h:
|
||
self.surf.fill(self.select_color)
|
||
else:
|
||
self.surf.fill(self.color)
|
||
|
||
def update_rotation(self, mouse_x, mouse_y, r_click_time):
|
||
# если клинкули правым кликом - переворачиваем домино
|
||
if self.rect.x <= mouse_x <= self.rect.x + self.w and self.rect.y <= mouse_y <= self.rect.y + self.h:
|
||
# pygame.transform.rotate(self.surf, 90)
|
||
self.values = self.values[::-1]
|
||
|
||
return time()
|
||
return r_click_time
|
||
|
||
def update_pos(self, move, mouse_x, mouse_y, mouse_new_x, mouse_new_y, hand, group_hand, game, dominos):
|
||
# check if mouse colide with domino
|
||
if self.rect.x <= mouse_new_x <= self.rect.x + self.w and self.rect.y <= mouse_new_y <= self.rect.y + self.h:
|
||
|
||
# передвижение вместе с мышью
|
||
self.rect.x -= mouse_x
|
||
self.rect.y -= mouse_y
|
||
|
||
# self.rect.x = mouse_x
|
||
# self.rect.y = mouse_y
|
||
# print(self.rect.x, self.rect.y, mouse_x, mouse_y)
|
||
return True
|
||
|
||
|
||
elif self.type_pos == "hand":
|
||
# проверяем, не находится ли домино за пределами руки (выложили ли домино на стол по правилам?)
|
||
self.update_type_pos(hand, group_hand, game, dominos)
|
||
# if self.type_pos == "hand":
|
||
# self.rect.x = self.start_pos[0]
|
||
# self.rect.y = self.start_pos[1]
|
||
return move
|
||
|
||
def update_type_pos(self, hand, group_hand, game, dominos):
|
||
if self.type_pos == "hand":
|
||
|
||
# если домино за пределами руки
|
||
if not hand.rect.colliderect(self.rect):
|
||
move = False
|
||
|
||
# проверяем колизию с пустышками (красными), чтобы на их место поставить домино
|
||
for empty in game.empty_group:
|
||
# ставим
|
||
if empty.rect.colliderect(self.rect):
|
||
if empty.pos == "up":
|
||
if not game.value_up or game.value_up == self.values[1][0]:
|
||
move = True
|
||
|
||
elif empty.pos == "down":
|
||
if not game.value_down or game.value_down == self.values[0][0]:
|
||
move = True
|
||
|
||
elif empty.pos == "both":
|
||
move = True
|
||
|
||
# ставим домино на место пустышки, и добавляем в группу объектов игрового стола
|
||
if move:
|
||
self.surf.fill(self.color)
|
||
self.type_pos == "game"
|
||
group_hand.remove(self)
|
||
dominos.remove(self)
|
||
game.group.add(self)
|
||
|
||
self.rect.x = empty.rect.x
|
||
self.rect.y = empty.rect.y
|
||
|
||
if empty.pos == "up":
|
||
game.value_up = self.values[0][0]
|
||
game.empty_up.rect.y -= game.empty_up.h + 10
|
||
elif empty.pos == "down":
|
||
game.value_down = self.values[1][0]
|
||
game.empty_down.rect.y += game.empty_down.h + 10
|
||
elif empty.pos == "both":
|
||
game.value_up = self.values[0][0]
|
||
game.value_down = self.values[1][0]
|
||
|
||
game.empty_up = Empty_domino(empty.rect.x, empty.rect.y - (s.DOMINO_W * 1.1 * 2 + 10),
|
||
"up")
|
||
game.group.add(game.empty_up)
|
||
game.empty_group.add(game.empty_up)
|
||
|
||
game.empty_down = Empty_domino(empty.rect.x, empty.rect.y + (s.DOMINO_W * 1.1 * 2 + 10),
|
||
"down")
|
||
game.group.add(game.empty_down)
|
||
game.empty_group.add(game.empty_down)
|
||
|
||
empty.kill()
|
||
|
||
else:
|
||
if self.damage:
|
||
game.lives -= 1
|
||
self.damage = False
|
||
|
||
|
||
|
||
# если домино не за пределами руки - возвращаем её на место
|
||
else:
|
||
self.rect.x = self.start_pos[0]
|
||
self.rect.y = self.start_pos[1]
|