找不到方法,当我按下鼠标单击时,可以让我的一张照片停留在屏幕上

时间:2019-01-13 09:02:29

标签: python tkinter pygame

当我按下鼠标左键时,我试图将图像保留在屏幕上。我一直在关注youtube上的Sentdex视频,以获取有关如何绘制按钮等的信息,因此,如果它有点混乱,那是因为我对pygame很陌生!该游戏的目标是成为第一人称射击游戏,但最近发现使用python :(实际上是不可能的,因此,我只是继续从事该项目,看看它还能去哪里。任何想法/建议将不胜感激! / p>

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()

1 个答案:

答案 0 :(得分:0)

backgroundGameType图像不会停留在屏幕上,因为只有在将鼠标按住为按钮定义的区域内时,代码才会在屏幕上显示(戳破)该图像。

如果要在屏幕上说出来,一种方法是设置一个标志,该标志在主事件处理循环(while intro:)中的每次迭代中都进行检查,以跟踪是否即使在鼠标不再位于该区域。

下面是修改您的代码以执行此操作的步骤。注意,我已经注释掉了与问题无关的部分-在发布代码之前您应该做的事情(或者更好的是,已完全删除)。参见How to create a Minimal, Complete, and Verifiable Example

已添加一个名为game_mode_selected的新全局标志变量,并将其初始化为False,并在每次主循环迭代时检查其值。只要它没有“真实”的值,就会进行调用以显示"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()