私は現在ゲームをPython 3.6でPygameでプログラミングしています。 私のゲームには、スタートメニューがあり、ボタンを重ねるとハイライト表示されます。 ボタンをクリックすると、スタートメニューが消えてゲームが始まります。 唯一の問題は、開始ボタンが残っている...私はboolean
の値とif
のステートメントを使用して、メニューが表示されたときに消えるようにしようとしましたが、無駄にしました。Pygame rectボタンが画面に貼り付けられ、ウィンドウコードが正しく動作しない
さらに悪いことに、ウィンドウを閉じることを許可するコードpygame
は機能しません(コメントアウトによって強調表示されます)。誰も私にこれらの小さな問題を手伝ってもらえますか?彼らは些細なことだと思うが、私はpygame
には新しく、どこでも修正を見つけることができないようだ。
あなたがプログラムを実行するために、下の写真が必要になります。
This is the photo of the girl.
コード:ループの事内
#Imports
import sys
import pygame
pygame.init() #Initialise Pygame
#RGB colours
GREY = (128,128,128)
WHITE = (255,255,255)
BLACK = (0,0,0)
#fonts
title_font = pygame.font.Font('freesansbold.ttf',72)
menu_font = pygame.font.Font('freesansbold.ttf',32)
#images
girl_img = pygame.image.load('emily.png')
background_img = pygame.image.load('tech_lab.png')
class Game: #Game Class
def __init__(self): #Constructor
self.size = width, height = (1000,563)
self.screen = pygame.display.set_mode(self.size)
def menu_screen(self): #Menu Screen Function
intro = True
while intro:
## for event in pygame.event.get(): #To close window
## print(event)
## if event.type == pygame.QUIT:
## pygame.quit()
## sys.exit()
self.screen.fill(GREY) #Set background colour
self.screen.blit(title_font.render('EXAMPLE', True, BLACK), (330,100)) #Display game title
start_button = pygame.draw.rect(self.screen, (BLACK), pygame.Rect(410,310,180,55)) #Display start button rect
self.screen.blit(menu_font.render('Play', True, GREY), (425,310)) #Display start button text
pygame.display.flip() #Update display
while True:
pygame.event.get() #Get pygame events
click_pos = pygame.mouse.get_pos() #Get mouse position
if 410+180 > click_pos[0] > 410 and 310+55 > click_pos[1] > 310: #If mouse position within start button rect...
pygame.draw.rect(self.screen, (WHITE), pygame.Rect(410,310,180,55)) #then change colour of start button rect
if (pygame.mouse.get_pressed())[0] == 1: #If start button rect clicked...
start_game(self) #Start main game
else:
pygame.draw.rect(self.screen, (BLACK), pygame.Rect(410,310,180,55)) #Otherwise start button rect colour is black
self.screen.blit(menu_font.render('Play', True, GREY), (425,310))#Display start button text
pygame.display.flip() #Update display
def start_game(game): #Main game function
game.screen.blit(background_img, [0,0]) #Display background image
game.screen.blit(girl_img, [80,25]) #Display image
pygame.display.flip() #Update display
def main():
game = Game() #Instantiate class 'game'
game.menu_screen() #Call menu screen function
main()
ご回答いただきありがとうございます。私はあなたの疑似コードをPythonコードに書いて試してみました。ゲームが始まり、メニューが消え、クリックするとメニューが閉じます。しかし、私の上にマウスを置くと、スタートボタンの色は変わりません。これはループ中の削除のためですか?私のコードを壊すことなく、マウスをその上に置いたときにボタンの色を変えるにはどうすればいいですか? – Arbiter
心配しないでください!それを私が直した。あなたの答えをありがとう! – Arbiter
@Arbiter cool。あなたがうまく働いてうれしい。ハッピーコーディング – The4thIceman