我正在开一场二十一点游戏。不幸的是,它不能只是代码必须以图形方式显示。 所以这是我的代码(这是一个doozy):
import graphics as gr
import random
win = gr.GraphWin("Blackjack", 700, 700)
win.setCoords(0.0, 0.0, 61.0, 80.0)
#Game Title
ptitle = gr.Point(30, 77)
title = gr.Text(ptitle, "Black Jack")
title.draw(win)
#player title
playerp = gr.Point(10, 70)
player_title = gr.Text(playerp, "Player")
player_title.draw(win)
#dealer title
dealerp = gr.Point(50, 70)
dealer_title = gr.Text(dealerp, "Dealer")
dealer_title.draw(win)
#buttons
hitp = gr.Point(30, 60)
hitButton = gr.Text(hitp, "Hit")
hitButton.draw(win)
hitbox = gr.Rectangle(gr.Point(25, 55), (gr.Point(35, 65)))
hitbox.draw(win)
standTextp = gr.Text(gr.Point(30, 45), "Stand")
standTextp.draw(win)
standB = gr.Rectangle(gr.Point(25, 40), (gr.Point(35, 50)))
standB.draw(win)
newgp = gr.Point(30.5, 10)
newButton = gr.Text(newgp, "New Game")
newButton.draw(win)
newbox = gr.Rectangle(gr.Point(25.5, 5), (gr.Point(35.5, 15)))
newbox.draw(win)
#Hand Totals
handp = gr.Text(gr.Point(10, 10), "Player Hand Total:").draw(win)
dealt = gr.Text(gr.Point(50, 10), "Dealer Hand Total:").draw(win)
money_sign = gr.Text(gr.Point(27,70), "$").draw(win)
player_money = 100
money_point = gr.Text(gr.Point(30, 70), player_money)
money_point.draw(win)
ls = []
for x in range(1, 20, 10):
for y in range(20, 68, 8):
p = gr.Point(x, y)
p1 = gr.Point(x + 10, y + 8)
rect = gr.Rectangle(p, p1)
rect.draw(win)
ls.append(rect)
dealer = []
for x in range(40, 60, 10):
for y in range(20, 68, 8):
p = gr.Point(x, y)
p1 = gr.Point(x + 10, y + 8)
rect = gr.Rectangle(p, p1)
rect.draw(win)
dealer.append(rect)
#player's text markers
playertext = []
for x in range(6, 20, 10):
for y in range(23, 68, 8):
p = gr.Point(x, y)
playertext.append(p)
#dealer's text markers
dealertext = []
for x in range(45, 60, 10):
for y in range(23, 68, 8):
p = gr.Point(x, y)
dealertext.append(p)
#assemble deck
def card_deck():
# sets the card types and values
card_value = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
card_type = ['Hearts', 'Spades', 'Clubs', 'Diamonds']
deck = []
#This iterates all 52 cards into a deck
for i in card_type:
for j in card_value:
deck.append(j + ' of ' + i)
return deck
def card_value(card):
#only reading first slice to determine value of the card
if card[:1] in ('J', 'Q', 'K', '1'):
return int(10)
elif card[:1] in ('2', '3', '4', '5', '6', '7', '8', '9'):
# card[:1] example '2' out of the full '2 of Hearts' string
return int(card[:1])
elif card[:1] == 'A':
if total <= 10:
return int(11)
elif total >= 11:
return int(1)
def player_draw():
card1 = random.choice(card_deck())
return card1
players_hand = []
#starting hand
start = player_draw()
valu1 = card_value(start)
starhand = gr.Text(playertext[0], start).draw(win).setSize(10)
start = player_draw()
valu2 = card_value(start)
start2 = gr.Text(playertext[1], start).draw(win).setSize(10)
total = valu1 + valu2
hand_total = gr.Text(gr.Point(10, 6), total)
hand_total.draw(win)
#dealer's starting hand
dstart = player_draw()
valu3 = card_value(dstart)
dstarthand1 = gr.Text(dealertext[0], dstart).draw(win).setSize(12)
dstart = player_draw()
valu4 = card_value(dstart)
dstarthand2 = gr.Text(dealertext[1], dstart).draw(win).setSize(12)
total2 = valu3 + valu4
dhand_total = gr.Text(gr.Point(50, 6), total2)
dhand_total.draw(win)
#defining the buttons
def buttons():
hitp = gr.Point(30, 60)
hitButton = gr.Text(hitp, "Hit")
hitButton.draw(win)
hitbox = gr.Rectangle(gr.Point(25, 55), (gr.Point(35, 65)))
hitbox.draw(win)
standTextp = gr.Text(gr.Point(30, 45), "Stand")
standTextp.draw(win)
standB = gr.Rectangle(gr.Point(25, 40), (gr.Point(35, 50)))
standB.draw(win)
newgp = gr.Point(30.5, 10)
newButton = gr.Text(newgp, "New Game")
newButton.draw(win)
newgme = gr.Rectangle(gr.Point(25.5, 5), (gr.Point(35.5, 15)))
newgme.draw(win)
return hitbox, standB, newgme
def inside(point, rectangle):
ll = rectangle.getP1()
ur = rectangle.getP2()
return ll.getX() < point.getX() < ur.getX() and ll.getY() < point.getY() < ur.getY()
hit, stand, newgme = buttons()
s = win.getMouse()
handcount = 2
dealercount = 2
while True:
clickPoint = win.getMouse()
if clickPoint is None:
print("")
elif inside(clickPoint, hitbox):
hand_total.undraw()
playercard = player_draw()
valu5 = card_value(playercard)
point = gr.Text(playertext[handcount], playercard).draw(win).setSize(12)
total = total + valu5
hand_total = gr.Text(gr.Point(10, 6), total)
hand_total.draw(win)
handcount = handcount + 1
if total > 21:
pbust = gr.Text(gr.Point(30, 20), "Player Busts")
pbust.draw(win)
money_point.undraw()
player_money = player_money - 10
money_point = gr.Text(gr.Point(30, 70), player_money)
money_point.draw(win)
break
if 25.5 <= s.getX() <= 35.5 and 5 <= s.getY() <= 15:
continue
elif total == 21:
pwin = gr.Text(gr.Point(30, 20), "Player Wins")
pwin.draw(win)
money_point.undraw()
player_money = player_money + 10
money_point = gr.Text(gr.Point(30, 70), player_money)
money_point.draw(win)
break
if 25.5 <= s.getX() <= 35.5 and 5 <= s.getY() <= 15:
continue
elif inside(clickPoint, standB):
while total2 < 16:
dhand_total.undraw()
dealercard = player_draw()
valu6 = card_value(dealercard)
point = gr.Text(dealertext[dealercount], dealercard).draw(win).setSize(12)
total2 = total2 + valu6
dhand_total = gr.Text(gr.Point(50, 6), total2).draw(win)
dealercount = dealercount + 1
if total2 == 21 or 21 > total2 > total:
p = gr.Text(gr.Point(30, 20), "Dealer Wins")
p.draw(win)
money_point.undraw()
player_money = player_money - 10
money_point = gr.Text(gr.Point(30, 70), player_money)
money_point.draw(win)
break
if 25.5 <= s.getX() <= 35.5 and 5 <= s.getY() <= 15:
continue
elif total2 > 21:
pwin = gr.Text(gr.Point(30, 20), "Player Wins")
pwin.draw(win)
money_point.undraw()
player_money = player_money + 10
money_point = gr.Text(gr.Point(30, 70), player_money)
money_point.draw(win)
break
if 25.5 <= s.getX() <= 35.5 and 5 <= s.getY() <= 15:
continue
elif total2 == total:
ppush = gr.Text(gr.Point(30, 20), "Push")
ppush.draw(win)
break
if 25.5 <= s.getX() <= 35.5 and 5 <= s.getY() <= 15:
continue
if 21> total > total2:
pwin = gr.Text(gr.Point(30, 20), "Player Wins")
pwin.draw(win)
money_point.undraw()
player_money = player_money + 10
money_point = gr.Text(gr.Point(30, 70), player_money)
money_point.draw(win)
break
if 25.5 <= s.getX() <= 35.5 and 5 <= s.getY() <= 15:
continue
if total2 == 21 or 21 > total2 > total:
p = gr.Text(gr.Point(30, 20), "Dealer Wins")
p.draw(win)
money_point.undraw()
player_money = player_money - 10
money_point = gr.Text(gr.Point(30, 70), player_money)
money_point.draw(win)
break
if 25.5 <= s.getX() <= 35.5 and 5 <= s.getY() <= 15:
continue
win.getMouse()
win.close()
当我点击“新游戏”按钮时,我想回到第一个while循环(同时为True),以便我可以开始一个新游戏,但不会丢失在下一轮获得或丢失的钱。我怎么能这样做/我做错了什么?