我正在嘗試為 3 個玩家連接 4 創建一個主選單(起始頁面),但我無法創建一個我想要 5 個按鈕(3 人游戲、2 人游戲、單人游戲、選項、退出)。
任何人都可以幫忙嗎?如果可能的話,誰能給我示例代碼,我可以適應我的游戲。
我以前沒有創建過主選單。
我不使用 tkinter 或 pygame。
uj5u.com熱心網友回復:
一個黃色按鈕的小例子,點擊時會列印一些文本(代碼注釋中有更多解釋):
import pygame
pygame.init()
screen = pygame.display.set_mode((500, 400))
clock = pygame.time.Clock()
# create the surface that will be the button
btn = pygame.Surface((300, 200))
btn.fill((255, 255, 0))
rect = btn.get_rect(center=(250, 200))
clicked = False
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
screen.fill((0, 0, 0))
# get mouse pos
mouse_pos = pygame.mouse.get_pos()
# get the state of left mouse button
left, *_ = pygame.mouse.get_pressed()
# check if mouse is in the button and if the mouse button has been clicked
# the flag `clicked` is for registering the click once when button is pressed
# otherwise it will loop again and the conditions will be true again
# you can also use the event loop above to detect whether the
# mouse button has been pressed, then the flag is not needed
if rect.collidepoint(mouse_pos) and left and not clicked:
clicked = True
print('clicked')
# reset flag
if not left:
clicked = False
# show the button
screen.blit(btn, rect)
pygame.display.update()
有用:
pygame.Rect.collidepoint
uj5u.com熱心網友回復:
這是一個使用 Tkinter 的小例子:
from tkinter import *
def click():
# Make another window.
top = Toplevel()
# You can add more elements here.
# Creating the Main Window
root = Tk()
root.title(“Connect 4”) # You can replace the title with your own
btn = Button(root, text=“Place Your Text”, command=click).pack()
# You can add as many of these buttons
root.mainloop()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/356588.html
