I am trying to create a game and i have to make this plexus made of buttons. Every time the player clicks the button, it should appear a small circle inside the pressed button. I tried to make a draw_circle function but when i place it on the action variable inside the buttons, I get this errror. Global name x is not defined.
import pygame
pygame.init()
display_width = 900
display_height = 600
black = (0,0,0)
white = (255,255,255)
red = (250,0,0)
green = (0,250,0)
bright_red =(200,0,0)
bright_green = (0,200,0)
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("A MATH'S GAME")
def draw_circle():
pygame.draw.circle(gameDisplay,black,(x,y),100)
def button(msg,x,y,w,h,ic,ac,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(gameDisplay, ac,(x,y,w,h))
if click[0] == 1 and action != None:
action()
else:
pygame.draw.rect(gameDisplay, ic,(x,y,w,h))
smallText = pygame.font.SysFont("comicsansms",20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
gameDisplay.blit(textSurf, textRect)
def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def messgae_display(text):
largeText = pygame.font.Font("Arial",60)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
pygame.display.update()
def quitgame():
pygame.quit()
quit()
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(white)
largeText = pygame.font.SysFont("comicsansms",115)
TextSurf, TextRect = text_objects("A math's game", largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
button("GO!",150,450,100,50,green,bright_green,draw_circle)
button("Quit",550,450,100,50,red,bright_red,quitgame)
pygame.display.update()
def game_loop():
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(white)
largeText = pygame.font.SysFont("comicsansms",50)
TextSurf, TextRect = text_objects("A math's game", largeText)
TextRect.center = ((display_width/2),100)
gameDisplay.blit(TextSurf, TextRect)
button(None,200,250,50,50,bright_red,red,None)
button(None,200,300,50,50,bright_red,red,None)
button(None,200,350,50,50,bright_red,red,None)
button(None,200,400,50,50,bright_red,red,None)
button(None,200,450,50,50,bright_red,red,None)
button(None,200,500,50,50,bright_red,red,None)
button(None,250,250,50,50,bright_red,red,None)
button(None,250,300,50,50,bright_red,red,None)
button(None,250,350,50,50,bright_red,red,None)
button(None,250,400,50,50,bright_red,red,None)
button(None,250,450,50,50,bright_red,red,None)
button(None,250,500,50,50,bright_red,red,None)
button(None,300,250,50,50,bright_red,red,None)
button(None,300,300,50,50,bright_red,red,None)
button(None,300,350,50,50,bright_red,red,None)
button(None,300,400,50,50,bright_red,red,None)
button(None,300,450,50,50,bright_red,red,None)
button(None,300,500,50,50,bright_red,red,None)
button(None,350,250,50,50,bright_red,red,None)
button(None,350,300,50,50,bright_red,red,None)
button(None,350,350,50,50,bright_red,red,None)
button(None,350,400,50,50,bright_red,red,None)
button(None,350,450,50,50,bright_red,red,None)
button(None,350,500,50,50,bright_red,red,None)
button(None,400,250,50,50,bright_red,red,None)
button(None,400,300,50,50,bright_red,red,None)
button(None,400,350,50,50,bright_red,red,None)
button(None,400,400,50,50,bright_red,red,None)
button(None,400,450,50,50,bright_red,red,None)
button(None,400,500,50,50,bright_red,red,None)
button(None,450,250,50,50,bright_red,red,None)
button(None,450,300,50,50,bright_red,red,None)
button(None,450,350,50,50,bright_red,red,None)
button(None,450,400,50,50,bright_red,red,None)
button(None,450,450,50,50,bright_red,red,None)
button(None,450,500,50,50,bright_red,red,None)
button(None,500,250,50,50,bright_red,red,None)
button(None,500,300,50,50,bright_red,red,None)
button(None,500,350,50,50,bright_red,red,None)
button(None,500,400,50,50,bright_red,red,None)
button(None,500,450,50,50,bright_red,red,None)
button(None,500,500,50,50,bright_red,red,None)
button(None,550,250,50,50,bright_red,red,None)
button(None,550,300,50,50,bright_red,red,None)
button(None,550,350,50,50,bright_red,red,None)
button(None,550,400,50,50,bright_red,red,None)
button(None,550,450,50,50,bright_red,red,None)
button(None,550,500,50,50,bright_red,red,None)
button(None,600,250,50,50,bright_red,red,None)
button(None,600,300,50,50,bright_red,red,None)
button(None,600,350,50,50,bright_red,red,None)
button(None,600,400,50,50,bright_red,red,None)
button(None,600,450,50,50,bright_red,red,None)
button(None,600,500,50,50,bright_red,red,None)
button(None,650,250,50,50,bright_red,red,None)
button(None,650,300,50,50,bright_red,red,None)
button(None,650,350,50,50,bright_red,red,None)
button(None,650,400,50,50,bright_red,red,None)
button(None,650,450,50,50,bright_red,red,None)
button(None,650,500,50,50,bright_red,red,None)
pygame.display.update()
pygame.init()
game_intro()
In the line button("GO!",150,450,100,50,green,bright_green,draw_circle) you aren't giving draw_circle a value for x or y, so they are, in fact, not defined. You need to pass these down to to the draw_circle function.
Inside of your button function, pass the arguments down to draw_circle like:
if click[0] == 1 and action != None:
action(x, y)
I am trying to make a game in pygame where you need to click the image to gain a score.My program works but i still need to fix 2 bugs:
Bug 1
When the user presses 'c' that is play again,the **time does not reset back to 0. I want it back to 0 , so that every 10 seconds the game gets over and you cant play it anymore.
Bug 2
The time starts even if the user does not press 'c' , i.e to play game.So by the time user reads the rules (assuming they took 5 seconds to read rules) , and then they press 'c' they only have 5 more seconds to play.I want it to do it so that only when the user presses 'c' the game time starts.
This is my game run up source code:
def runGame():
#Initial score is 0
score=0
gameExit = False
gameOver = False
while not gameExit:
while gameOver == True:
#Game Over message
gameDisplay.fill(black)
message_to_screen("Game over",
red,
y_displace=-50,
size="large")
message_to_screen("Press C to play again or Q to quit.",
white,
y_displace=50,
size="medium")
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
gameExit = True
gameOver = False
if event.key == pygame.K_c: #If user press 'c' , game starts
gameLoop()
gameDisplay.fill((white))
#Possible Error from here
# ~~~~~~~~~~~~~~~
#My time function
time=pygame.time.get_ticks()/1000
if time==11: #Checks if time hit 10 (i took as 11 as it should complete the 10th second)
global time
global score
score=0
time=0
gameOver=True
#~~~~~~~~~~~~~ Till here
for event in pygame.event.get():
if event.type==pygame.QUIT:
running=False
pygame.quit()
quit()
if event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT:
# Set the x, y postions of the mouse click
x, y = event.pos
if foodrect.collidepoint(x, y):
foodrect.center=(random.randint(5,1060),random.randint(5,700))
print "New Position: ",foodrect.center
score+=1
continue
gameDisplay.blit(foodimg,foodrect)
showtime=font.render("Time: "+str(time),0,(0,0,0))
gameDisplay.blit(showtime,(950,10))
scoredisplay(score)
pygame.display.update()
My Game Loop
def gameLoop():
clock.tick(FPS)
runGame()
pygame.quit()
quit()
gameLoop()
Full Code
As requested by Herman Yanush.
import pygame
import time
import random
pygame.init()
foodimg=pygame.image.load("food.png")
foodrect=foodimg.get_rect()
#Colours
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
LEFT =1
#Game Display
display_width = 1080
display_height = 720
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Click It! ~ Snapnel Productions')
gameDisplay.fill((white))
font=pygame.font.SysFont("Arial",30)
#Clock
clock = pygame.time.Clock()
FPS = 30
cellSize=10
#Font Size
exsmallfont = pygame.font.SysFont("comicsansms", 17)
smallfont = pygame.font.SysFont("comicsansms", 25)
medfont = pygame.font.SysFont("comicsansms", 50)
largefont = pygame.font.SysFont("comicsansms", 80)
#The score function - displays the score on top right
def scoredisplay(scoredef=0):
text=smallfont.render("Score :%s" %(scoredef) ,True ,white)
gameDisplay.blit(text,[0,0])
def scoredisplay1(musicname):
text=exsmallfont.render("Now Playing :%s" %(musicname) ,True ,white)
gameDisplay.blit(text,[690,5])
#Starting Of the game
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key ==pygame.K_c:
intro = False
if event.key ==pygame.K_q:
pygame.quit()
quit()
gameDisplay.fill(black)
#Game Initial display message
message_to_screen("Click It!",
red,
-200,
size="large")
message_to_screen("This Game is created by Shrapnel",
white,
-30,
size="small")
message_to_screen(" the food to gain a point!",
white,
10,
size="small")
message_to_screen("Press 'C' to play the game or 'Q' to quit.",
white,
150,
size="small")
pygame.display.update()
clock.tick(15)
#Text Size
def text_objects(text,color, size):
if size=="small":
textSurface=smallfont.render(text, True ,color)
elif size=="medium":
textSurface=medfont.render(text, True ,color)
elif size=="large":
textSurface=largefont.render(text, True ,color)
return textSurface,textSurface.get_rect()
#Message to screen
def message_to_screen(msg,color,y_displace=0,size="small"):
textSurf,textRect=text_objects(msg,color,size)
textRect.center = (display_width / 2),(display_height / 2)+y_displace
gameDisplay.blit(textSurf,textRect)
#Score Display
def scoredisplay(scoredef=0):
text=smallfont.render("Score :%s" %(scoredef) ,True ,black)
gameDisplay.blit(text,[0,0])
score=0
global a
a=10
#print score
def runGame():
score=0
gameExit = False
gameOver = False
thing_startx = random.randrange(0,display_width)
thing_starty = -600
thing_speed = 7
thing_width = 100
thing_height = 100
while not gameExit:
deadZones=[]
while gameOver == True:
#Game Over message
gameDisplay.fill(black)
message_to_screen("Game over",
red,
y_displace=-50,
size="large")
message_to_screen("Press C to play again or Q to quit.",
white,
y_displace=50,
size="medium")
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
gameExit = True
gameOver = False
if event.key == pygame.K_c:
gameLoop()
gameDisplay.fill((white))
time=pygame.time.get_ticks()/1000
if time==11:
global time
global score
score=0
time=0
gameOver=True
#print score
for event in pygame.event.get():
if event.type==pygame.QUIT:
running=False
pygame.quit()
quit()
if event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT:
# Set the x, y postions of the mouse click
x, y = event.pos
if foodrect.collidepoint(x, y):
foodrect.center=(random.randint(5,1060),random.randint(5,700))
print "New Position: ",foodrect.center
score+=1
continue
gameDisplay.blit(foodimg,foodrect)
showtime=font.render("Time: "+str(time),0,(0,0,0))
gameDisplay.blit(showtime,(950,10))
#scoredisplay(score)
pygame.display.update()
def gameLoop():
clock.tick(FPS)
runGame()
pygame.quit()
quit()
game_intro()
gameLoop()
Thanks in advance to help.
I think I might have a solution that at least I use. While sometimes it is nice to use pygame.time.get_ticks, there is an easier solution. Before your while loop set a variable called frame to 0. Then at the end of your while loop type:
if gameEnd == True:
frame=0
score=0
else:
frame+=1
Then all you have to do is only draw the picture when frame>0. You can also easily manipulate frame. Message me if you need any more help.