D move u[date
parent
3cec795eb1
commit
29efd9f34b
47
domino.py
47
domino.py
|
@ -1,6 +1,7 @@
|
||||||
import pygame
|
import pygame
|
||||||
|
from time import time
|
||||||
from random import randint
|
from random import randint
|
||||||
from settings import SCREEN_HEIGHT, SCREEN_WIDTH
|
import settings as s
|
||||||
from pygame.locals import (
|
from pygame.locals import (
|
||||||
K_UP,
|
K_UP,
|
||||||
K_DOWN,
|
K_DOWN,
|
||||||
|
@ -15,20 +16,24 @@ from pygame.locals import (
|
||||||
pygame.font.init()
|
pygame.font.init()
|
||||||
my_font = pygame.font.SysFont('Calibri (Body)', 50)
|
my_font = pygame.font.SysFont('Calibri (Body)', 50)
|
||||||
|
|
||||||
|
|
||||||
# Define a Player object by extending pygame.sprite.Sprite
|
# Define a Player object by extending pygame.sprite.Sprite
|
||||||
# The surface drawn on the screen is now an attribute of 'player'
|
# The surface drawn on the screen is now an attribute of 'player'
|
||||||
class Domino(pygame.sprite.Sprite):
|
class Domino(pygame.sprite.Sprite):
|
||||||
def __init__(self, x, y):
|
def __init__(self, x, y):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.type = "domino"
|
self.type = "domino"
|
||||||
self.w = 100
|
self.w = s.DOMINO_W
|
||||||
self.h = 200
|
self.h = s.DOMINO_H
|
||||||
self.surf = pygame.Surface((self.w, self.h))
|
self.surf = pygame.Surface((self.w, self.h))
|
||||||
self.surf.fill((0, 0, 0))
|
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.rect = self.surf.get_rect(center=(x, y))
|
||||||
|
|
||||||
self.values = [randint(0, 6), randint(0, 6)]
|
self.start_pos = (x - self.w/2, y - self.h/2)
|
||||||
|
|
||||||
|
self.values = [randint(0, 6), randint(0, 6)]
|
||||||
|
|
||||||
def blit_domino(self, screen):
|
def blit_domino(self, screen):
|
||||||
screen.blit(self.surf, self.rect)
|
screen.blit(self.surf, self.rect)
|
||||||
|
@ -39,15 +44,37 @@ class Domino(pygame.sprite.Sprite):
|
||||||
(self.rect.x + (self.w / 2) - v_up.get_width(), self.rect.y + (self.h / 4) - v_up.get_height()))
|
(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())
|
# 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, (
|
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()))
|
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,
|
# screen.blit(v_up,
|
||||||
# (310, 16))
|
# (310, 16))
|
||||||
|
|
||||||
|
def update_color(self, mouse_x, mouse_y):
|
||||||
def update(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:
|
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((100, 100, 100))
|
self.surf.fill(self.select_color)
|
||||||
else:
|
else:
|
||||||
self.surf.fill((0, 0, 0))
|
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]
|
||||||
|
|
66
main.py
66
main.py
|
@ -1,6 +1,9 @@
|
||||||
import pygame
|
import pygame
|
||||||
import random
|
import random
|
||||||
from domino import Domino
|
from domino import Domino
|
||||||
|
from time import time
|
||||||
|
import settings as s
|
||||||
|
from screens import Hand_screen, Game_screen
|
||||||
|
|
||||||
from pygame.locals import (
|
from pygame.locals import (
|
||||||
K_UP,
|
K_UP,
|
||||||
|
@ -10,31 +13,46 @@ from pygame.locals import (
|
||||||
K_ESCAPE,
|
K_ESCAPE,
|
||||||
KEYDOWN,
|
KEYDOWN,
|
||||||
QUIT,
|
QUIT,
|
||||||
MOUSEBUTTONDOWN
|
MOUSEBUTTONDOWN,
|
||||||
|
MOUSEBUTTONUP
|
||||||
)
|
)
|
||||||
|
|
||||||
pygame.init()
|
pygame.init()
|
||||||
SCREEN_WIDTH = 800
|
SCREEN_WIDTH = s.SCREEN_WIDTH
|
||||||
SCREEN_HEIGHT = 600
|
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((0,0),pygame.FULLSCREEN)
|
||||||
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
|
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
|
||||||
|
|
||||||
x = 100
|
|
||||||
y = 100
|
|
||||||
all_sprites = pygame.sprite.Group()
|
all_sprites = pygame.sprite.Group()
|
||||||
dominos = pygame.sprite.Group()
|
dominos = pygame.sprite.Group()
|
||||||
for i in range(6):
|
|
||||||
|
|
||||||
|
game_screen = Game_screen()
|
||||||
|
all_sprites.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)
|
domino = Domino(x, y)
|
||||||
x += 110
|
x += DOMINO_W+10
|
||||||
|
|
||||||
dominos.add(domino)
|
dominos.add(domino)
|
||||||
all_sprites.add(domino)
|
all_sprites.add(domino)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
run = True
|
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:
|
while run:
|
||||||
|
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
|
@ -44,24 +62,44 @@ while run:
|
||||||
if event.key == K_ESCAPE:
|
if event.key == K_ESCAPE:
|
||||||
run = False
|
run = False
|
||||||
|
|
||||||
# elif event.type == MOUSEBUTTONDOWN:
|
|
||||||
|
|
||||||
|
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.
|
# Did the user click the window close button? If so, stop the loop.
|
||||||
elif event.type == QUIT:
|
elif event.type == QUIT:
|
||||||
run = False
|
run = False
|
||||||
|
|
||||||
for domino in dominos:
|
for domino in dominos:
|
||||||
domino.update(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1])
|
domino.update_color(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1])
|
||||||
|
|
||||||
|
if mouse_down:
|
||||||
|
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]]
|
||||||
|
|
||||||
|
if mouse_down == "left":
|
||||||
|
domino.update_pos(mouse_move[0], mouse_move[1], mouse_new_pos[0], mouse_new_pos[1])
|
||||||
|
|
||||||
screen.fill((255, 255, 255))
|
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)
|
||||||
|
|
||||||
|
screen.fill((230, 230, 230))
|
||||||
|
|
||||||
for entity in all_sprites:
|
for entity in all_sprites:
|
||||||
if entity.type == "domino":
|
if entity.type == "domino":
|
||||||
entity.blit_domino(screen)
|
entity.blit_domino(screen)
|
||||||
|
elif entity.type == "game_screen":
|
||||||
|
game_screen.blit_g_screen(screen)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
screen.blit(entity.surf, entity.rect)
|
screen.blit(entity.surf, entity.rect)
|
||||||
|
|
||||||
pygame.display.flip()
|
mouse_old_pos = [pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]]
|
||||||
|
|
||||||
|
pygame.display.flip()
|
||||||
|
|
46
screens.py
46
screens.py
|
@ -0,0 +1,46 @@
|
||||||
|
import pygame
|
||||||
|
import settings as s
|
||||||
|
|
||||||
|
|
||||||
|
class Hand_screen(pygame.sprite.Sprite):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
self.type = "hand_screen"
|
||||||
|
self.w = s.SCREEN_WIDTH
|
||||||
|
self.h = s.DOMINO_H * 1.5
|
||||||
|
self.color = (141, 111, 100)
|
||||||
|
self.surf = pygame.Surface((self.w, self.h))
|
||||||
|
self.surf.fill(self.color)
|
||||||
|
self.pos = (0, s.SCREEN_HEIGHT - self.h)
|
||||||
|
self.rect = self.surf.get_rect(center=(self.pos[0] + s.SCREEN_WIDTH / 2, self.pos[1] + self.h / 2))
|
||||||
|
|
||||||
|
|
||||||
|
class Empty_domino(pygame.sprite.Sprite):
|
||||||
|
def __init__(self, x, y):
|
||||||
|
super().__init__()
|
||||||
|
self.type = "domino"
|
||||||
|
self.w = s.DOMINO_W * 1.1
|
||||||
|
self.h = s.DOMINO_H * 1.1
|
||||||
|
self.surf = pygame.Surface((self.w, self.h))
|
||||||
|
self.color = (50, 50, 50)
|
||||||
|
self.surf.fill(self.color)
|
||||||
|
self.rect = self.surf.get_rect(center=(x, y))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class Game_screen(pygame.sprite.Sprite):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
self.type = "game_screen"
|
||||||
|
self.w = s.SCREEN_WIDTH * 10
|
||||||
|
self.h = s.SCREEN_HEIGHT * 10
|
||||||
|
self.color = (230, 230, 230)
|
||||||
|
self.surf = pygame.Surface((self.w, self.h))
|
||||||
|
self.surf.fill(self.color)
|
||||||
|
self.rect = self.surf.get_rect(center=(self.w / 2, self.h / 2))
|
||||||
|
|
||||||
|
self.start_d_pos = (0, 0)
|
||||||
|
self.d_edges = []
|
||||||
|
|
||||||
|
def blit_g_screen(self, screen):
|
||||||
|
screen.blit(self.surf, self.rect)
|
|
@ -1,3 +1,5 @@
|
||||||
# Define constants for the screen width and height
|
# Define constants for the screen width and height
|
||||||
SCREEN_WIDTH = 800
|
SCREEN_WIDTH = 1200
|
||||||
SCREEN_HEIGHT = 600
|
SCREEN_HEIGHT = 900
|
||||||
|
DOMINO_W = 100
|
||||||
|
DOMINO_H = 200
|
Loading…
Reference in New Issue