forked from bymayanksingh/connect4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
165 lines (121 loc) · 4.83 KB
/
game.py
File metadata and controls
165 lines (121 loc) · 4.83 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import sys
import pygame
from pygame.locals import KEYDOWN
from config import black, blue, gray, green, red, violet, white, yellow
from connect_game import ConnectGame
from events import MouseClickEvent, MouseHoverEvent, bus
from game_data import GameData
from game_renderer import GameRenderer
c1 = red
c2 = yellow
def quit():
sys.exit()
def start():
data = GameData(c1, c2)
screen = pygame.display.set_mode(data.size)
game = ConnectGame(data, GameRenderer(screen, data))
game.print_board()
game.draw()
pygame.display.update()
pygame.time.wait(1000)
# Processes mouse and keyboard events, dispatching events to the event bus.
# The events are handled by the ConnectGame and GameRenderer classes.
while not game.game_data.game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game.quit()
if event.type == pygame.MOUSEMOTION:
bus.emit("mouse:hover", game.renderer, MouseHoverEvent(event.pos[0]))
pygame.display.update()
if event.type == pygame.MOUSEBUTTONDOWN:
bus.emit("mouse:click", game, MouseClickEvent(event.pos[0]))
if event.type == KEYDOWN:
if event.key == pygame.K_z:
mods: int = pygame.key.get_mods()
if mods & pygame.KMOD_CTRL:
bus.emit("game:undo", game)
game.update()
game.draw()
flag = 1
def color_change():
global screen, flag
screen.fill(black)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
message_display("CONNECT FOUR!!", white, 350, 150, 75)
message_display("PLAYER 1", c1, 215, 300, 35)
message_display("PLAYER 2", c2, 215, 500, 35)
button("", 100, 325, 30, 30, red, coin_change, 1)
button("", 150, 325, 30, 30, yellow, coin_change, 1)
button("", 200, 325, 30, 30, green, coin_change, 1)
button("", 250, 325, 30, 30, blue, coin_change, 1)
button("", 300, 325, 30, 30, violet, coin_change, 1)
button("", 350, 325, 30, 30, gray, coin_change, 1)
button("", 100, 525, 30, 30, red, coin_change, 2)
button("", 150, 525, 30, 30, yellow, coin_change, 2)
button("", 200, 525, 30, 30, green, coin_change, 2)
button("", 250, 525, 30, 30, blue, coin_change, 2)
button("", 300, 525, 30, 30, violet, coin_change, 2)
button("", 350, 525, 30, 30, gray, coin_change, 2)
button("SAVE", 510, 600, 130, 50, white, save)
button("SAVE", 512, 602, 125, 46, black, save)
pygame.display.update()
if flag == 0:
flag = 1
screen.fill(black)
message_display("CONNECT FOUR!!", white, 350, 150, 75)
message_display("HAVE FUN!", (23, 196, 243), 350, 300, 75)
running = False
def save():
global flag
flag = 0
def coin_change(color, player_no):
global c1, c2
if player_no == 1:
c1 = color
else:
c2 = color
def button(msg, x, y, w, h, color, action=None, player_no=0):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + w > mouse[0] > x and y + h > mouse[1] > y:
pygame.draw.rect(screen, color, (x, y, w, h))
if click[0] == 1 and action != None:
if action == coin_change:
action(color, player_no)
else:
action()
else:
pygame.draw.rect(screen, color, (x, y, w, h))
smallText = pygame.font.SysFont("monospace", 30)
textSurf, textRect = text_objects(msg, smallText, white)
textRect.center = ((x + (w / 2)), (y + (h / 2)))
screen.blit(textSurf, textRect)
def text_objects(text, font, color):
textSurface = font.render(text, True, color)
return textSurface, textSurface.get_rect()
def message_display(text, color, p, q, v):
largeText = pygame.font.SysFont("monospace", v)
TextSurf, TextRect = text_objects(text, largeText, color)
TextRect.center = (p, q)
screen.blit(TextSurf, TextRect)
pygame.init()
screen = pygame.display.set_mode(GameData().size)
pygame.display.set_caption("Connect Four | Mayank Singh")
message_display("CONNECT FOUR!!", white, 350, 150, 75)
message_display("HAVE FUN!", (23, 196, 243), 350, 300, 75)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
button("PLAY!", 150, 450, 100, 50, white, start)
button("PLAY", 152, 452, 96, 46, black, start)
button("QUIT", 450, 450, 100, 50, white, quit)
button("QUIT", 452, 452, 96, 46, black, quit)
button("COLOR CHANGE", 210, 600, 230, 50, white, color_change)
button("COLOR CHANGE", 212, 602, 225, 46, black, color_change)
pygame.display.update()