-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplayinggame.py
More file actions
344 lines (273 loc) · 12.1 KB
/
playinggame.py
File metadata and controls
344 lines (273 loc) · 12.1 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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import shuffle
import random
import pygame
import shuffle
import config
import loadcard
import Computerplay
import singlegame
from loadcard import Card
from shuffle import UNODeck
from config import Configset
#문제 우노와 와일드 드로우 4 둘다 게임도중구현??
#추가 구현 우노 1장인데 걸리면 ->2장챙기기
# 똑같이 우노버튼으로 만들고 1장이 아닌사람이 우노 버튼 누르면 1장인 사람 2장 챙기고
#와일드 드로우 4 -> 낸사람 손에 그색 카드 없어야한다 -> 도전 / 도전자 6장, 공격자 4장
#와일드 스왑 -> 교체후 색 고르기
#게임 실행할수 있게 모듈화
def gameplay():
# 카드뽑기 함수, top에서
def drawCards(numCards):
cardsDrawn = []
for x in range(numCards):
cardsDrawn.append(unodeck.pop(0))
return cardsDrawn
# 플레이어가 플레이 가능한지 판단, 버린카드보고
def canPlay(colour, value, playerHand):
for card in playerHand:
if "Black" in card:
return True
elif colour in card or value in card:
return True
return False
# 버리는카드
discards = []
colours = ["Red", "Green", "Yellow", "Blue"]
# 플레이어
players = []
#플레이어 인원 입력받기
numPlayers = int(input("몇명?"))
while numPlayers < 2 or numPlayers > 4:
numPlayers = int(input("2명~4명으로 다시 입력하시오"))
#플레이어 점수
playerscore = []
for i in range(numPlayers):
playerscore.append(0)
# 덱만들고 섞기
unodeck = shuffle.UNODeck()
temp = unodeck.deal(numPlayers)
for hand in temp:
players.append(hand)
card = unodeck.getCards()
cf = Configset()
default = cf.getChange()
screen_width = int(default[0])
screen_height = int(default[1])
screen = pygame.display.set_mode((screen_width, screen_height))
deck = loadcard.Card('./최회민/img/BACK.png', (int(singlegame.section1_width*0.20), int(singlegame.section1_height*0.45)))
back = pygame.image.load('./최회민/img/BACK.png')
# 다음 턴
playerTurn = 0
# 시계방향1 반시계방향 -1
playDirection = 1
#게임 종료조건
playing = True
#처음 카드 1장 버리기
discards.append(unodeck.cards.pop(0))
#와일드 드로우 4인거 체크하기
while discards[0] == 'Black_Draw Four':
unodeck.append(discards.pop(0))
discards.append(unodeck.pop(0))
splitCard = discards[0].split("_", 1)
#처음 카드색
curruntcolour = splitCard[0]
if curruntcolour != "Black":
cardVal = splitCard[1]
else:
cardVal = "Any"
#승자
winner = -1
# 게임 시작할 때
user_card = []
for item in players[0]:
cards = Card(item, (120, 110))
user_card.append(cards)
i = 0
temp_list = []
for item in user_card:
item.update((50 + screen_width / 10 * i, screen_height * 0.35 / 2))
temp_list.append(item)
i += 1
user_group = pygame.sprite.renderPlain(*temp_list)
user_group.draw(screen) # 그리기
while playing:
#플레이서 손에 있는거 보여주기
#showHand(playerTurn, players[playerTurn])
# 0번 플레이어로 하고 나머지 컴퓨터로 하기
#버린 카드 top에 있는 거 보여주기
#print("Card on top of discard pile : {}".format(discards[-1]))
# 플레이 가능한지 체크
if canPlay(curruntcolour, cardVal, players[playerTurn]):
#플레이인지 체크
if players[0]:
# 플레이 가능한지 체크
cardChosen = int(input("어떤 카드 고를거니?"))
while not canPlay(curruntcolour, cardVal, [players[playerTurn][cardChosen - 1]]):
cardChosen = int(input("유효하지 않아 다시 선택해?"))
print("You played {}".format(players[playerTurn][cardChosen - 1]))
discards.append(players[playerTurn].pop(cardChosen - 1))
# 카드 낼때
sprite = user_group[cardChosen-1] # sprite는 내는 카드
user_group.remove(sprite) # 핸드에서 내려는 카드를 제거하고
for temp in players[playerTurn]: # 남아있는 카드들에 대해
temp.move(sprite.getposition()) # 제거한 카드의 위치에 대해 빈자리를 채우게 카드 이동
pygame.display.update() # 화면 업데이트
#1장 남았다면 우노 누르기
#if len(players[playerTurn]) == 1:
# if 우노버튼 누르면
# 우노버튼 안눌르면 드로두 2개
# 플레이어 이기는거 체크
if len(players[playerTurn]) == 0:
playing = False
winner = playerTurn + 1
#바로 못이기는 경우
else:
# 버린카드 특별카드 체크
splitCard = discards[0].split("_", 1)
curruntcolour = splitCard[0]
# 와일드면 카드값에 any부여
if curruntcolour == "Black":
cardVal = "Any"
else:
cardVal = splitCard[1]
# 와읻드면 색 선택하게
if curruntcolour == "Black":
newColour = Computerplay.UnoPlayer.choose_color(players[playerTurn])
curruntcolour = colours[newColour - 1]
# 리버스면 다음턴 회전반대로
if cardVal == "Rerverse":
playDirection = playDirection * (-1)
if len(players) == 2:
playerTurn += playDirection
if playerTurn == numPlayers:
playerTurn = 0
elif playerTurn < 0:
playerTurn = numPlayers - 1
# 스킵하기
elif cardVal == "Skip":
playerTurn += playDirection
if playerTurn == numPlayers:
playerTurn = 0
elif playerTurn < 0:
playerTurn = numPlayers - 1
# 2장 뽑기
elif cardVal == "Draw Two":
playerDraw = playerTurn + playDirection
if playerDraw == numPlayers:
playerDraw = 0
elif playerDraw < 0:
playerDraw = numPlayers - 1
players[playerDraw].extend(drawCards(2))
# 와일드 드로우 4
elif cardVal == "Draw Four":
playerDraw = playerTurn + playDirection
if playerDraw == numPlayers:
playerDraw = 0
elif playerDraw < 0:
playerDraw = numPlayers - 1
players[playerDraw].extend(drawCards(4))
print("")
#컴퓨터인 경우
else:
#이함수 hand넘겨받는기능추가필요
res = Computerplay.play_card(discards[0], curruntcolour, players[playerTurn])
#컴퓨터 낼수 있는경우
discards.append(res)
players[playerTurn].pop(players[playerTurn].index(res))
#컴퓨터도 카드 내서 카드 수 감소하는거 구현필요
#if len(players[playerTurn]) == 1:
#컴퓨터 우노구현
#컴퓨터 승리조건
if len(players[playerTurn]) == 0:
playing = False
winner = playerTurn + 1
else:
# 버린카드 특별카드 체크
splitCard = discards[-1].split("_", 1)
curruntcolour = splitCard[0]
# 와일드면 카드값에 any부여
if curruntcolour == "Black":
cardVal = "Any"
else:
cardVal = splitCard[1]
# 와읻드면 색 선택하게
if curruntcolour == "Black":
newColour = Computerplay.UnoPlayer.choose_color(players[playerTurn])
curruntcolour = colours[newColour - 1]
# 리버스면 다음턴 회전반대로
if cardVal == "Rerverse":
playDirection = playDirection * (-1)
if len(players) == 2:
playerTurn += playDirection
if playerTurn == numPlayers:
playerTurn = 0
elif playerTurn < 0:
playerTurn = numPlayers - 1
# 스킵하기
elif cardVal == "Skip":
playerTurn += playDirection
if playerTurn == numPlayers:
playerTurn = 0
elif playerTurn < 0:
playerTurn = numPlayers - 1
# 2장 뽑기
elif cardVal == "Draw Two":
playerDraw = playerTurn + playDirection
if playerDraw == numPlayers:
playerDraw = 0
elif playerDraw < 0:
playerDraw = numPlayers - 1
players[playerDraw].extend(drawCards(2))
# 와일드 드로우 4
elif cardVal == "Draw Four":
playerDraw = playerTurn + playDirection
if playerDraw == numPlayers:
playerDraw = 0
elif playerDraw < 0:
playerDraw = numPlayers - 1
players[playerDraw].extend(drawCards(4))
else:
# 카드선택할 수 없으면
players[playerTurn].extend(drawCards(1))
if playerTurn==0: #플레이어라면
# 카드 뽑을때
item = players[playerTurn][-1]
lastcard1 = len(user_group)
temp = Card(item, (120, 110))
if lastcard1 > 8:
x = 50 + screen_width / 10 * (lastcard1 - 8)
y = screen_height * 0.35
else:
x = 50 + screen_width / 10 * lastcard1
y = screen_height * 0.35 / 2
temp.setposition(x, y)
user_group.add(temp)
pygame.display.update() # 화면 업데이트
#else:
#컴퓨터일때 구현, 카드 드로우해서 추가하는거 구현
# 턴 이동
playerTurn += playDirection
if playerTurn == numPlayers:
playerTurn = 0
elif playerTurn < 0:
playerTurn = numPlayers - 1
#탑카드 바꾸기
pygame.display.update() # 화면 업데이트
#점수계산
#일반카드 숫자대로 / 와일드와 와일드 드로우4 50점/ 500점 나오면 전체 승리
#드로우2, 리버스, 스킵 20점
score = 0
for player in players:
for j in range(len(player)):
curruntcard = player[j].split("_", 1)
# 처음 카드색
if splitCard[0] == "Black":
score += 50
else:
if splitCard[1] == "Rerverse" or splitCard[1] == "Skip" or splitCard[1] == "Draw Two":
score += 20
else:
score += splitCard[1]
#토탈 점수 계산해 500이상이면 완전끝
playerscore[playerTurn-1] += score
return winner