127 lines
3.1 KiB
Python
127 lines
3.1 KiB
Python
import pygame
|
|
import random
|
|
from domino import Domino
|
|
from time import time
|
|
import settings as s
|
|
from screens import Hand_screen, Game_screen
|
|
|
|
from pygame.locals import (
|
|
K_UP,
|
|
K_DOWN,
|
|
K_LEFT,
|
|
K_RIGHT,
|
|
K_ESCAPE,
|
|
KEYDOWN,
|
|
QUIT,
|
|
MOUSEBUTTONDOWN,
|
|
MOUSEBUTTONUP
|
|
)
|
|
|
|
pygame.init()
|
|
SCREEN_WIDTH = s.SCREEN_WIDTH
|
|
SCREEN_HEIGHT = s.SCREEN_HEIGHT
|
|
DOMINO_W = s.DOMINO_W
|
|
DOMINO_H = s.DOMINO_H
|
|
# screen = pygame.display.set_mode((0,0),pygame.FULLSCREEN)
|
|
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
|
|
|
|
|
|
all_sprites = pygame.sprite.Group()
|
|
dominos = pygame.sprite.Group()
|
|
group_hand_screen = pygame.sprite.Group()
|
|
group_game_screen = pygame.sprite.Group()
|
|
|
|
game_screen = Game_screen()
|
|
all_sprites.add(game_screen)
|
|
group_game_screen.add(game_screen)
|
|
|
|
hand_screen = Hand_screen()
|
|
all_sprites.add(hand_screen)
|
|
|
|
|
|
|
|
|
|
x = 100
|
|
y = SCREEN_HEIGHT - DOMINO_H / 2 - 25
|
|
for i in range(6):
|
|
domino = Domino(x, y)
|
|
x += DOMINO_W+10
|
|
|
|
dominos.add(domino)
|
|
group_hand_screen.add(domino)
|
|
all_sprites.add(domino)
|
|
|
|
|
|
|
|
|
|
|
|
pygame.display.flip()
|
|
run = True
|
|
|
|
mouse_down = False
|
|
|
|
mouse_old_pos = [pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]]
|
|
r_click_cd = 0.2
|
|
r_click_time = 0
|
|
while run:
|
|
|
|
for event in pygame.event.get():
|
|
# Did the user hit a key?
|
|
if event.type == KEYDOWN:
|
|
# Was it the Escape key? If so, stop the loop.
|
|
if event.key == K_ESCAPE:
|
|
run = False
|
|
|
|
|
|
elif event.type == MOUSEBUTTONDOWN:
|
|
if event.button == 1:
|
|
mouse_down = "left"
|
|
elif event.button == 3:
|
|
mouse_down = "right"
|
|
|
|
elif event.type == MOUSEBUTTONUP:
|
|
mouse_down = False
|
|
|
|
# Did the user click the window close button? If so, stop the loop.
|
|
elif event.type == QUIT:
|
|
run = False
|
|
|
|
if mouse_down:
|
|
move = False
|
|
|
|
mouse_new_pos = pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]
|
|
mouse_move = [mouse_old_pos[0] - mouse_new_pos[0], mouse_old_pos[1] - mouse_new_pos[1]]
|
|
|
|
|
|
for domino in group_hand_screen:
|
|
domino.update_color(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1])
|
|
|
|
if mouse_down == "left":
|
|
move = domino.update_pos(move, mouse_move[0], mouse_move[1], mouse_new_pos[0], mouse_new_pos[1], hand_screen, group_hand_screen, game_screen)
|
|
|
|
elif mouse_down == "right" and time() - r_click_time >= r_click_cd:
|
|
r_click_time = domino.update_rotation(mouse_new_pos[0], mouse_new_pos[1], r_click_time)
|
|
|
|
|
|
if not move and mouse_down == "left":
|
|
game_screen.update_pos(mouse_move[0], mouse_move[1], mouse_new_pos[0], mouse_new_pos[1])
|
|
|
|
|
|
|
|
|
|
|
|
screen.fill((230, 230, 230))
|
|
|
|
for entity in all_sprites:
|
|
if entity.type == "domino":
|
|
entity.blit_domino(screen, hand_screen, group_hand_screen)
|
|
elif entity.type == "game_screen":
|
|
game_screen.blit_g_screen(screen)
|
|
|
|
else:
|
|
screen.blit(entity.surf, entity.rect)
|
|
|
|
mouse_old_pos = [pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]]
|
|
|
|
pygame.display.flip()
|