Pygame-RKG-domino/domino.py

81 lines
2.7 KiB
Python

import pygame
from time import time
from random import randint
import settings as s
from pygame.locals import (
K_UP,
K_DOWN,
K_LEFT,
K_RIGHT,
K_ESCAPE,
KEYDOWN,
QUIT,
)
pygame.font.init()
my_font = pygame.font.SysFont('Calibri (Body)', 50)
# Define a Player object by extending pygame.sprite.Sprite
# The surface drawn on the screen is now an attribute of 'player'
class Domino(pygame.sprite.Sprite):
def __init__(self, x, y):
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.start_pos = (x - self.w/2, y - self.h/2)
self.values = [randint(0, 6), randint(0, 6)]
def blit_domino(self, screen):
screen.blit(self.surf, self.rect)
v_up = my_font.render(f"{self.values[0]}", True, (255, 255, 255))
v_down = my_font.render(f"{self.values[1]}", True, (255, 255, 255))
screen.blit(v_up,
(self.rect.x + (self.w / 2) - v_up.get_width(), self.rect.y + (self.h / 4) - v_up.get_height()))
# 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(), self.rect.y + (self.h / 4) * 3 - v_down.get_height()))
# 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, mouse_x, mouse_y, mouse_new_x, mouse_new_y):
# 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)
else:
self.rect.x = self.start_pos[0]
self.rect.y = self.start_pos[1]