我一直试图弄清楚如何在右上角和左上角的DISPLAYSURF窗口中显示pWin和dWin变量。我到底该怎么做?这是我的代码
##21 Card Game
##Wattel
##2016 Spring
import pygame, sys, random
from pygame.locals import *
from random import *
import random
from random import shuffle, randint
Green = (0,200,0)
CARDPOINTS = {'AD': 11,'AH': 11,'AC': 11,'AS': 11,
'KD': 10,'KH': 10,'KC': 10,'KS': 10,
'QD': 10,'QH': 10,'QC': 10,'QS': 10,
'JD': 10,'JH': 10,'JC': 10,'JS': 10,
'10D': 10,'10H': 10,'10C': 10,'10S': 10,
'9D': 9,'9H': 9,'9C': 9,'9S': 9,
'8D': 8,'8H': 8,'8C': 8,'8S': 8,
'7D': 7,'7H': 7,'7C': 7,'7S': 7,
'6D': 6,'6H': 6,'6C': 6,'6S': 6,
'5D': 5,'5H': 5,'5C': 5,'5S': 5,
'4D': 4,'4H': 4,'4C': 4,'4S': 4,
'3D': 3,'3H': 3,'3C': 3,'3S': 3,
'2D': 2,'2H': 2,'2C': 2,'2S': 2,}
def shuffleDeck():
List = ['AD','AH','AC','AS',
'KD','KH','KC','KS',
'QD','QH','QC','QS',
'JD','JH','JC','JS',
'10D','10H','10C','10S',
'9D','9H','9C','9S',
'8D','8H','8C','8S',
'7D','7H','7C','7S',
'6D','6H','6C','6S',
'5D','5H','5C','5S',
'4D','4H','4C','4S',
'3D','3H','3C','3S',
'2D','2H','2C','2S',]
CardPile = len(List)
random.shuffle(List)
return List
def want2play():
Hit2Play = input(" Hit 'y' to begin your game of 21): ")
print ("")
return Hit2Play
def deckCheck(DECK):
print (DECK)
print("There are:",len(DECK),"cards in the deck.")
if len(DECK) < 20:
DECK = shuffleDeck()
return DECK
else:
return DECK
def openingDeal(DECK):
PHAND = []
DHAND = []
DHANDT = []
PHAND, DECK = dealOneCard(PHAND, DECK)
DHAND, DECK = dealOneCard(DHAND, DECK)
DHANDT.append(DHAND[0])
PHAND, DECK = dealOneCard(PHAND, DECK)
DHAND, DECK = dealOneCard(DHAND, DECK)
DHANDT.append("back of card")
PPOINTS = scoreHAND(PHAND)
printScores_0(PPOINTS, PHAND, DHANDT)
return PHAND, DHAND, DHANDT, DECK
def pLoop(PHAND, DHAND, DHANDT, DECK):
while scoreHAND(PHAND) < 21:
PlayHH = input('Please hit or hold to continue: ')
if PlayHH == 'hit':
dealOneCard(PHAND, DECK)
printScores_0(scoreHAND(PHAND), PHAND, DHANDT)
elif PlayHH == 'hold':
print('Player holds')
printScores_0(scoreHAND(PHAND), PHAND, DHANDT)
break
return PHAND, DHAND, DECK
def dLoop(PHAND, DHAND, DECK):
printScores_1(scoreHAND(PHAND), scoreHAND(DHAND), PHAND, DHAND)
if scoreHAND(DHAND) < 18 or scoreHAND(DHAND) < 21 or scoreHAND(DHAND) < scoreHAND(PHAND) or scoreHAND(PHAND) < 22 or scoreHAND(DHAND) < 21:
dealOneCard(DHAND, DECK)
printScores_1(scoreHAND(PHAND), scoreHAND(DHAND), PHAND, DHAND)
return scoreHAND(PHAND), scoreHAND(DHAND), PHAND, DHAND
else:
return scoreHAND(PHAND), scoreHAND(DHAND), PHAND, DHAND
def checkScore(pWin, dWin, PPOINTS, DPOINTS):
if PPOINTS == DPOINTS and PPOINTS < 21:
dWin += 1
print (' ')
print ("Dealer Win")
return pWin, dWin
elif PPOINTS > 21:
dWin += 1
print(' ')
print("Dealer Win")
return pWin, dWin
elif DPOINTS > 21:
pWin += 1
print(' ')
print("Player Win")
return pWin, dWin
elif DPOINTS > PPOINTS and DPOINTS < 22:
dWin += 1
print (' ')
print ("Dealer Win")
return pWin, dWin
elif PPOINTS == 21 and DPOINTS == 21:
print(' ')
print("Tie")
return pWin, dWin
elif PPOINTS > 21 and DPOINTS > 21:
dWin += 1
print (' ')
print ("Dealer Win")
return pWin, dWin
else:
PPOINTS < 22 and DPOINTS > 21
pWin += 1
print (' ')
print ("Player Win")
return pWin, dWin
def dealOneCard(HAND,DECK):
theCard = DECK.pop(0)
HAND.append(theCard)
return HAND, DECK
def scoreHAND(HAND):
points = addScore(HAND)
aceCount=0
if points > 21:
aceCount += HAND.count('AS')
aceCount += HAND.count('AH')
aceCount += HAND.count('AC')
aceCount += HAND.count('AD')
while points > 21 and aceCount > 0:
points -= 10
aceCount -= 1
return points
else:
return points
def addScore(HAND):
tempScore = 0
for i in HAND:
tempScore += CARDPOINTS[i]
return tempScore
def printScores_0(POINTS, HAND, HAND1):
print("Player's cards: ", HAND, "Player's hand score: ", POINTS)
print("Dealer's cards: ", HAND1)
def printScores_1(PPOINTS, DPOINTS, PHAND, DHAND):
print("Player's cards: ", PHAND, "Player's hand score: ", PPOINTS)
print("Dealer's cards: ", DHAND, "Dealer's hand score: ", DPOINTS)
def main():
pygame.init()
gamex,gamey=(500,500)
DISPLAYSURF = pygame.display.set_mode((gamex,gamey))
DISPLAYSURF.fill(Green)
pygame.display.update()
DECK = shuffleDeck()
pWin = 0
dWin = 0
while True:
Hit2Play = want2play()
if Hit2Play == 'y':
DECK = deckCheck(DECK)
PHAND, DHAND, DHANDT, DECK = openingDeal(DECK)
PHAND, DHAND, DECK = pLoop(PHAND, DHAND, DHANDT, DECK)
PPOINTS, DPOINTS, PHAND, DHAND = dLoop(PHAND, DHAND, DECK)
pWin, dWin = checkScore(pWin, dWin, PPOINTS, DPOINTS)
print("")
print("Player's Wins:", pWin, "Dealer's Wins:",dWin)
print("")
else:
print("Player's Wins:", pWin, "Dealer's Wins:",dWin)
break
main()
答案 0 :(得分:0)
首先,我看不到任何游戏循环。您创建了曲面并仅更新了一次。一个有效的游戏循环看起来像这样:
while True:
# Event handling
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
# Game logic
# Rendering
DISPLAYSURF.fill((0, 0, 0,)) # Filling the screen with black to clear previous frame
pWin_textObj = font.render(str(pWin), True, pWin_textColor)
dWin_textObj = font.render(str(dWin), True, dWin_textColor)
DISPLAYSURF.blit(pWin_textObj, (0, 0)) # Place pWin text in upper left corner
DISPLAYSURF.blit(dWin_textObj, (500-dWin_textObj.get_width(), 0)) # Place pWin text in upper right corner
pygame.display.update()
pWin_textColor
和dWin_textColor
都是rgb元组,例如(0,0,0)或(144,100,255)。
但是,根据我所看到的代码,您的游戏基于游戏机的控制台输入,而pygame显示仅用于显示分数(?)。由于所需的游戏循环,控制台变得无法使用。为了能够实现控制台和pygame窗口的想法,您将不得不对线程或多处理进行一些研究。祝你好运。