I’m making my first game, where you click on a strawberry, then it disappears and reappears, and so on and so forth. There is a Score that increases by 10 each time you click the strawb, and a Crapscore that begins at 10. Each time the player misses the strawb, the player loses score equal to their Crapscore, then Crapscore is increased by 10.
However, I am trying to make it so that if the player has clicked on the strawb, ‘Good Shot!’ appears randomly on the screen, which works. Likewise for missing, ‘You Missed!’ appears on the screen. This worked before, but ‘You Missed!’ appears on the screen before doing anything. I tried implementing another variable to stop the words appearing unless it has been set to True. The set-to-true would be if the player has clicked yet. But this seems to only stop both lines from appearing on screen alltogether. Here’s the code for ‘Good Shot!’ and ‘You Missed!’.
def place_goodshot(): global hit global goodshotlocationrandomX global goodshotlocationrandomY global clickedyet if hit == True and clickedyet == True: goodshotlocationrandomX = randint(0, 1250) goodshotlocationrandomY = randint(0, 700) goodshotlocation = goodshotlocationrandomX, goodshotlocationrandomY screen.draw.text("Good Shot!", fontsize=60, center=goodshotlocation, color=FONT_COLOR) def place_badshot(): global hit global badshotlocationrandomX global badshotlocationrandomY global clickedyet if hit == False and clickedyet == True: badshotlocationrandomX = randint (0, 1250) badshotlocationrandomY = randint (0, 700) badshotlocation = badshotlocationrandomX, badshotlocationrandomY screen.draw.text("You Missed!", fontsize=60, center=badshotlocation, color=FONT_COLOR)
It works off a variable named ‘clickedyet’. It begins as False, and whenever the player clicks, it is updated to True, as seen in what happens when you click.
def on_mouse_down(pos): global crapscore global score global hit global clickedyet if strawberry.collidepoint(pos): clickedyet = True hit = True score = score + 10 place_strawberry() else: hit = False clickedyet = True score = score - crapscore crapscore = crapscore + 10 place_strawberry()
There is another variable named hit. It begins as False, then it switched to True if the player clicks the strawberry and False if the player missed.