简体   繁体   English

如何在“游戏菜单”中创建 pygame

[英]how to create pygame “in game menus”

I'm making a pygame game where currently a character is moving around on a screen.我正在制作 pygame 游戏,其中当前角色正在屏幕上四处移动。 I want the character to be able to buy items when he stands on top of a shop.我希望角色站在商店顶部时能够购买物品。 I want a menu like screen to appear when the character is on top of the shop.当角色在商店顶部时,我希望出现类似屏幕的菜单。 I also want the rest of the game to continue in the background in case I decide to add some enemies.我还希望游戏的 rest 在后台继续运行,以防我决定添加一些敌人。 shop_items is the function that should run for the in game menu, showing the items available.I'm providing the whole code below. shop_items是 function 应该为游戏内菜单运行,显示可用的项目。我在下面提供整个代码。 Any help is appreciated, thank you.任何帮助表示赞赏,谢谢。

import pygame
pygame.font.init()

width = 800
height = 600

screen = pygame.display.set_mode([width, height])

walkRight = [pygame.image.load('R1.png'), pygame.image.load('R2.png'), pygame.image.load('R3.png'), pygame.image.load('R4.png'), pygame.image.load('R5.png'), pygame.image.load('R6.png'), pygame.image.load('R7.png'), pygame.image.load('R8.png'), pygame.image.load('R9.png')]
walkLeft = [pygame.image.load('L1.png'), pygame.image.load('L2.png'), pygame.image.load('L3.png'), pygame.image.load('L4.png'), pygame.image.load('L5.png'), pygame.image.load('L6.png'), pygame.image.load('L7.png'), pygame.image.load('L8.png'), pygame.image.load('L9.png')]
char = pygame.image.load('standing.png')


x = 50
y = 50
width = 40
height = 60
vel = 5
isJump = False
jumpCount = 10
left = False
right = False
down = False
up = False
walkCount = 0
shop = pygame.transform.scale(pygame.image.load("shop.png"), (60,60))

clock = pygame.time.Clock()
FPS = 60



def shop_items():
    if x + char.get_width() < 60 and y + char.get_height() < 60:
        screen.fill([0,0,0])

def redrawGameWindow():
    global walkCount

    screen.fill([166, 166, 166])  
    screen.blit(shop, (0,0))

    if walkCount + 1 >= 27:
        walkCount = 0

    if left:  
        screen.blit(walkLeft[walkCount//3], (x,y))
        walkCount += 1 

    elif right:
        screen.blit(walkRight[walkCount//3], (x,y))
        walkCount += 1

    elif down:
        screen.blit(char,(x,y))
        walkcount = 0

    elif up :
        screen.blit(char, (x,y))
        walkcount = 0

    else:
        screen.blit(char, (x, y))
        walkCount = 0

    pygame.display.update() 


def main():
    run = True

    pygame.display.set_caption("bomb-mania")


    global x
    global y
    global width
    global height
    global vel

    global isJump
    global jumpCount

    global left
    global right
    global down

    global walkCount    

    while run:

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

        keys = pygame.key.get_pressed()

        if keys[pygame.K_LEFT] and x > vel-15:
            x -= vel
            left = True
            right = False
            down = False
            up = False



        elif keys[pygame.K_RIGHT] and x < 800 - vel - width:
            x += vel
            left = False
            right = True
            down = False
            up = False

        elif keys[pygame.K_DOWN] and y < 600 - height:
            y += vel
            left = False
            right = False
            down = True
            up = False


        elif keys[pygame.K_UP] and y > vel-15:
            y -= vel
            left = False
            right = False
            down = False
            up = True

        else:
            left = False
            right = False
            down = False
            up = False
            walkCount = 0

        if x + char.get_width() < 60 and y + char.get_height() < 60:
            # print("reached")
            shop_items()


        redrawGameWindow()

        clock.tick(FPS)
        pygame.display.flip()


main()

I assume something like they have in League of Legends?我假设他们在英雄联盟中拥有类似的东西? :) You need to add a function to your main loop, something like: :) 您需要将 function 添加到您的主循环中,例如:

shop = False
def toggle_shop():
    if player.rect.colliderect(shop.rect) and key_pressed():
        if shop:
            shop = True
        else:
            shop = False


def display_shop():
    # Run code to display your shop

In your main loop it would look something like:在您的主循环中,它看起来像:

shop = False
main():
    toggle_shop()
    if shop:
        display_shop()
    ...

Inside of the toggle_shop() function, I wrote I key_pressed() function.在 toggle_shop() function 内部,我写了 I key_pressed() function。 If you need help with that, there is a great functionality of pygame called pygame.event: https://www.pygame.org/docs/ref/event.html Use this line: If you need help with that, there is a great functionality of pygame called pygame.event: https://www.pygame.org/docs/ref/event.html Use this line:

events = pygame.event.get()
for event in events:
    if event.type == pygame.KEYDOWN:
         # Test which key was pressed

Hope this helps and good luck!希望这会有所帮助,祝你好运!

In response to the follow up question- look at the fourth and fifth line:回答后续问题 - 查看第四行和第五行:

shop = pygame.transform.scale(pygame.image.load("shop.png"), (60,60))
toggle_shop = False
def toggle_shop():
    shop_rect = shop.get_rect()  # This will return a pygame.Rect object
    if player.rect.colliderect(shop_rect) and key_pressed():  # Alter this line accordingly- shop_rect rather than shop.rect
        if toggle_shop:
            toggle_shop = True
        else:
            toggle_shop = False

One more note- generally it isn't good practice to name variables the same as functions.还有一点需要注意——通常,将变量命名为与函数相同的名称并不是一个好习惯。 Maybe change the "toggle_shop" Boolean variable to "shop_state" or "shop_active".也许将“toggle_shop”Boolean 变量更改为“shop_state”或“shop_active”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM