cant seem to get this code to work. It is supposed to be a card game where I draw for me then for the computer: from Button import Button import random ranks = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"] suits = ["Spades", "Hearts", "Diamonds", "Clubs"] image_filenames = ["spade.png", "heart.png", "diamond.png", "club.png"] images = [] blank_image = None player_one_suit = None player_two_suit = None player_one_rank = "-" player_two_rank = "-" game_result = "" btn = None def cut(): rank = random.choice(ranks) suit_index = random.randrange(4) return rank, suit_index def setup(): global btn, images, blank_image global player_one_rank, player_two_rank, player_one_suit, player_two_suit for i in range(len(image_filenames)): images.append(loadImage(image_filenames[i])) images[i].resize(50,50) blank_image = loadImage("blank.png") player_one_suit = blank_image player_two_suit = blank_image size(400,400) btn = Button(width /2, height*0.75, 100, 40, "You Cut") def draw(): global btn, game_reslut global player_one_rank, player_two_rank, player_one_suit, player_two_suit background(127) btn.show() textSize(70) image(player_one_suit, width/4, height/4) image(player_two_suit, width*0.75, height/4) image(player_one_rank, width*0.18, height/4+20) image(player_two_rank, width*0.68, height/4+20) textSize(30) text(game_result, width*0.5, height*0.9) def mouseClicked(): global btn if btn.collide(mouseX, mouseY): take_turn() def keyTyped(): if key == 't' or key == 'T': print("key=" + key) take_turn() def take_turn(): global btn, suits, ranks, images, blank_image, game_result global player_one_rank, player_two_rank, player_one_suit, player_two_suit if btn.text == "You Cut": rank, suit_index = cut() print(rank + ' ' + suits[suit_index]) player_two_rank = rank player_two_suit = images[suit_index] btn.text = "Computer Cuts" btn.W = 145 elif btn.text == "Computer Cuts": rank, suit_index = cut() print(rank + ' ' + suits[suit_index]) player_two_rank = rank player_two_suit = images[suit_index] btn.text = "Reset" btn.W = 60 any advice?
I cant seem to get this code to work. It is supposed to be a card game where I draw for me then for the computer:
from Button import Button
import random
ranks = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
suits = ["Spades", "Hearts", "Diamonds", "Clubs"]
image_filenames = ["spade.png", "heart.png", "diamond.png", "club.png"]
images = []
blank_image = None
player_one_suit = None
player_two_suit = None
player_one_rank = "-"
player_two_rank = "-"
game_result = ""
btn = None
def cut():
rank = random.choice(ranks)
suit_index = random.randrange(4)
return rank, suit_index
def setup():
global btn, images, blank_image
global player_one_rank, player_two_rank, player_one_suit, player_two_suit
for i in range(len(image_filenames)):
images.append(loadImage(image_filenames[i]))
images[i].resize(50,50)
blank_image = loadImage("blank.png")
player_one_suit = blank_image
player_two_suit = blank_image
size(400,400)
btn = Button(width /2, height*0.75, 100, 40, "You Cut")
def draw():
global btn, game_reslut
global player_one_rank, player_two_rank, player_one_suit, player_two_suit
background(127)
btn.show()
textSize(70)
image(player_one_suit, width/4, height/4)
image(player_two_suit, width*0.75, height/4)
image(player_one_rank, width*0.18, height/4+20)
image(player_two_rank, width*0.68, height/4+20)
textSize(30)
text(game_result, width*0.5, height*0.9)
def mouseClicked():
global btn
if btn.collide(mouseX, mouseY):
take_turn()
def keyTyped():
if key == 't' or key == 'T':
print("key=" + key)
take_turn()
def take_turn():
global btn, suits, ranks, images, blank_image, game_result
global player_one_rank, player_two_rank, player_one_suit, player_two_suit
if btn.text == "You Cut":
rank, suit_index = cut()
print(rank + ' ' + suits[suit_index])
player_two_rank = rank
player_two_suit = images[suit_index]
btn.text = "Computer Cuts"
btn.W = 145
elif btn.text == "Computer Cuts":
rank, suit_index = cut()
print(rank + ' ' + suits[suit_index])
player_two_rank = rank
player_two_suit = images[suit_index]
btn.text = "Reset"
btn.W = 60
any advice?
Step by step
Solved in 3 steps