I am trying to create the simplest game in python mode for my class. I have created a very very simple “whack-a-mole” type game. It generally works except when i click outside of the circle, the score updates, but just write the new score over the old one, so it look weird. Not sure how to fix this since I dont want to refresh the whole background is I dont click inside the circle.
But beyond this, I would love a critique of my code. While I am sure there are more elegant ways to write this, I want to do it with very simple concepts, as I only have limited time to introduce this project. But I also dont want to give poorly written code to the students. I hope this makes sense.
Below is the code, and thanks in advance for any guidance (also any other ideas on creating extremely simple games would be appreciated):
time2 = 2000
x = 1
y = 1
radius = 90
score = 0
def setup():
size(400, 400)
frameRate(60)
def draw():
global time2, x, y, score
currentTime = millis()
if currentTime > time2: #user did not click and 2 seconds is up
time2 = currentTime + 2000 #increase timer by 2 seconds
background(204) #delete old circle
text("score =", 330, 10) #dsiplay score title
text (score, 380, 10) #display score
x = random(0, width) #reset x variable
y = random(0, height) #reset y variable
ellipse(x, y, radius, radius) #create new circle
def mouseClicked():
global x, y, score
currentTime = millis()
d = dist(mouseX, mouseY, x, y)
if d < radius: # if clicked in circle
score += 1 #increase score by 1
background(204) #delete old circle
text("score =", 330, 10) #display score title
text (score, 380, 10) #display score
x = random(0, width) #reset x variable
y = random(0, height) #reset y variable
ellipse(x, y, radius, radius) #create new circle
else:
score -= 1
text (score, 380, 10)