我刚开始一段时间后,有一些问题,有点迷失.

一直在try 用图像作为按钮在Electron 游戏中制作菜单.在看了其他人之后,我模糊了大部分内容,但我认为我犯了一个愚蠢的错误,弄不明白--砍掉了很多行.第一个按钮起作用了,会把你带到正确的菜单,但其他按钮都没有注册.

大多数向导使用绘制来创建按钮,因此没有什么帮助.

Similar but uses draw

我try 移动If\Elif\Else语句,但出现错误或什么都不起作用.

不是真正的程序员,只是想要一些东西来把我们的D&Dbrew 软件结合在一起--以前使用过TKint,但我想我会试一试.

import pygame
from pygame import mixer
 
pygame.init()
width=900;
height=800
screen = pygame.display.set_mode( (width, height ) )
pygame.display.set_caption('Chapter 1')
#TownMap = pygame.image.load("Data/images/town.png").convert()
#Button = pygame.image.load("Data/images/buttonsSolo.png").convert_alpha()

def Main_Menu():  

  Back_Drop = pygame.image.load("Data/images/Art/cursewick1.jpg").convert()
  Start_Button = pygame.image.load("Data/Images/UI/Buttons/Start.png").convert_alpha() 
  Help_Button = pygame.image.load("Data/Images/UI/Buttons/Help.png").convert_alpha() 
  Exit_Button = pygame.image.load("Data/Images/UI/Buttons/Exit.png").convert_alpha() 
  screen.blit(Back_Drop, (0,0))
  screen.blit(Start_Button ,  ( 10,10)) # paint to screen
  screen.blit(Help_Button ,  ( 10,110)) # paint to screen
  screen.blit(Exit_Button ,  ( 10,210)) # paint to screen
  pygame.display.flip() # paint screen one time
  
  mixer.init()
  mixer.music.load("Data\Audio\Music\Intro.mp3")
  pygame.mixer.music.set_volume(0.1)
  mixer.music.play()
  
  
  running = True
  while (running):  
  
      for event in pygame.event.get():
          if event.type == pygame.QUIT:
              running = False
          if event.type == pygame.MOUSEBUTTONDOWN:
              # Set the x, y postions of the mouse click
              x, y = event.pos
              if Start_Button.get_rect().collidepoint(x, y):
                  print('clicked on image')
                  pygame.mixer.music.pause()
                  Town_Map()
              if Help_Button.get_rect().collidepoint(x, y):
                  print('Help')
                  pygame.mixer.music.pause()
                      
                  

  #loop over, quite pygame
  pygame.quit()

推荐答案

pygame.Surface没有位置,因此get_rect方法返回一个Rect,其中包含所有图像的图像尺寸和坐标(x=0, y=0).

您必须将blit位置保存在曲面矩形中,以便矩形的原点坐标与图像在屏幕上显示的全局坐标匹配.例如:

import pygame


pygame.init()
width = 900
height = 800
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Chapter 1')


def main_menu():

    back_drop = pygame.image.load("Data/images/Art/cursewick1.jpg").convert()
    back_drop_rect = back_drop.get_rect(topleft=(0, 0))

    start_button = pygame.image.load("Data/Images/UI/Buttons/Start.png").convert_alpha() 
    start_button_rect = start_button.get_rect(topleft=(10, 10))

    help_button = pygame.image.load("Data/Images/UI/Buttons/Help.png").convert_alpha() 
    help_button_rect = help_button.get_rect(topleft=(10, 110))

    exit_button = pygame.image.load("Data/Images/UI/Buttons/Exit.png").convert_alpha() 
    exit_button_rect = exit_button.get_rect(topleft=(10, 210))

    screen.blit(back_drop, back_drop_rect)
    screen.blit(start_button, start_button_rect)
    screen.blit(help_button, help_button_rect)
    screen.blit(exit_button, exit_button_rect)
    pygame.display.flip()

    pygame.mixer.music.load("Data\Audio\Music\Intro.mp3")
    pygame.mixer.music.set_volume(0.1)
    pygame.mixer.music.play()

    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.MOUSEBUTTONDOWN:
                x, y = event.pos
                if start_button_rect.collidepoint(x, y):
                    print("Start")
                    pygame.mixer.music.pause()
                elif help_button_rect.collidepoint(x, y):
                    print('Help')
                    pygame.mixer.music.pause()
                elif exit_button_rect.collidepoint(x, y):
                    print('Exit')
                    pygame.mixer.music.pause()

    pygame.quit()


main_menu()

Python相关问答推荐

如何随着收件箱的增加动态添加到HTML表的右下角?

为什么自定义pytree aux_data对于jnp.数组来说在.jit()之后跟踪,而对于np.数组来说则不是?

有什么方法可以修复奇怪的y轴Python matplotlib图吗?

覆盖Django rest响应,仅返回PK

剧作家Python没有得到回应

返回nxon矩阵的diag元素,而不使用for循环

如何在箱形图中添加绘制线的传奇?

Pandas 有条件轮班操作

可变参数数量的重载类型(args或kwargs)

在Polars(Python库)中将二进制转换为具有非UTF-8字符的字符串变量

Telethon加入私有频道

我们可以为Flask模型中的id字段主键设置默认uuid吗

如何从pandas的rame类继承并使用filepath实例化

部分视图的DataFrame

导入...从...混乱

形状弃用警告与组合多边形和多边形如何解决

将pandas导出到CSV数据,但在此之前,将日期按最小到最大排序

用渐近模计算含符号的矩阵乘法

合并帧,但不按合并键排序

pandas:对多级列框架的列进行排序/重新排序