I’m making a simple text based RPG game in pygame. I’m using a button class (not mine, but fairly simple to use and integrate) to add buttons with choices in them. However, it seems a button on the second slide, the ‘Sit and Wait’ choice, is overlapping with the ‘No’ button on the first slide. Since the No button closes the game, pressing on Sit and Wait seems to close the game also. I’ve tried ordering the if-else statements in different ways, but every other button seems fine. Any tips? Thank you. Here is my code, sorry I know it’s a lot but I’m a beginner and not great at condensing yet:
import pygame pygame.init() win=pygame.display.set_mode((800,700)) win.fill((255,255,255)) our_game_display=pygame.Surface((800,700)) font_name = pygame.font.get_default_font() class button(): def __init__(self, color, x, y, width, height, text=''): self.color = color self.x = x self.y = y self.width = width self.height = height self.text = text def draw(self, win, outline=None): # Call this method to draw the button on the screen if outline: pygame.draw.rect(win, outline, (self.x - 2, self.y - 2, self.width + 4, self.height + 4), 0) pygame.draw.rect(win, self.color, (self.x, self.y, self.width, self.height), 0) if self.text != '': font = pygame.font.SysFont('comicsans', 30) text = font.render(self.text, 1, (255, 255, 255)) win.blit(text, (self.x + (self.width / 2 - text.get_width() / 2), self.y + (self.height / 2 - text.get_height() / 2))) def isOver(self, pos): # Pos is the mouse position or a tuple of (x,y) coordinates if pos[0] > self.x and pos[0] < self.x + self.width: if pos[1] > self.y and pos[1] < self.y + self.height: return True return False def draw_text(text, size, x, y): pygame.font.init() font = pygame.font.Font(font_name, size) text_surface = font.render(text, True, (255,255,255)) text_rect = text_surface.get_rect() text_rect.center = (x, y) our_game_display.blit(text_surface, text_rect) def yell_choice(): our_game_display.fill((0, 0, 0)) draw_text('You yell to see if anyone can hear you.', 20, 800 / 2, 700 / 2 - 100) win.blit(our_game_display, (0, 0)) pygame.display.update() def sit_choice(): our_game_display.fill((0,0,0)) draw_text('So you decided to sit and wait', 20, 800 / 2, 700 / 2 - 100) win.blit(our_game_display,(0,0)) pygame.display.update() def search_choice(): our_game_display.fill((0,0,0)) draw_text('So you decided to search', 20, 800 / 2, 700 / 2 - 100) win.blit(our_game_display,(0,0)) pygame.display.update() def beginning_question(): our_game_display.fill((0, 0, 0)) draw_text('The story of this game depends on your choices. Do you wish to play?', 20, 800 / 2, 700 / 2 - 100) win.blit(our_game_display, (0, 0)) YesButton.draw(win, (255, 255, 255)) NoButton.draw(win, (255, 255, 255)) pygame.display.update() def begin_game(): our_game_display.fill((0,0,0)) draw_text("You wake up, or...at least you think you do. Even though your eyes are open,", 20, 800 / 2, 700 / 2 - 300) draw_text("they still can't detect anything in the complete darkness that surrounds you.", 20, 800 / 2, 700 / 2 - 270) draw_text("What do you do?", 20, 800 / 2, 700 / 2 - 210) win.blit(our_game_display,(0,0)) SitWaitButton.draw(win,(255,255,255)) SearchButton.draw(win,(255,255,255)) YellHelpButton.draw(win,(255,255,255)) pygame.display.update() #game loop running = True #color,x,y,width,height YesButton=button((0,0,0),100,500,250,100,'Yes') NoButton=button((0,0,0),450,500,250,100,'No') SitWaitButton=button((0,0,0),500,500,200,50,'Sit and wait..') SearchButton=button((0,0,0),250,600,600,50,'Get up and try to search your surroundings.') YellHelpButton=button((0,0,0),250,400,600,50,'Yell to see if anyone is there.') game_begun='Input' #beginning choice variable search_option='Input'#search variable sit_option='Input' #sitting variable yell_option = 'Input' while running: if search_option=='Go': search_choice() elif sit_option=='Go': sit_choice() elif yell_option=='Go': yell_choice() elif game_begun=='Go': begin_game() else: beginning_question() for event in pygame.event.get(): pos=pygame.mouse.get_pos() if event.type==pygame.QUIT: running = False pygame.quit() quit() #yes and no buttons for beginning question if event.type==pygame.MOUSEBUTTONDOWN: if SitWaitButton.isOver(pos): sit_option = 'Go' print("this button is working") if SearchButton.isOver(pos): search_option = 'Go' print("this button is working") if YellHelpButton.isOver(pos): yell_option='Go' print("this button is working") if YesButton.isOver(pos): game_begun='Go' if NoButton.isOver(pos): running=False pygame.quit() quit() if event.type==pygame.MOUSEMOTION: if YellHelpButton.isOver(pos): YellHelpButton.color = (0, 0, 139) elif SitWaitButton.isOver(pos): SitWaitButton.color = (0, 0, 139) elif SearchButton.isOver(pos): SearchButton.color = (0, 0, 139) elif YesButton.isOver(pos): YesButton.color=(0,0,139) elif NoButton.isOver(pos): NoButton.color=(0,0,139) else: YesButton.color=(0,0,0) NoButton.color=(0,0,0) YellHelpButton.color = (0, 0, 0) SitWaitButton.color = (0, 0, 0) SearchButton.color = (0, 0, 0)
Advertisement
Answer
You can use an if-elif
instruction like below, so that if the user clicks on the mouse while cursor is over the sit and wait
button then you don’t check if it’s also over the quit
button (and thus you don’t quit the game):
if SitWaitButton.isOver(pos): sit_option = 'Go' print("this button is working") elif NoButton.isOver(pos): pygame.quit() quit()
A cleaner solution would however be to remember which buttons are actually currently displayed. For example, you could have a list of visible buttons and check if the cursor is over a button only if this one is in the list.
On a side note, your running
variable is useless: your main loop is while running
but as soon as running
is set to False
you just quit the game immediately. So the condition of your loop never evaluates to False
.