-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
140 lines (102 loc) · 3.99 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import pygame, sys, os, copy
from pygame.locals import *
from menu_gui import Button
from engine import *
global rec, surf
global pressed_keys, pressed_mouse, mouse_pos
pygame.init()
pygame.display.init()
FPS = 30
FramePerSec = pygame.time.Clock()
gameState = False
# Predefined some colors
BLUE = (0, 0, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
# For draw.rect has alpha value
SELECT_RED = (200, 10, 20, 50)
SELECT_GREEN = (0, 200, 20, 10)
# Screen information
SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 800
DISPLAYSURFACE = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Chess Game")
FONT = pygame.font.SysFont(None, 48)
TILE_DIMENSION = 32 # 32x32 pixels
rec = pygame.Rect(100, 100, 20, 20) # mouse collision detection
surf = pygame.Surface((rec.width, rec.height))
surf.fill((255, 0, 0))
select_rect = pygame.Rect((0, 0), (64, 64))
show_rect = False
def on_event(event):
global gameState, mouse_pos, pressed_keys, pressed_mouse
global show_rect, select_rect
pressed_keys = pygame.key.get_pressed()
pressed_mouse = pygame.mouse.get_pressed()
mouse_pos = pygame.mouse.get_pos()
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
for k, c in board.current_board.items():
click = c.clicked()
if type(click) == tuple:
select_rect.x = click[1][1][0]
select_rect.y = click[1][1][1]
print(k, click[1][0], click[1][1], board.current_board[k])
if show_rect == False:
show_rect = True
else:
show_rect = False
elif event.type == pygame.MOUSEMOTION:
pass
elif event.type == pygame.KEYDOWN:
if pressed_keys[K_q]:
if gameState == True:
gameState = False
else:
gameState = True
elif pressed_keys[K_ESCAPE]:
print(mouse_pos)
elif pressed_keys[K_LEFT]:
rec.move_ip(-10, 0)
pygame.event.pump()
if __name__ == "__main__":
board = chessBoard()
boardState = board.starting_board
# menu background image
img = pygame.image.load(os.path.join(os.getcwd(), "assets",
"chess-1280.jpg")).convert()
img = pygame.transform.scale(img, (SCREEN_WIDTH, SCREEN_HEIGHT))
btn1 = Button(DISPLAYSURFACE, pos = (SCREEN_WIDTH/2, SCREEN_HEIGHT/2),
text="Jugar", font=FONT)
btn1.x = SCREEN_WIDTH/2 - btn1.size[0]/2 # centers button
btn1.y = SCREEN_HEIGHT/2 - btn1.size[1]/2
title_font = pygame.font.SysFont(None, 100)
for keys, content in board.starting_board.items(): # fill the board with chessPiece object
board.current_board[keys] = chessPiece(content)
while True:
if gameState == False: # When the game is paused, i.e.: in the start menu
DISPLAYSURFACE.blit(img, (0, 0))
btn1.render()
if btn1.click_button() == True:
gameState = True
f = title_font.render("pyChess", True, WHITE)
DISPLAYSURFACE.blit(f, (SCREEN_WIDTH/2 - f.get_width()/2, SCREEN_HEIGHT/4))
elif gameState == True:
DISPLAYSURFACE.fill((0, 0, 0))
board.render(DISPLAYSURFACE)
if show_rect:
pygame.draw.rect(DISPLAYSURFACE, SELECT_GREEN, select_rect)
for keys, content in board.starting_board.items():
board.current_board[keys].render(DISPLAYSURFACE)
_rec = board.current_board['A1'].rect
for event in pygame.event.get():
on_event(event)
rec.x = mouse_pos[0] - 10
rec.y = mouse_pos[1] - 10
DISPLAYSURFACE.blit(surf, (rec.x, rec.y))
pygame.display.update()
FramePerSec.tick(FPS)