[SOLVED] What's wrong with my code?
The click variable is meant to go up by one everytime I click the button. What do I do?. :(
Thanks in advance!
Here's the code:
from scene import *
from PIL import Image
clicks = 0
self.button = Button(Rect(self.size.w/2-100, self.size.h/2-140, 200, 200))
self.button.background = Color(0,0,0)
self.button.stroke = Color(0,0,0)
self.button.image = 'Red_Circle'
self.button.action = self.add_clicks
clicks += 1
text('Clicks: %i' % clicks, x=self.size.w/2, y=self.size.h/4*3, font_size=64)
When the code is run there is a global error on the text().
Why not in your setup method, just do self.clicks = 0. Then reference self.clicks thereafter. I had to do that before in your previous code to get it to run. No need for the global.
This should work
from scene import * #clicks = 0 class ClickScene(Scene): def setup(self): self.clicks = 0 def touch_began(self, touch): # the "global" keyword is important here: #global clicks #clicks += 1 self.clicks += 1 def draw(self): background(0, 0, 0) # clicks, changed to self.clicks below text('Clicks: %i' % self.clicks, 'Helvetica', 30, self.size.w/2, self.size.h/2, 5) run(ClickScene())
Man, stop writing in caps and pretending answers like a little kid -.-
So much for your help @filippocld
The global error in the original script is caused by the fact that the first
clickis singular (without an 's') and the second
clicksis plural (with an 's') so they are not the same variable.
Changed the question.
Add the following line as the last line of
This will make the counter update but you will have a screen refresh problem to solve. See the
draw()methods that @omz and @Phukett2 have provided above for a partial answer to your refresh problem.
I figured out how to fix the screen refresh error.
Thanks a lot everyone!