Help on creating very simple game in python mode

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)

Hi @givetosurfrider,

even if you want to keep it as simple as possible, consider not showing your students code with duplicate code blocks if it is not required (which actually is never) :slight_smile:
I would also using myself some other, more object oriented approach for it, but assuming your student are at the very beginning of learning to code.

So I made a possible example without using any advanced technics below with smaller adjustments.

Hope that helps …

timer  = 2000
x      = 0
y      = 0
radius = 45
score  = 0
tried  = False 

def setup():
    global x, y
    size(400, 400)
    frameRate(60)
    x = random(radius, width-radius)  # init x variable
    y = random(radius, height-radius) # init y variable    
    
def draw():
    global x, y, timer, tried
    background(0)    
    currentTime = millis()
    if tried or currentTime > timer:        # user did tried or not click and 2 seconds is up
        timer = currentTime + 2000          # reset timer
        x = random(radius, width-radius)    # reset x variable
        y = random(radius, height-radius)   # reset y variable
        tried = False                       # reset try state
    text("score = " + str(score), 330, 10)  # display score title
    circle(x, y, radius*2)                  # display circle

def mouseClicked():
    global tried, score
    tried = True
    d = dist(mouseX, mouseY, x, y)    
    if d < radius: # if clicked inside circle
        score += 1 # increase score
    else:          # if clicked outside circle
        score -= 1 # decrease score

PS: Actually, I’m usually not using processing python mode at all, even if I’m using python for my real work quite a lot, so I was wondering very much about, that processing python mode is still based on py 2.7. :open_mouth:

1 Like

This is so incredibly helpful. I can’t thank you enough. I ended up using this code, and building on it for the class. Thank you for taking the time to do this, I really appreciate it!

1 Like