无法弄清楚如何在更改时在 Pygame 中运行背景场景"levels"



我是python的新手,想创建一个游戏来测试一些知识。请坦白我的错误。如果可能的话,我也非常感谢写一个例子,这样我就可以理解这个概念了。

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

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

一些背景大致相同:它最初是作为一款只基于文本的游戏编写的,后来我听说pygame想知道如何将游戏放入窗口。因此,首先我创建了所有的代码,即使有音效/音乐,这些代码也运行得很好,但在试图弄清楚pygame的游戏循环后,我不知道如何更改背景图像。

我有5个场景,我想要5个图像,当我通过用户输入更改场景时,这些图像会发生变化。即使是现在,音乐也会改变,但在"命名"场景中闪电般地播放第一张后,背景图像不会改变。我真的不明白pygame循环是如何与我目前拥有的游戏引擎协同工作的。我知道它在pygame循环中循环,因为我用打印东西来测试它,但当试图添加图像或说如果场景==命名:blit图像时,它不起作用。我还试着把图像放进每一个场景中。我还试着把这些图像放在一个名为背景的类中,每个类都有5个场景和图像。搞不清楚。

如有任何帮助,我们将不胜感激!-M

下面是一个如何使用场景的示例。这不是唯一的方法,但我认为它很容易使用/理解。我还注意到,在您的代码中,您使用了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()。希望你能用它来改进你的程序

最新更新