在此输入
当单击主菜单上的播放按钮时,它会更改屏幕并单击播放屏幕上的纸张按钮。我和我的教练在解决单击两个屏幕上的按钮问题时都遇到了问题。任何帮助都会有帮助的。谢谢你。
当我单击主菜单上的“播放”时,是否应更改为播放屏幕并等待再次单击。 图形用户界面代码。我已经包含了使程序运行的所有代码。
下面的代码用于程序的 GUI。它加载带有三 (3) 个按钮的主菜单。所有按钮都按其应有的方式工作,但是,当我单击播放按钮时,它会按应有的方式将屏幕更改为播放屏幕,但它还会单击播放屏幕上的纸张按钮。我没有包含图像文件或字体文件。
# Game Imports.
import pygame, sys, code_classes, button, time
# Initializes pygame.
pygame.init()
# Variables that set windows Width and Height
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 550
fps = 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()
下面是我用来创建按钮的代码。该类被导入到 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
下面的代码是我的主要游戏代码。它执行所有逻辑检查。
import random # This imports the module for generating random numbers.
'''Below are Varables for the game. '''
computers_choise = ''
users_choise = ''
score = 0
win = 0
statement = '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_string
def pick():
pick = random.randrange(5)
computers_choise = rpsls[pick]
return computers_choise
def 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 statement
if __name__ == '__main__': # This says if this is the main program run everything if not do not print.
computers_choise = pick()
print(computers_choise)
我已经提供了所有代码,因为我不确定问题是否出在 GUI、主程序或按钮中。
简而言之:您必须使用
event.MOUSEBUTTONDOWN
而不是 pygame.mouse.get_pressed()
,因为当您按住鼠标时,get_pressed()
会一直发送 True
,在您释放鼠标按钮之前,它可以更改屏幕并单击下一个屏幕上的对象。电脑太快了
但是
event
仅发送一个 event.MOUSEBUTTONDOWN
,它将仅单击按钮,但不会单击下一屏幕上的对象。
可能需要将
draw_button
拆分为两个函数:
handle_event(event)
中运行
for event in pygame.event.get():
,它将检查 event.MOUSEBUTTONDOWN
和 event.pos
以设置 self.clicked = True
(最终会通过 event.MOUSEMOTION
和 event.pos
检查鼠标是否悬停按钮并设置 self.hovered = True
) draw()
,不使用get_pressed()
,而仅使用self.clicked
来绘制单击的按钮,(最终使用self.hovered
来绘制悬停按钮)。