-
Notifications
You must be signed in to change notification settings - Fork 1
/
app_pygame.py
204 lines (180 loc) · 6.89 KB
/
app_pygame.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
203
204
import sys
import pygame
from pygame.locals import K_SPACE, QUIT
from pygame_entities.pygame_controller import PyGameController
from pygame_entities.sprites import load_image, draw_text, CardSprite, EscobaSprite
WIDTH = 1024
HEIGHT = 720
class Application(object):
def __init__(self):
self.game = None
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
self.background_image = load_image('images/background.jpg')
self.cards_sprites = pygame.sprite.Group()
self.hand_sprites = pygame.sprite.Group()
self.table_sprites = pygame.sprite.Group()
def __draw_card(self, sprite, allow_click=True):
"""
generate a specific card with his properties and position
:rtype: CardSprite
"""
self.screen.blit(sprite.image, sprite.rect)
if allow_click:
self.cards_sprites.add(sprite)
return sprite
def draw_table_card(self, card, posx, posy, index):
"""
:param card: card Object
:param posx: position X
:param posy: position Y
:param index: card index
:return:
"""
sprite = CardSprite(card, posx, posy, index, True)
sprite = self.__draw_card(sprite)
self.table_sprites.add(sprite)
def draw_player_card(self, card, posx, posy, index, show_card):
"""
:param card: card Object
:param posx: position X
:param posy: position Y
:param index: card index
:param show_card: show value of card
:rtype: CardSprite
"""
sprite = CardSprite(card, posx, posy, index, show_card)
sprite = self.__draw_card(sprite, show_card)
self.hand_sprites.add(sprite)
return sprite
def draw_escoba_card(self, card, posx, posy, index):
"""
:param card: card Object
:param posx: position X
:param posy: position Y
:param index: card index
:return:
"""
sprite = EscobaSprite(card, posx, posy, index)
self.__draw_card(sprite, False)
def update_screen(self):
"""
render cards and points for each player in the screen
"""
self.screen.blit(self.background_image, (0, 0))
self.cards_sprites.empty()
self.hand_sprites.empty()
self.table_sprites.empty()
# Players Cards
number_player = 0
for player in self.game.players:
number_player += 1
if number_player == 1:
height_card = HEIGHT - 170
show_card = True
else:
height_card = 25
show_card = False
index = 0
# HAND CARDS
for card in player.hand:
self.draw_player_card(card, 350 + (index * 100), height_card, index, show_card)
index += 1
# PICKED CARDS
if player.escobas:
self.draw_escoba_card(player.escobas[-1], 150, height_card, index)
if player.cards:
self.draw_player_card(player.cards[0], 50, height_card, index, False)
# Table Cards
index = 0
for card in self.game.table:
if len(self.game.table) <= 5:
posx = 250 + (index * 100)
posy = (HEIGHT/2) - 75
else:
if index < 5:
posx = 250 + (index * 100)
posy = (HEIGHT / 2) - 150
else:
posx = 250 + ((index-5) * 100)
posy = (HEIGHT / 2) + 15
self.draw_table_card(card, posx, posy, index)
index += 1
# MESSAGES
text, position = draw_text("ROUND %d" % self.game.round, WIDTH-100, HEIGHT/2)
self.screen.blit(text, position)
text, position = draw_text("%s" % self.game.players[1].name.upper(), WIDTH-100, 50)
self.screen.blit(text, position)
text, position = draw_text("Points: %d" % self.game.players[1].points, WIDTH-100, 80)
self.screen.blit(text, position)
text, position = draw_text("%s" % self.game.players[0].name.upper(), WIDTH-100, HEIGHT-100)
self.screen.blit(text, position)
text, position = draw_text("Points: %d" % self.game.players[0].points, WIDTH-100, HEIGHT-70)
self.screen.blit(text, position)
pygame.display.flip()
def __show_end_round(self):
"""
show points for each player
"""
self.screen.blit(self.background_image, (0, 0))
text, position = draw_text("Finished Round", (WIDTH / 2), (HEIGHT / 2))
self.screen.blit(text, position)
pygame.display.flip()
pygame.time.wait(2000)
for player in self.game.players:
self.screen.blit(self.background_image, (0, 0))
text, position = draw_text("%s scored %s points" % (player.name, player.round_points),
(WIDTH / 2), (HEIGHT / 2))
self.screen.blit(text, position)
pygame.display.flip()
pygame.time.wait(2000)
def __show_winner(self):
"""
shows winner and his points on the screen
"""
self.screen.blit(self.background_image, (0, 0))
text, position = draw_text("THE WINNER IS %s WITH %s POINTS" %
(self.game.winner.name, self.game.winner.points), (WIDTH/2), HEIGHT / 2)
self.screen.blit(text, position)
text, position = draw_text("Press SPACE to restart game", (WIDTH/2), HEIGHT / 2 + 30)
self.screen.blit(text, position)
pygame.display.flip()
while True:
clock = pygame.time.Clock()
clock.tick(30)
keys = pygame.key.get_pressed()
if keys[K_SPACE]:
self.start()
for event in pygame.event.get():
if event.type == QUIT:
sys.exit(0)
def start(self):
"""
start the game
"""
pygame.display.set_caption("PYEscoba")
self.game = PyGameController(self)
self.game.add_human_player("Player1")
self.game.add_cpu_player("CPU1")
# self.game.add_cpu_player("CPU2", self) # For test 2 IA's
while not self.game.someone_win():
self.game.round += 1
self.game.generate_deck()
self.game.mix_deck()
self.game.give_cards_to_table()
while 1:
self.game.give_cards_to_players()
for x in xrange(3):
for player in self.game.players:
self.update_screen()
player.play()
self.update_screen()
if not self.game.deck:
break
self.game.update_points()
self.__show_end_round()
self.game.clear_players()
self.__show_winner()
if __name__ == '__main__':
pygame.init()
APP = Application()
APP.start()