当按下鼠标时,找不到方法使我的一张图片留在屏幕上

2024-04-27 01:02:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图得到一个图像留在屏幕上时,我的左键点击按下。我一直在关注youtube上的Sentdex视频,了解如何绘制按钮等信息,所以如果有点混乱,那是因为我对pygame还很陌生!这个游戏的目标是成为一个第一人称射击手,但是最近发现用python这几乎是不可能的:(所以我只是继续这个项目,看看它还能去哪里。如有任何意见/建议,将不胜感激

import time
import pygame
from tkinter import *
#import pyautogui

pygame.mixer.pre_init(44100,16,2,4096)
pygame.init()

display_width = 1200
display_height = 600

black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)

hgrey = (77,197,179)
hlight_grey = (255,102,106)
grey = (68,187,169)
light_grey = (247,94,98)

pygame.mixer.music.load('music/privia_the_begining.mp3')
pygame.mixer.music.play(-1)

counter = 0

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Privia 1.0')
clock = pygame.time.Clock()

gameIcon = pygame.image.load('pics/priviaicon.png')
bannerIcon = pygame.image.load('pics/priviabanner.png')
backgroundMenu = pygame.image.load('pics/bgm.png')
backgroundGameType = pygame.image.load('pics/bgmws.png')

pygame.display.set_icon(gameIcon)

def text_objects(text, font):
  textSurface = font.render(text, True, black)
  return textSurface, textSurface.get_rect()

def messeage_display(text):
  largeText = pygame.font.Font('freesansbold.ttf',115)
  TextSurf, TextRect = text_objects(text, largeText)
  TextRect.center = ((display_width/2),(display_height/2))
  gameDisplay.blit(TextSurf, TextRect)

  pygame.display.update()

def button(msg,x,y,w,h,ic,ac,action=None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()

    if x+w > mouse[0] > x and y + h > mouse[1] > y:
        pygame.draw.rect(gameDisplay, ac,(x,y,w,h))
        if click[0] == 1 and action != None:
            action()         
    else:
        pygame.draw.rect(gameDisplay, ic,(x,y,w,h))

    smallText = pygame.font.Font("freesansbold.ttf",20)
    textSurf, textRect = text_objects(msg, smallText)
    textRect.center = ( (x+(w/2)), (y+(h/2)) )
    gameDisplay.blit(textSurf, textRect)

def quitgame():
    pygame.quit()
    quit()
    pygame.mixer.stop()

def main_menu():
  intro = True

  while intro:
    for event in pygame.event.get():
      if event.type == pygame.QUIT:
          pygame.quit()
          quit()

    gameDisplay.blit(backgroundMenu, [0, 0])

    gameDisplay.blit(bannerIcon,(300,4))

    button("SinglePlayer",430,260,350,100,grey,hgrey,game_type)
    button("Quit",507,400,200,50,light_grey,hlight_grey,quitgame)

    pygame.display.update()
    clock.tick(15)

def game_type():
  gameDisplay.blit(backgroundGameType, [0, 0])
  print("working")

main_menu()

Tags: textimageimportpngdefdisplayloadpygame
1条回答
网友
1楼 · 发布于 2024-04-27 01:02:08

backgroundGameType图像没有保留在屏幕上,因为代码只在为按钮定义的区域内按住鼠标时显示(blit)它到屏幕上

如果您想在屏幕上显示它,一种方法是设置一个标记,在主事件处理循环(while intro:)的每次迭代中检查该标记,该标记跟踪是否在鼠标不再位于该区域时也显示该标记

下面是修改后的代码。请注意,我已经注释掉了其中与问题无关的部分,这是您在发布代码之前应该做的事情(或者更好的是,已经完全删除)。见How to create a Minimal, Complete, and Verifiable Example

添加了一个名为game_mode_selected的新全局标志变量,并将其初始化为False,并且在主循环的每次迭代中检查其值。只要它没有“truthy”值,就会调用以显示"SinglePlayer"按钮并检查鼠标的状态。当鼠标在为其定义的区域内单击,并调用关联的action函数game_type()时,此全局标志的值将更改为“记住”它发生了什么

因为我假设此时您不再希望显示"SinglePlayer"按钮,所以我还向主循环的开头添加了一个gameDisplay.fill(black)调用。没有它按钮将继续可见,即使它不再做任何事情

import time
import pygame
from tkinter import *
#import pyautogui

#pygame.mixer.pre_init(44100,16,2,4096)
pygame.init()

display_width = 1200
display_height = 600

black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)

hgrey = (77,197,179)
hlight_grey = (255,102,106)
grey = (68,187,169)
light_grey = (247,94,98)

#pygame.mixer.music.load('music/privia_the_begining.mp3')
#pygame.mixer.music.play(-1)

counter = 0

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Privia 1.0')
clock = pygame.time.Clock()

#gameIcon = pygame.image.load('pics/priviaicon.png')
#bannerIcon = pygame.image.load('pics/priviabanner.png')
backgroundMenu = pygame.image.load('pics/bgm.png')
backgroundGameType = pygame.image.load('pics/bgmws.png')

#pygame.display.set_icon(gameIcon)

def text_objects(text, font):
  textSurface = font.render(text, True, black)
  return textSurface, textSurface.get_rect()

def messeage_display(text):
  largeText = pygame.font.Font('freesansbold.ttf',115)
  TextSurf, TextRect = text_objects(text, largeText)
  TextRect.center = ((display_width/2),(display_height/2))
  gameDisplay.blit(TextSurf, TextRect)

  pygame.display.update()

def button(msg, x, y, w, h, ic, ac, action=None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()

    if (x+w > mouse[0] > x) and (y + h > mouse[1] > y):
        pygame.draw.rect(gameDisplay, ac, (x,y,w,h))
        if click[0] == 1 and action != None:
            action()
    else:
        pygame.draw.rect(gameDisplay, ic, (x,y,w,h))

    smallText = pygame.font.Font("freesansbold.ttf", 20)
    textSurf, textRect = text_objects(msg, smallText)
    textRect.center = ( (x+(w/2)), (y+(h/2)) )
    gameDisplay.blit(textSurf, textRect)

def quitgame():
    pygame.quit()
    quit()
#    pygame.mixer.stop()

game_mode_selected = False  # New global variable

def main_menu():
  global game_mode_selected
  game_mode_selected = False
  intro = True

  while intro:
    gameDisplay.fill(black)  # Added.

    for event in pygame.event.get():
      if event.type == pygame.QUIT:
          pygame.quit()
          quit()

    gameDisplay.blit(backgroundMenu, [0, 0])

#    gameDisplay.blit(bannerIcon,(300,4))

    if not game_mode_selected:
        button("SinglePlayer", 430,260, 350,100, grey, hgrey, game_type)
    else:
        gameDisplay.blit(backgroundGameType, [0, 0])

    button("Quit", 507,400, 200,50, light_grey, hlight_grey, quitgame)

    pygame.display.update()
    clock.tick(15)

def game_type():
  global game_mode_selected
  game_mode_selected = True  # Change global because button was clicked.

  gameDisplay.blit(backgroundGameType, [0, 0])
  print("working")

main_menu()

相关问题 更多 >