Add Domino's movement logic, game_creen movement, and domino's placemnt into game_screen
parent
29efd9f34b
commit
c77457fb98
74
domino.py
74
domino.py
|
@ -2,6 +2,7 @@ import pygame
|
|||
from time import time
|
||||
from random import randint
|
||||
import settings as s
|
||||
from screens import Empty_domino
|
||||
from pygame.locals import (
|
||||
K_UP,
|
||||
K_DOWN,
|
||||
|
@ -31,11 +32,14 @@ class Domino(pygame.sprite.Sprite):
|
|||
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.type_pos = "hand"
|
||||
|
||||
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, hand, hand_group):
|
||||
|
||||
screen.blit(self.surf, self.rect)
|
||||
|
||||
v_up = my_font.render(f"{self.values[0]}", True, (255, 255, 255))
|
||||
|
@ -63,8 +67,7 @@ class Domino(pygame.sprite.Sprite):
|
|||
return time()
|
||||
return r_click_time
|
||||
|
||||
|
||||
def update_pos(self, mouse_x, mouse_y, mouse_new_x, mouse_new_y):
|
||||
def update_pos(self, move, mouse_x, mouse_y, mouse_new_x, mouse_new_y, hand, group_hand, game):
|
||||
# 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:
|
||||
|
||||
|
@ -74,7 +77,64 @@ class Domino(pygame.sprite.Sprite):
|
|||
# self.rect.x = mouse_x
|
||||
# self.rect.y = mouse_y
|
||||
# print(self.rect.x, self.rect.y, mouse_x, mouse_y)
|
||||
return True
|
||||
|
||||
else:
|
||||
self.rect.x = self.start_pos[0]
|
||||
self.rect.y = self.start_pos[1]
|
||||
|
||||
elif self.type_pos == "hand":
|
||||
self.update_type_pos(hand, group_hand, game)
|
||||
# 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):
|
||||
if self.type_pos == "hand":
|
||||
|
||||
if not hand.rect.colliderect(self.rect):
|
||||
for empty in game.empty_group:
|
||||
if empty.rect.colliderect(self.rect):
|
||||
move = False
|
||||
if empty.pos == "up":
|
||||
if not game.value_up or game.value_up == self.values[1]:
|
||||
move = True
|
||||
|
||||
elif empty.pos == "down":
|
||||
if not game.value_down or game.value_down == self.values[0]:
|
||||
move = True
|
||||
|
||||
elif empty.pos == "both":
|
||||
move = True
|
||||
|
||||
|
||||
if move:
|
||||
self.type_pos == "game"
|
||||
group_hand.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]
|
||||
game.empty_up.rect.y -= game.empty_up.h + 10
|
||||
elif empty.pos == "down":
|
||||
game.value_down = self.values[1]
|
||||
game.empty_down.rect.y += game.empty_down.h + 10
|
||||
elif empty.pos == "both":
|
||||
game.value_up = self.values[0]
|
||||
game.value_down = self.values[1]
|
||||
|
||||
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:
|
||||
self.rect.x = self.start_pos[0]
|
||||
self.rect.y = self.start_pos[1]
|
||||
|
|
35
main.py
35
main.py
|
@ -28,14 +28,19 @@ 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):
|
||||
|
@ -43,8 +48,13 @@ for i in range(6):
|
|||
x += DOMINO_W+10
|
||||
|
||||
dominos.add(domino)
|
||||
group_hand_screen.add(domino)
|
||||
all_sprites.add(domino)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
pygame.display.flip()
|
||||
run = True
|
||||
|
||||
|
@ -76,24 +86,35 @@ while run:
|
|||
elif event.type == QUIT:
|
||||
run = False
|
||||
|
||||
for domino in dominos:
|
||||
domino.update_color(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1])
|
||||
if mouse_down:
|
||||
move = False
|
||||
|
||||
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]]
|
||||
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":
|
||||
domino.update_pos(mouse_move[0], mouse_move[1], mouse_new_pos[0], mouse_new_pos[1])
|
||||
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)
|
||||
entity.blit_domino(screen, hand_screen, group_hand_screen)
|
||||
elif entity.type == "game_screen":
|
||||
game_screen.blit_g_screen(screen)
|
||||
|
||||
|
|
46
screens.py
46
screens.py
|
@ -16,16 +16,16 @@ class Hand_screen(pygame.sprite.Sprite):
|
|||
|
||||
|
||||
class Empty_domino(pygame.sprite.Sprite):
|
||||
def __init__(self, x, y):
|
||||
def __init__(self, x, y, pos):
|
||||
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.color = (100, 10, 10)
|
||||
self.surf.fill(self.color)
|
||||
self.rect = self.surf.get_rect(center=(x, y))
|
||||
|
||||
self.rect = self.surf.get_rect(center=(x + self.w / 2, y + self.h / 2))
|
||||
self.pos = pos
|
||||
|
||||
|
||||
class Game_screen(pygame.sprite.Sprite):
|
||||
|
@ -34,13 +34,41 @@ class Game_screen(pygame.sprite.Sprite):
|
|||
self.type = "game_screen"
|
||||
self.w = s.SCREEN_WIDTH * 10
|
||||
self.h = s.SCREEN_HEIGHT * 10
|
||||
self.color = (230, 230, 230)
|
||||
self.color = (230, 0, 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.rect = self.surf.get_rect(center=(0, 0))
|
||||
|
||||
self.start_d_pos = (0, 0)
|
||||
self.d_edges = []
|
||||
self.group = pygame.sprite.Group()
|
||||
self.group.add(self)
|
||||
self.empty_group = pygame.sprite.Group()
|
||||
|
||||
empty_up = Empty_domino(s.SCREEN_WIDTH / 2, s.SCREEN_HEIGHT / 2 - (s.DOMINO_W * 1.1 * 2 + 10), "up")
|
||||
empty_down = Empty_domino(s.SCREEN_WIDTH / 2, s.SCREEN_HEIGHT / 2 + (s.DOMINO_W * 1.1 * 2 + 10), "down")
|
||||
empty_both = Empty_domino(s.SCREEN_WIDTH / 2, s.SCREEN_HEIGHT / 2, "both")
|
||||
self.emptys = [empty_up, empty_down, empty_both]
|
||||
|
||||
self.group.add(empty_both)
|
||||
self.empty_group.add(empty_both)
|
||||
|
||||
|
||||
|
||||
self.value_up = None
|
||||
self.value_down = None
|
||||
|
||||
self.empty_up = empty_both
|
||||
self.empty_down = empty_both
|
||||
|
||||
def blit_g_screen(self, screen):
|
||||
screen.blit(self.surf, self.rect)
|
||||
# add_empty()
|
||||
|
||||
for entity in self.group:
|
||||
screen.blit(entity.surf, entity.rect)
|
||||
|
||||
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:
|
||||
|
||||
for entity in self.group:
|
||||
entity.rect.x -= mouse_x
|
||||
entity.rect.y -= mouse_y
|
||||
|
|
Loading…
Reference in New Issue