首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >游戏按钮get.pressed()

游戏按钮get.pressed()
EN

Stack Overflow用户
提问于 2017-12-11 17:31:24
回答 2查看 2.3K关注 0票数 2

我只是刚开始玩游戏,这是我的代码,小片。我有主按钮功能,在gameloop中我成功地创建了一个交互式按钮,我不知道如何按下游戏中的按钮(“开始播放”)并填充我的gameDisplay,以获取example...white颜色。提前感谢........................................................................................................................

代码语言:javascript
复制
import pygame,sys


pygame.init()
#############
pygame.mixer.music.load('Invincible.mp3')
pygame.mixer.music.play()

#############

display_width = 800
display_height = 600

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

red = (200,0,0)
green = (0,200,0)

bright_red = (255,0,0)
bright_green = (0,255,0)

block_color = (53,115,255)


gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('One Day After')
clock = pygame.time.Clock()

gameIcon = pygame.image.load('gameicon.jpg')
pygame.display.set_icon(gameIcon)

pause = False

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


def GameOver():
    ####################################
    pygame.mixer.Sound.play("smb_gameover.wav")
    pygame.mixer.music.stop()
    ####################################
    largeText = pygame.font.SysFont("comicsansms",115)
    TextSurf, TextRect = text_objects("Game Over", largeText)
    TextRect.center = ((display_width/2),(display_height/2))
    gameDisplay.blit(TextSurf, TextRect)


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



        button("Play Again",150,450,100,50,green,bright_green,game_loop)
        button("Quit",550,450,100,50,red,bright_red,quitgame)

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

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:
            pygame.mixer.music.stop()
            action()

    else:
        pygame.draw.rect(gameDisplay, ic,(x,y,w,h))
    smallText = pygame.font.SysFont("comicsansms",20)
    textSurf, textRect = text_objects(msg, smallText)
    textRect.center = ( (x+(w/2)), (y+(h/2)) )
    gameDisplay.blit(textSurf, textRect)


def quitgame():
    pygame.quit()
    sys.exit()
    quit()

def unpause():
    global pause
    pygame.mixer.music.unpause()
    pause = False


def paused():
    ############
    pygame.mixer.music.pause()
    #############
    largeText = pygame.font.SysFont("comicsansms",115)
    TextSurf, TextRect = text_objects("Paused", largeText)
    TextRect.center = ((display_width/2),(display_height/2))
    gameDisplay.blit(TextSurf, TextRect)


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


        button("Continue",150,450,100,50,green,bright_green,unpause)
        button("Quit",550,450,100,50,red,bright_red,quitgame)

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


def game_intro():

    intro = True

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

        pilt1 = pygame.image.load('apoc2.jpg').convert()
        gameDisplay.blit(pilt1, [0,0])
        pygame.display.flip()


        button("Start",150,450,100,50,green,bright_green,game_loop)
        button("Quit",550,450,100,50,red,bright_red,quitgame)

        pygame.display.update()

def game_loop():
    global pause

    gameExit = False

    while not gameExit:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
                quit()
        gameDisplay.fill(white)
        gameDisplaypic = pygame.image.load('back.jpg').convert()
        gameDisplay.blit(gameDisplaypic, [0,0])
        tekst = "This game will go as far as you choose!"
        meie_font = pygame.font.SysFont("Arial", 36)
        teksti_pilt = meie_font.render(tekst, False, (50,50,155))
        gameDisplay.blit(teksti_pilt, (100, 250))
        tekst2 = "You are the smith of your destiny"
        meie_font = pygame.font.SysFont("Arial", 36)
        teksti_pilt = meie_font.render(tekst2, False, (50,50,155))
        gameDisplay.blit(teksti_pilt, (100, 400))
        button("Start playing",300,500,150,50,green,bright_green)
        pygame.display.update()









game_intro()
game_loop()
pygame.quit()
quit()
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-12-11 19:58:48

您可以使用状态变量(True/False)来控制要显示的元素,并且可以使用按钮更改这些变量。

举个例子

代码语言:javascript
复制
    display_text = True
    display_button_1 = True
    display_button_2 = False

以控制何时显示文本和两个按钮。

第一个按钮更改值,它隐藏文本和第一个按钮,并显示第二个按钮。

有一个问题-你的按钮功能是丑陋的,它使用get_buttons(),所以当我按住按下按钮,我移除一个按钮,把另一个放在同一个地方,然后它自动点击第二个按钮。您应该使用event.type == MOUSEBUTTONUP (建议@Hilea)来修复这个问题。

代码语言:javascript
复制
import pygame
import sys # every import in separated line

# --- constants ---

display_width = 800
display_height = 600

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

red = (200,0,0)
green = (0,200,0)

bright_red = (255,0,0)
bright_green = (0,255,0)

block_color = (53,115,255)

# --- functions ---

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

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:
            pygame.mixer.music.stop()
            action()

    else:
        pygame.draw.rect(gameDisplay, ic,(x,y,w,h))
    smallText = pygame.font.SysFont("comicsansms",20)
    textSurf, textRect = text_objects(msg, smallText)
    textRect.center = ( (x+(w/2)), (y+(h/2)) )
    gameDisplay.blit(textSurf, textRect)

def quitgame():
    pygame.quit()
    sys.exit()
    quit()

def hide_text():
    global display_text
    global display_button_1
    global display_button_2

    display_text = False
    display_button_1 = False
    display_button_2 = True

def game_loop():
    global display_text
    global display_button_1
    global display_button_2

    gameExit = False
    display_text = True
    display_button_1 = True
    display_button_2 = False

    meie_font = pygame.font.SysFont("Arial", 36)

    tekst = "This game will go as far as you choose!"
    teksti_pilt = meie_font.render(tekst, False, (50,50,155))

    tekst2 = "You are the smith of your destiny"
    teksti_pilt = meie_font.render(tekst2, False, (50,50,155))

    while not gameExit:

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

        gameDisplay.fill(white)

        if display_text:
            gameDisplay.blit(teksti_pilt, (100, 250))
            gameDisplay.blit(teksti_pilt, (100, 400))

        if display_button_1:
            button("Start playing", 300,500,150,50,green,bright_green, hide_text)

        if display_button_2:
            button("Exit", 300,100,150,50,green,bright_green, pygame.quit)

        pygame.display.update()

# --- main ---

pygame.init()

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('One Day After')

clock = pygame.time.Clock()

pause = False

game_loop()
pygame.quit()
票数 2
EN

Stack Overflow用户

发布于 2017-12-11 18:20:17

我希望这个项目能有所帮助。

代码语言:javascript
复制
import pygame,sys
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((400,400))
white=(255,255,255)
blue=(0,0,255)
bright_red = (255,0,0)
red = (200,0,0)
black=(0,0,0)
color=white

def changecolor(newcolor):
    global color
    color=newcolor

while True:
    screen.fill(color)

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == MOUSEBUTTONUP:
            # Whenever somebody clicks on the screen the color
            # will change from the initial color to the color
            # value you send to the function. Using this you could
            # make it specifically for your button. Like calling
            # the change color function whenever the button is clicked.
            changecolor(blue)

    pygame.display.update()
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47758248

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档