-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquoridor2.py
160 lines (147 loc) · 5.3 KB
/
quoridor2.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import pygame
from pygame.locals import *
from sys import exit
import random
class Bar(pygame.sprite.Sprite):
def __init__(self, width, height,row,column):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((width,height)).convert()
self.image.fill((0,0,0))
self.rect = self.image.get_rect()
self.column = column
self.row = row
def update(self,mouse):
if num_bar_clicked == 0:
if self.rect.collidepoint(mouse):
self.image.fill((255,0,0))
if self.rect.width > self.rect.height:
hor_redbar_list.add(self)
elif self.rect.height > self.rect.width:
vert_redbar_list.add(self)
elif num_bar_clicked == 1:
if self.rect.collidepoint(mouse) and self.rect.width > self.rect.height:
for bar in hor_redbar_list:
if bar.row == self.row and abs(bar.column - self.column) == 1:
self.image.fill((255,0,0))
elif self.rect.collidepoint(mouse) and self.rect.width < self.rect.height:
for bar in vert_redbar_list:
if bar.column == self.column and abs(bar.row - self.column) == 1:
self.image.fill((255,0,0))
class Player(pygame.sprite.Sprite):
def __init__(self,image,row,column):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image)
self.rect = self.image.get_rect()
self.column = column
self.row = row
self.update_position()
def update_position(self):
self.rect.y = self.row*50 + 20
self.rect.x = self.column*50 + 23
def update(self,mouse):
move_blocked = 0
if self.rect.collidepoint(mouse):
if event.key == K_RIGHT:
for bar in vert_redbar_list:
if bar.row == self.row and bar.column == self.column:
move_blocked = 1
if move_blocked == 0: self.column += 1
if self.column >= 8: self.column = 8
elif event.key == K_UP:
for bar in hor_redbar_list:
if bar.row == self.row - 1 and bar.column == self.column:
move_blocked = 1
if move_blocked == 0: self.row -= 1
if self.row < 0: self.row = 0
elif event.key == K_DOWN:
for bar in hor_redbar_list:
if bar.row == self.row and bar.column == self.column:
move_blocked = 1
if move_blocked == 0: self.row += 1
if self.row >= 8: self.row = 8
elif event.key == K_LEFT:
for bar in vert_redbar_list:
if bar.row == self.row and bar.column == self.column - 1:
move_blocked = 1
if move_blocked == 0: self.column -= 1
if self.column <= 0: self.rect.x = 0
self.update_position()
pygame.init()
screen = pygame.display.set_mode([470,470])
pygame.display.set_caption("Quoridor")
#create background
back = pygame.Surface((470,470))
background = back.convert()
background.fill((255,255,255))
bar_list = pygame.sprite.Group()
hor_bar_list = pygame.sprite.Group()
vert_bar_list = pygame.sprite.Group()
hor_redbar_list = pygame.sprite.Group()
vert_redbar_list = pygame.sprite.Group()
player1_group = pygame.sprite.Group()
player2_group = pygame.sprite.Group()
player_list = pygame.sprite.Group()
all_sprites_list = pygame.sprite.Group()
bar_coord_x = [60,110,160,210,260,310,360,410]
bar_coord = bar_coord_x
bar_coord_y = [10,60,110,160,210,260,310,360,410]
#create grid of bars
for row in range(0,9):
for column in range(0,8):
bar_vert = Bar(5, 50, row, column)
bar_vert.rect.x = bar_coord_x[column]
bar_vert.rect.y = bar_coord_y[row]
vert_bar_list.add(bar_vert)
bar_list.add(bar_vert)
all_sprites_list.add(bar_vert)
for row in range(0,8):
for column in range(0,9):
bar_hor = Bar(50,5, row, column)
bar_hor.rect.x = bar_coord_y[column]
bar_hor.rect.y = bar_coord_x[row]
hor_bar_list.add(bar_hor)
bar_list.add(bar_hor)
all_sprites_list.add(bar_hor)
#create players
player1 = Player('piece1.png',0,4)
player1_group.add(player1)
player_list.add(player1)
all_sprites_list.add(player1)
player2 = Player('piece2.png',8,4)
player2_group.add(player2)
player_list.add(player2)
all_sprites_list.add(player2)
num_bar_clicked= 0
p1_turn = 1
num_moves = 0
#loop through game
while 1:
mouse = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == QUIT:
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
bar_list.update(mouse)
if num_bar_clicked == 0:
num_moves +=1
num_bar_clicked = 1
else: num_bar_clicked = 0
if event.type == pygame.KEYDOWN:
if p1_turn == 1:
player1_group.update(mouse)
p1_turn = 0
num_moves += 1
elif p1_turn == 0:
player2_group.update(mouse)
p1_turn = 1
num_moves += 1
screen.blit(background,(0,0))
pygame.draw.rect(screen,(0,0,0),Rect((10,10),(450,450)),5)
all_sprites_list.draw(screen)
pygame.display.update()
# TODO
# double click bar = black
# piece cannot move through red walls (up not working?)
# more contraints than double clicking
# limit blocks/player (list of each player's block)
# check if legal move is actually illegal b/c blocks off all paths