-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
142 lines (113 loc) · 3.71 KB
/
game.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
from board import *
from players import *
from rules import *
from endscreen import *
import pygame, pygame.font, pygame.image, pygame.mixer, pygame.transform
from pygame.locals import *
import gui
from quit import *
from gamelog import *
import types
from endgamedialog import *
class Game:
def __init__(self, screen, images, locations, player1, player2):
self.screen = screen
self.images = images
self.locations = locations
self.player1 = player1
self.player2 = player2
def run(self):
self.gameloop()
# Graphics methods
def drawPiece(self, piece, x, y):
self.screen.blit( piece, (
int(self.locations['board']['x']) + x * piece.get_width(),
int(self.locations['board']['y']) + (5-y) * piece.get_height()))
def drawPieces(self, board):
for x in range(7):
for y in range(len(board.state[x])):
if board.state[x][y] == 1:
self.drawPiece(self.images['game']['stone1'], x, y)
elif board.state[x][y] == 2:
self.drawPiece(self.images['game']['stone2'], x, y)
def drawBoard(self, board):
self.screen.blit(self.images['game']['back'], (
int(self.locations['board']['x']),
int(self.locations['board']['y'])))
self.drawPieces(board)
self.drawGrid()
return self.screen
def drawGrid(self):
self.screen.blit(self.images['game']['grid'], (
int(self.locations['board']['x']),
int(self.locations['board']['y'])))
def doMove(self, selector_pos, stone):
board.state[selector_pos].append(stone)
return board
# Display the winner screen
def winner(self, player):
endscreen = EndScreen(self.screen, self.images, self.locations, player)
endscreen.run()
return
# The gameloop
def gameloop(self):
self.screen.blit(self.images['game']['background'], (0,0))
player1 = self.player1
player2 = self.player2
# Board generating code
board = Board()
# Board generating code
self.drawBoard(board)
pygame.display.flip()
#Can be used for logging a game
#gamelogger = GameLogger()
#gamelogger.newgame()
while 1:
while 1:
event = pygame.event.poll()
#self.chat.handleEvent(event)
if event.type is KEYDOWN and event.key is K_ESCAPE:
quitdialog = EndGameDialog(self.screen, self.images, self.locations)
if quitdialog.run() is "quit":
quit()
else:
self.drawBoard(board)
pygame.display.flip()
move = player1.doMove(board, 1, event)
if type(move) is types.IntType and rules.isMoveLegal(board, move):
board.move(move, 1)
break
self.drawBoard(board)
pygame.display.flip()
if rules.isWinner(board, 1):
player2.gameOver(move)
player1.gameOver(move)
return self.winner(1)
elif rules.isBoardFull(board):
player2.gameOver(move)
player1.gameOver(move)
return self.winner(0)
while 1:
event = pygame.event.poll()
#self.chat.handleEvent(event)
if event.type is KEYDOWN and event.key is K_ESCAPE:
quitdialog = EndGameDialog(self.screen, self.images, self.locations)
if quitdialog.run() is "quit":
quit()
else:
self.drawBoard(board)
pygame.display.flip()
move = player2.doMove(board, 2, event)
if type(move) is types.IntType and rules.isMoveLegal(board, move):
board.move(move, 2)
break
self.drawBoard(board)
pygame.display.flip()
if rules.isWinner(board, 2):
player1.gameOver(move)
player2.gameOver(move)
return self.winner(2)
elif rules.isBoardFull(board):
player1.gameOver(move)
player2.gameOver(move)
return self.winner(0)