-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
202 lines (166 loc) · 8.04 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import pygame
import random
import time
import config
from utils import *
from player import Player
from computer import Computer
from pygame.locals import (
K_ESCAPE,
K_k,
K_SPACE,
KEYDOWN,
QUIT,
)
clock = pygame.time.Clock()
screen = pygame.display.set_mode((config.SCREEN_WIDTH, config.SCREEN_HEIGHT))
pygame.display.set_caption(config.WINDOW_CAPTION)
canvas = pygame.image.load('assets/canvas.png')
# font = pygame.font.Font(config.FONTFACE, config.FONT_SIZE)
font = pygame.font.SysFont(config.FONTFACE, config.FONT_SIZE)
deckSprites = pygame.sprite.Group()
cards = createDeck()
random.shuffle(cards)
playerCards = sortCards(cards[0:13])
for card in playerCards:
deckSprites.add(card)
player0 = Player(cards=playerCards, id = 0, sprites=deckSprites)
player1 = Computer(cards=sortCards(cards[13:26]), id = 1)
player2 = Computer(cards=sortCards(cards[26:39]), id = 2)
player3 = Computer(cards=sortCards(cards[39:52]), id = 3)
players = [player0, player1, player2, player3]
for player in players:
if player.hasAceOfSpades:
firstPlayer = player.id
break
# def getInformation(player:Player):
# return "Cards in Hand: {}\nThulas Received: {}\nThulas Bestowed: {}".format(
# len(player.cards),
# player.thulaReceived,
# player.thulaGiven
# )
getInformation = lambda x: "Cards in Hand: {}\nThulas Received: {}\nThulas Bestowed: {}".format(
len(x.cards),
x.thulaReceived,
x.thulaGiven
) if x.id % 2 else "Cards in Hand: {} Thulas Received: {} Thulas Bestowed: {}".format(
len(x.cards),
x.thulaReceived,
x.thulaGiven
)
def main():
index = firstPlayer
highestCard = firstPlayer
toggleInformation = True
cardsInPlay = []
playersInPlay = []
ongoingSuit = None
isFirstTurnInGame = True
totalRounds = 0
hasRendered = False
hasGoneOffscreen = True
shouldAllowNewCards = True
thula = False
pygame.init()
clock.tick(config.FPS)
text = []
while True:
# screen.blit(canvas,(0,0))
screen.fill((148,0,211))
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
quit()
if event.key == K_k:
toggleInformation = not toggleInformation
if len(player0.cards) == 0:
quit()
if len(cardsInPlay) > 0:
isEverythingStill = all([card.isStationary() for card in cardsInPlay])
if not hasGoneOffscreen and isEverythingStill: # after round completion without thula
# this condition and loop is inserted for removing cards only after they have moved to their destination
for player in playersInPlay:
players[player].cardThrownThisTurn.target = None
if players[player].cardThrownThisTurn in players[player].cards:
players[player].cards.remove(players[player].cardThrownThisTurn)
cardsInPlay = []
playersInPlay = []
hasRendered = False # allow for rendering after cards have been moved (causes issue without this after thula)
hasGoneOffscreen = True
if toggleInformation:
for idx, player in enumerate(players): # get all the information that is to be displayed
text = getInformation(player)
blit_text(screen, text, config.TEXT_RECT[idx], font)
if len(cardsInPlay) == 0:
isEverythingStill = True
if index in playersInPlay and isEverythingStill and not thula: # if not thula and round complete
for player in playersInPlay:
players[player].cardThrownThisTurn.target = config.OFFSCREEN_RECT
highestCard = highestCard if players[highestCard].cardThrownThisTurn.rank > players[index].cardThrownThisTurn.rank else index
index = highestCard
ongoingSuit = None
hasGoneOffscreen = False
shouldAllowNewCards = True
pygame.time.wait(config.DELAY_AFTER_ROUND_NO_THULA)
# glowEdge(screen=screen, playerID=index)
if len(players[index].cards) != 0:
if isEverythingStill and hasRendered and shouldAllowNewCards and hasGoneOffscreen:
assert not index in playersInPlay, "Player {} has already played his turn".format(index)
players[index].isTurn = True
cardsInPlay.append(
players[index].getCard(ongoingSuit,isFirstTurnInGame)
)
isFirstTurnInGame = False
assert not players[index].isTurn, "Player {} has not completed their turn yet.".format(index)
playersInPlay.append(index)
players[index].cardThrownThisTurn.target = config.TARGET_RECT[index]
shouldAllowNewCards = False
# Draw it on the screen and do not do anything else until it has reached the target
renderCards(screen=screen,decks=[player0.cards, player1.cards, player2.cards, player3.cards],ongoingSuit = ongoingSuit,isFirstTurnInGame=isFirstTurnInGame,showEligible = player0.id not in playersInPlay)
hasRendered = True
if len(cardsInPlay) > 1: # if this is not the first card that thrown
isEverythingStill = all([card.isStationary() for card in cardsInPlay])
if isEverythingStill and hasGoneOffscreen:
thula = isThula([
players[index].cardThrownThisTurn,
players[playersInPlay[-2]].cardThrownThisTurn
])
if thula: # if there is a thula
pygame.time.wait(config.DELAY_AFTER_THULA)
for card in cardsInPlay:
card.target = config.DECK_RECT[highestCard]
players[highestCard].insertCards(cardsInPlay)
ongoingSuit = None
isEverythingStill = False
hasGoneOffscreen = False
index = highestCard
highestCard = None
shouldAllowNewCards = True
else: # if there's not a thula
highestCard = highestCard if players[highestCard].cardThrownThisTurn.rank > players[index].cardThrownThisTurn.rank else index
index = index + 1 if index != 3 else 0
shouldAllowNewCards = True
if len(cardsInPlay) == 1 and isEverythingStill: # if this is the first card that's thrown just do the following things
ongoingSuit = cardsInPlay[0].suit
totalRounds += 1
highestCard = index
index = index + 1 if index != 3 else 0
shouldAllowNewCards = True
# if index in playersInPlay and isEverythingStill and not thula: # if not thula and round complete
# for player in playersInPlay:
# players[player].cardThrownThisTurn.target = config.OFFSCREEN_RECT
# highestCard = highestCard if players[highestCard].cardThrownThisTurn.rank > players[index].cardThrownThisTurn.rank else index
# index = highestCard
# ongoingSuit = None
# hasGoneOffscreen = False
# shouldAllowNewCards = True
# pygame.time.wait(config.DELAY_AFTER_ROUND_NO_THULA)
else:
# if current player has no cards just pass the turn
renderCards(screen=screen,decks=[player0.cards, player1.cards, player2.cards, player3.cards],ongoingSuit = ongoingSuit,isFirstTurnInGame=isFirstTurnInGame,showEligible = player0.id not in playersInPlay)
index = index + 1 if index != 3 else 0
pygame.display.flip()
if __name__ == '__main__':
main()