无法确定当“级别”更改时如何在Pygame中运行背景场景

问题描述 投票:0回答:1

我是python的新手,想创建一个游戏来测试一些知识。请说明我的错误。如果可能的话,我也非常感谢您写出一个示例,以便我可以全神贯注于这个概念。

我不知道如何在不格式化噩梦的情况下发布整个代码,所以我把它放在github〜抱歉,如果这违反条款!

我删除了大部分对话,因为对话时间非常长。

https://github.com/fluxation87/pup/blob/master/game%20code

一些背景大致相同:它最初是作为基于文本的游戏编写的,然后我听说pygame想弄清楚如何将游戏放入窗口。因此,首先我创建了所有代码,即使在有声音效果/音乐的情况下,它们也可以很好地运行,但是在尝试找出pygame的游戏循环之后,我不知道如何更改背景图片。

我有5个场景,我想要5个图像,当我用用户输入更改场景时,这些场景就会改变。即使是现在,音乐仍在变化,但是在“命名”场景中将第一个图像闪烁后,背景图像仍然没有变化。我不太了解pygame循环如何与当前使用的游戏引擎一起工作。我知道在打印内容测试时它正在pygame循环中循环,但是当尝试添加图像或说场景==命名:blit图像时,它是无效的。我还尝试将图像放入每个场景。我还尝试将图像放入一个名为background的类中,其中包含所有5种场景和图像。无法解决。

任何帮助将不胜感激!-M

python loops pygame game-engine
1个回答
0
投票

这里是如何使用场景的示例。这不是做到这一点的唯一方法,但尽管它易于使用/理解。我还在您的代码中注意到您使用了input()。如果您使用的是窗口,这很烦人,所以我还添加了一个按钮和一个文本框输入,可以使用。

import pygame as pg

pg.init() # initialize the pg
win = pg.display.set_mode((1080, 720)) # create the game window
pg.display.set_caption("Puppy Love")
white = (255,255,255)
black = (0,0,0)

clock = pg.time.Clock()
fps = 60


class Scene:
    def __init__(self):
        self.sceneNum = 1

    def Scene_1(self):
        win.fill(white)
        if next_scene_button.draw(): #if clicked button
            self.sceneNum += 1
        name_Box.Draw()
        #anything else you want to draw

    def Scene_2(self):
        win.fill((0,0,255))
        if next_scene_button.draw():
            self.sceneNum = 1
        #...

    def Draw_Scene(self):
        if self.sceneNum == 1:
            self.Scene_1()
        elif self.sceneNum == 2:
            self.Scene_2()


class Button:
    def __init__(self,x,y,w,h,text):
        self.x = x
        self.y = y
        self.w = w
        self.h = h
        self.text = text
        self.font = pg.font.Font(pg.font.match_font("Calibri"),20)

    def draw(self):
        button_clicked = False
        col = (150,150,150)
        if mouse_pos[0] > self.x and mouse_pos[0] < self.x + self.w:
            if mouse_pos[1] > self.y and mouse_pos[1] < self.y + self.h:
                col = (200,200,200)
                if click:
                    button_clicked = True
        pg.draw.rect(win,col,(self.x,self.y,self.w,self.h))
        obj = self.font.render(self.text,True, black)
        win.blit(obj,(self.x + (self.w-obj.get_width())//2,self.y + (self.h-obj.get_height())//2)) #center the text
        return button_clicked #return if the button was clicked or not

class TextBox:

    def __init__(self,x = 0, y = 0, w = 0, h = 0, text = "", background = False, font_size = 30, font = "Calibri", text_colour = (0,0,0)):
        self.x = x
        self.y = y
        self.w = w
        self.h = h
        self.text_colour = text_colour
        self.text = text
        self.background = background
        self.font = pg.font.Font(pg.font.match_font(font),font_size)

    def Add_char(self,key):
        if key == 8:
            self.text = self.text[:-1]
        else:
            char = pg.key.name(key)
            self.text += char

    def Draw(self):
        if self.background:
            pg.draw.rect(win, self.background, (self.x,self.y,self.w,self.h))
        obj = self.font.render(self.text,True,self.text_colour)
        win.blit(obj,(self.x,self.y))

    def Get_pos(self):
        return (self.x,self.y)


name_Box = TextBox(x=100, y=200, w=150, h=40, background=(200,200,200))

Scenes = Scene()
next_scene_button = Button(400,400,100,50,"next scene")

click = False
mouse_pos = [0,0]

running = True
while running:
    clock.tick(fps)
    Scenes.Draw_Scene()


    mouse_pos = pg.mouse.get_pos()
    click = False

    pg.display.update() #no diference between .flip() and .update()
    for event in pg.event.get():
        if event.type == pg.QUIT:
            pg.quit()
            running = False    
        if event.type == pg.MOUSEBUTTONDOWN:
            click = True
        if event.type == pg.KEYDOWN:
            if Scenes.sceneNum == 1:
                name_Box.Add_char(event.key)

要摆脱的主要问题是仅使用一个pygame.display.flip()。希望您可以使用它来改进您的程序

© www.soinside.com 2019 - 2024. All rights reserved.