type here
When clicking my play button on my main menu it changes the screen and clicks the paper button on the play screen. My instructor and I are both have issues solving the problem of clicking buttons on both screens. Any help would be helpful. Thank you.
When I click play on the main menu if should change to the play screen and wait for another click.GUI code. I have included all the code to make the program work.
The code below is for the GUI of the program. It loads a Main Menu with three (3) buttons. All the button work as they should, however, when I click the play button it changes the screens as it should to the play screen but it also clicks the paper button on the play screen. I did not include the image files or font files.
# Game Imports. import pygame, sys, code_classes, button, time# Initializes pygame.pygame.init()# Variables that set windows Width and HeightWINDOW_WIDTH = 800WINDOW_HEIGHT = 550fps = 30 clock = pygame.time.Clock()# Sets the fonts for pygame. title_font = pygame.font.Font('fonts/campus.ttf', 25)title_bf = pygame.font.Font('fonts/college.ttf', 20)game_bf = pygame.font.Font('fonts/collegeb.ttf', 30)socre_font = pygame.font.Font('fonts/CollegiateFLF.ttf', 20)# Loads the images for background and buttons.icon = pygame.image.load('photo/icon.png')play = pygame.image.load('photo/play_button.png')play_again = pygame.image.load('photo/play_again.png')main_menu = pygame.image.load('photo/main_menu.png')directions = pygame.image.load('photo/directions_button.png')exit_img = pygame.image.load('photo/exit_button.png')background = pygame.image.load('photo/enterprise.png')rock = pygame.image.load('photo/rock.jpg')paper = pygame.image.load('photo/paper.jpg')scissors = pygame.image.load('photo/scissors.png')lizard = pygame.image.load('photo/lizard.jpg')spock = pygame.image.load('photo/spock.jpg')# Sets the colors for pygame. white = (255, 255, 255)black = (0, 0, 0)gray = (113, 121, 126)green = (170, 255, 0)# Creates Buttons.play_button = button.Button(375, 175, play, .35)info_button = button.Button(375, 250, directions, .35)exit_button = button.Button(375, 325, exit_img, .35)exit_button1 = button.Button(126, 505, exit_img, .30)play_again_button = button.Button(251, 505, play_again, .30)main_menu_button = button.Button(1, 505, main_menu, .30)rock_button = button.Button(150, 100, rock, .15)paper_button = button.Button(275, 100, paper, .35)scissors_button = button.Button(525, 100, scissors, .68)lizard_button = button.Button(185, 250, lizard, .20)spock_button = button.Button(450, 250, spock, .17)# Creates the window and sets the window title. window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))pygame.display.set_caption("Main Menu")pygame.display.set_icon(icon)# game window control. window_state = 'main'# player selection.player = ''code_classes.users_choise = player# Fuction to draw text on screen.def show_text(text, font, text_color, x, y): text_img = font.render(text, True, text_color) window.blit(text_img, (x, y))# main game loop.app = True # Sets the varabile app to True.while app: # Creates a while loop. # fills the root_window with black. window.fill('black') window.blit(background, (0, 0)) clock.tick(fps) # chech game state. if window_state == 'main': # if window_state varabile is 'main' run code below. show_text('Rock, Paper, Scissors, Lizard, Spock', title_font, green, 180, 10) # Puts text on the screen. show_text('Please make a selection', title_font, green, 255, 40) # Puts text on the screen. if play_button.draw_button(window): # Puts button on screen. #time.sleep(.5) window_state = 'play' # Changes the window_state varabile to 'play' if info_button.draw_button(window): # Puts button on screen. window_state = 'info' # Changes the window_state varabile to 'info' if exit_button.draw_button(window): # Puts button on screen. app = False # Sets the varabile app to False and close the while loop exiting the game. if window_state == 'play': # if window_state varabile is 'main' run code below. player_choise = '' show_text('Rock, Paper, Scissors, Lizard, Spock', title_font, green, 180, 10) # Puts text on the screen. show_text('Please make a selection', title_font, green, 255, 40) # Puts text on the screen. show_text(f'Score = {code_classes.score_str()}', socre_font, green, 650, 510) # Puts text on the screen. show_text(f'{code_classes.statement}', game_bf, green,300, 375) if main_menu_button.draw_button(window): # Puts button on screen. window_state = 'main' # Changes the window_state varabile to 'main' if exit_button1.draw_button(window): # Puts button on screen. app = False # Sets the varabile app to False and close the while loop exiting the game. sys.exit() if rock_button.draw_button(window): # Puts button on screen. player_choise = 'Rock' print('Rock') if paper_button.draw_button(window): # Puts button on screen. player_choise = 'Paper' print('Paper') if scissors_button.draw_button(window): # Puts button on screen. player_choise = 'Scissors' print('Scissors') if lizard_button.draw_button(window): # Puts button on screen. player_choise = 'Lizard' print('Lizard') if spock_button.draw_button(window): # Puts button on screen. player_choise = 'Spock' print('Spock') if player_choise: computer_choise = code_classes.pick() check_win = code_classes.Check_Win(player_choise, computer_choise) winner = check_win.player_Win() check_win.add_score(winner) statement = check_win.statments_to_return(winner) player_choise = '' if window_state == 'info': # if window_state varabile is 'main' run code below. show_text('Rock, Paper, Scissors, Lizard, Spock', title_font, green, 180, 10) # Puts text on the screen. show_text('THE RULES ARE AS FOLLOWS:', title_font, green, 250, 80) # Puts text on the screen. show_text('The user will pick one of the following: Rock, Paper, Scissors, Lizard or Spock.', title_bf, green, 25, 110) # Puts text on the screen. show_text('The computer will than randomly make a choise.', title_bf, green, 175, 130) # Puts text on the screen. show_text('HOW TO WIN:', title_font, green, 325, 200) # Puts text on the screen. show_text('Rock beats scissors and lizard.', title_bf, green, 250, 220) # Puts text on the screen. show_text('Paper beats rock and disproves Spock.', title_bf, green, 225, 240) # Puts text on the screen. show_text('Scissors cuts paper and decapitates lizard.', title_bf, green, 200, 260) # Puts text on the screen. show_text('Lizard eats paper and poisons Spock.', title_bf, green, 225, 280) # Puts text on the screen. show_text('Spock vaporizes rock and smashes scissors.', title_bf, green, 200, 300) # Puts text on the screen. if main_menu_button.draw_button(window): # Puts button on screen. window_state = 'main' # Changes the window_state varabile to 'main' if exit_button1.draw_button(window): # Puts button on screen. app = False # Sets the varabile app to False and close the while loop exiting the game. sys.exit() # handles events for event in pygame.event.get(): # Looks for and get events from pygame. if event.type == pygame.QUIT: # If the event is equal to pygame quit event exacute code below. app = False # Sets the varabile app to False and close the while loop exiting the game. # updates the game window. pygame.display.update() #update game display# Quits pygame when loop exits. pygame.quit()
Below is the code I use to create the buttons. This class is imported to the GUI.py.
# Game Imports. import pygame# Creats a class for making a button in pygame.class Button: def __init__(self, x, y, image, scale): # Gets the image width and set it to variable image_width image_width = image.get_width() # Gets the image height and set it to variable image_height image_height = image.get_height() # Sets the scale of the image using transform.scale by multipling the image varables by a decimal and asigns the image to a variable image. self.image = pygame.transform.scale(image, (int(image_width * scale), int(image_height * scale))) # You have to set your math to an int pygame will crash if your code returns a float. # Sets the image on a pygame rect object. self.rect = self.image.get_rect() # Sets the top left conor of the rect object to x, y position. self.rect.topleft = (x, y) # Sets the button clicked value to False (button clicked off). self. clicked = False # Defines a new fuction under the Button class. def draw_button(self, surface): action = False # Gets the mouse positon. pos = pygame.mouse.get_pos() # Checks if the mouse is over a button and in button was clicked. if self.rect.collidepoint(pos): # checks if the left [0] mouse button was clicked and only lets the button be click if clicked is False. if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False: # When letf mouse button click set the click varable to True and stops the mouse from clicking again. self.clicked = True action = True # Reset the clicked varable to False when left mouse button is released. if pygame.mouse.get_pressed()[0] == 0: self.clicked = False # Draws the botton on the window. surface.blit(self.image, (self.rect.x, self.rect.y)) return action
The code below is my main game code. It does all the logic checks.
import random # This imports the module for generating random numbers.'''Below are Varables for the game. '''computers_choise = ''users_choise = ''score = 0win = 0statement = 'Make a Choise'win_statement = 'You Won'lose_statement = 'You lost'tie_statement = 'You have tied''''Below is a list of choise for the user and computer to use.'''rpsls = ['Rock', 'Paper', 'Scissors', 'Lizard', 'Spock']def score_str(): score_string = str(score) return score_stringdef pick(): pick = random.randrange(5) computers_choise = rpsls[pick] return computers_choisedef users_choise(): return users_choise.capitalize()class Check_Win: def __init__(self, users_choise, computers_choise): self.computers_choise = computers_choise self.users_choise = users_choise def player_Win(self): win = 0 if (self.users_choise == 'Rock'): if (self.computers_choise == 'Scissors'): win += 1 elif (self.computers_choise == 'Lizard'): win += 1 elif (self.users_choise == self.computers_choise): win += 1.5 elif (self.users_choise == 'Paper'): if (self.computers_choise == 'Rock'): win += 1 elif (self.computers_choise == 'Spock'): win += 1 elif (self.users_choise == self.computers_choise): win += 1.5 elif (self.users_choise == 'Scissors'): if (self.computers_choise == 'Paper'): win += 1 elif (self.computers_choise == 'Lizard'): win += 1 elif (self.users_choise == self.computers_choise): win += 1.5 elif (self.users_choise == 'Lizard'): if (self.computers_choise == 'Paper'): win += 1 elif (self.computers_choise == 'Spock'): win += 1 elif (self.users_choise == self.computers_choise): win += 1.5 elif (self.users_choise == 'Spock'): if (self.computers_choise == 'Rock'): win += 1 elif (self.computers_choise == 'Scissors'): win += 1 elif (self.users_choise == self.computers_choise): win += 1.5 else: return "What the hell" return win def add_score(self, win): global score self.win = win if win == 1: score += 1 else: score return score def statments_to_return(self, win): global statement self.win = win if win == 1: win = 0 statement = win_statement return statement elif win == 1.5: win = 0 statement = tie_statement return statement else: statement = lose_statement return statementif __name__ == '__main__': # This says if this is the main program run everything if not do not print. computers_choise = pick() print(computers_choise)
I have provided all of the code because I am not sure if the issue is in GUI, main, or button.