Reputation: 11
First of all sorry for my bad english. I want do a ping pong game with using pygame. I do these : singleplayer mode, doubleplayer mode, main menu and pause menu. I can enter pause menu while play game there is no trouble here. I can continue the game there is no trouble here too. But I can't go main menu. When i click Main Menu button i go doubleplayer mode.
If statement :
if 462+225 > mouse[0] > 462 and 363+100 > mouse[1] > 363:
pygame.draw.rect(display,(250,250,250), (462,363,225,100))
mainmenutext=font.render("Main Menu", True, (0,0,0))
mainmenutextRect=mainmenutext.get_rect()
mainmenutextRect.center=(575, 410)
display.blit(mainmenutext, mainmenutextRect)
if click[0]==1
mainmenu()
I am trying to return to the main menu with this code but it is not working It is enter doubleplayer() function, I can't find bug.
Here is main menu function :
def mainmenu():
while menu:
for event in pygame.event.get():
#print(event)
if event.type==pygame.QUIT:
pygame.quit()
quit()
display.fill((0,0,0))
largefont=pygame.font.Font('data/BalooBhai-Regular.ttf',50)
maintext=largefont.render("Ping Pong", True, (255,255,255))
maintextRect=maintext.get_rect()
maintextRect.center=(400,200)
display.blit(maintext, maintextRect)
#Button
mainmenuoptionsfont=pygame.font.Font(None,30)
pygame.draw.rect(display, (255,255,255), (100,350,250,125))
pygame.draw.rect(display, (0,0,0), (112,363,225,100))
singletext=mainmenuoptionsfont.render("Single Player", True, (255,255,255))
singleRect=singletext.get_rect()
singleRect.center=(225,410)
display.blit(singletext, singleRect)
pygame.draw.rect(display,(255,255,255), (450,350,250,125))
pygame.draw.rect(display,(0,0,0), (462,363,225,100))
dobuletext=mainmenuoptionsfont.render("Double Player", True, (255,255,255))
doubletextRect=doubletext.get_rect()
doubletextRect.center=(575, 410)
display.blit(doubletext, doubletextRect)
mouse=pygame.mouse.get_pos()
#print(mouse)
click=pygame.mouse.get_pressed()
#print(click)
if 112+225 > mouse[0] > 112 and 363+100 > mouse[1] > 363: #on hover
pygame.draw.rect(display, (250,250,250), (100,350,250,125))
singletext=mainmenuoptionsfont.render("Single Player", True, (0,0,0))
singleRect=singletext.get_rect()
singleRect.center=(225,410)
display.blit(singletext, singleRect)
if click[0]==1: #if click
singleplayer()
if 462+225 > mouse[0] > 462 and 363+100 > mouse[1] > 363: #on hover
pygame.draw.rect(pencere,(250,250,250), (462,363,225,100))
doubletext=mainmenuoptionsfont.render("Double Player", True, (0,0,0))
doubletextRect=doubletext.get_rect()
doubletextRect.center=(575, 410)
display.blit(doubletext, doubletextRect)
if click[0]==1: #if click
doubleplayer()
pygame.display.update()
Sorry for my bad english :(
Upvotes: 0
Views: 132
Reputation: 1866
I think you are handling this a bit unnecessary complicated. The way I do menu buttons is something like this:
Create the button texts and then create a rectangle around it (visible or non visible, doens't matter). This is easiest done by using self.button1 = pygame.Rect(x, y, w, w)
.
Blit the buttons and texts on the screen.
In the event loop I then check for event clicks, like this:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
if self.button1.collidepoint(pos):
print('Do what the button is supposed to do')
You are also duplicating a lot of code. Create a function that handles the text/button like below. The atttribute "boxed" is there to give an option to create a visible rectangle around the text.
def create_button(self, text, font, color, x, y, boxed):
text = font.render(text, True, color)
rect = text.get_rect()
rect.center = (x, y)
if boxed:
box = (rect[0] - PADDING, rect[1] - PADDING/2, rect[2] + PADDING*2, rect[3] + PADDING)
pygame.draw.rect(self.win, WHITE, box, int(PADDING/2))
self.win.blit(text, rect)
return rect
Then you simply create the buttons with the following code:
self.button1 = self.create_button('Text 1', FONT, COLOR, X-COORDINATE, Y-COORDINATE, True)
self.button2 = self.create_button('Text 2', FONT, COLOR, X-COORDINATE, Y-COORDINATE, True)
Upvotes: 1