如何在pygame中将两个文件放在一起?

我正在使用 pygame 制作游戏,我将每个部分制作在一个单独的文件中,例如主页、说明页面、实际游戏等,但我不知道如何将它们放在一起。我考虑过使用这段代码


from graphics import*


w = GraphWin("Window", 600,400)


playing = True

while playing:

    click = w.getMouse()

    potato = click.getX()

    carrot = click.getY()


    if potato < 300 and carrot < 200:

        newWin = GraphWin("New", 200, 200)

    if potato > 300 and carrot > 200:

        w.setBackground("blue")

    if potato < 300 and carrot > 200:

        playing = False


    w.close()

    n = GraphWin("Homepage", 500, 200)

    n.getMouse()

    n.close()

但我仍然不知道如何将它们放在一起。你能帮我看看如何把这两个文件放在一起吗?这个文件是主页:


from graphics import* 

import pygame

import sys

import random 

from time import sleep


padWidth = 500 #the width the of game 

padHeight = 600 # the length of the game

white = (255,255,255)

black = (0,0,0)

red = (255,0,0)


def writeIns(text):

    global gamePad

    textfont = pygame.font.Font('Ranchers-Regular.ttf', 29) #textfont of the game message 

    text = textfont.render(text, True, red) #red text

    textpos = (158,417)

    gamePad.blit(text, textpos) #print the text

    pygame.display.update()

    

def drawObject(obj, x, y):

    global gamePad

    gamePad.blit(obj, (int(x), int(y)))


def initGame():

    global gamePad, clock, background

    pygame.init()

    gamePad = pygame.display.set_mode((padWidth, padHeight))

    pygame.display.set_caption('Shooting game') #the title of the game

    clock = pygame.time.Clock()


def runGame():

    global gamePad, clock, background

    

    onGame = False

    while not onGame:

        for event in pygame.event.get():

            if event.type in [pygame.QUIT]:

                pygame.quit()

                sys.exit()

                

        drawObject(background, 0, 0) #display the background 


        

        pygame.draw.rect(gamePad, black, (120,400,250,70))


        writeIns('INSTRUCTIONS')

        

        pygame.display.update()


        clock.tick(60)


    pygame.quit()

       


initGame()

runGame()


当年话下
浏览 121回答 1
1回答

PIPIONE

要合并这些文件,您需要进行两个关键更改:将其他源文件导入主文件将导入的源代码转换为类以防止变量和函数重叠这是工作代码。起始文件是 gamex.py,导入文件是 ghome.py 和 ginstructions.py。gamex.pyfrom graphics import *# import home and instructionsfrom ghome import homefrom ginstructions import instructions# call home screenh = home()&nbsp; # create instance of homeh.initGame()h.runGame()# call instructions screeni = instructions()&nbsp; # create instance of instructionsi.initGame()i.runGame()w = GraphWin("Window", 600,400)playing = Truewhile playing:&nbsp; &nbsp; click = w.getMouse()&nbsp; &nbsp; potato = click.getX()&nbsp; &nbsp; carrot = click.getY()&nbsp; &nbsp; if potato < 300 and carrot < 200:&nbsp; &nbsp; &nbsp; &nbsp; newWin = GraphWin("New", 200, 200)&nbsp; &nbsp; if potato > 300 and carrot > 200:&nbsp; &nbsp; &nbsp; &nbsp; w.setBackground("blue")&nbsp; &nbsp; if potato < 300 and carrot > 200:&nbsp; &nbsp; &nbsp; &nbsp; playing = False&nbsp; &nbsp; w.close()&nbsp; &nbsp; n = GraphWin("Homepage", 500, 200)&nbsp; &nbsp; n.getMouse()&nbsp; &nbsp; n.close()&nbsp; &nbsp;&nbsp;ghome.pyfrom graphics import*&nbsp;import pygameimport sysimport random&nbsp;from time import sleepclass home():&nbsp; &nbsp; def __init__(self):&nbsp; &nbsp; &nbsp; &nbsp; self.padWidth = 500 #the width the of game&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; self.padHeight = 600 # the length of the game&nbsp; &nbsp; &nbsp; &nbsp; self.white = (255,255,255)&nbsp; &nbsp; &nbsp; &nbsp; self.black = (0,0,0)&nbsp; &nbsp; &nbsp; &nbsp; self.red = (255,0,0)&nbsp; &nbsp; def writeIns(self, text):&nbsp; &nbsp; &nbsp; &nbsp; global gamePad&nbsp; &nbsp; &nbsp; &nbsp; textfont = pygame.font.Font('Ranchers-Regular.ttf', 29) #textfont of the game message&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; text = textfont.render(text, True, self.red) #red text&nbsp; &nbsp; &nbsp; &nbsp; textpos = (158,417)&nbsp; &nbsp; &nbsp; &nbsp; gamePad.blit(text, textpos) #print the text&nbsp; &nbsp; &nbsp; &nbsp; pygame.display.update()&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; def drawObject(obj, x, y):&nbsp; &nbsp; &nbsp; &nbsp; global gamePad&nbsp; &nbsp; &nbsp; &nbsp; gamePad.blit(obj, (int(x), int(y)))&nbsp; &nbsp; def initGame(self):&nbsp; &nbsp; &nbsp; &nbsp; global gamePad, clock, background&nbsp; &nbsp; &nbsp; &nbsp; pygame.init()&nbsp; &nbsp; &nbsp; &nbsp; gamePad = pygame.display.set_mode((self.padWidth, self.padHeight))&nbsp; &nbsp; &nbsp; &nbsp; pygame.display.set_caption('Shooting game') #the title of the game&nbsp; &nbsp; &nbsp; &nbsp; clock = pygame.time.Clock()&nbsp; &nbsp; def runGame(self):&nbsp; &nbsp; &nbsp; &nbsp; global gamePad, clock, background&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; onGame = False&nbsp; &nbsp; &nbsp; &nbsp; while not onGame:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for event in pygame.event.get():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if event.type in [pygame.QUIT]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pygame.quit()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sys.exit()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if event.type == pygame.MOUSEBUTTONDOWN: break#&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.drawObject(background, 0, 0) #display the background&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pygame.draw.rect(gamePad, self.black, (120,400,250,70))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.writeIns('INSTRUCTIONS')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pygame.display.update()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clock.tick(60)&nbsp; &nbsp; &nbsp; &nbsp; pygame.quit()ginstructions.pyimport pygameimport sysimport random&nbsp;from time import sleepclass instructions():&nbsp; &nbsp; def __init__(self):&nbsp; &nbsp; &nbsp; &nbsp; self.padWidth = 500 #the width the of game&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; self.padHeight = 600 # the length of the game&nbsp; &nbsp; &nbsp; &nbsp; self.red = (255,0,0)&nbsp; &nbsp; def writeExit(self,text):&nbsp; &nbsp; &nbsp; &nbsp; global gamePad&nbsp; &nbsp; &nbsp; &nbsp; textfont = pygame.font.Font('Ranchers-Regular.ttf', 20) #textfont of the game message&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; text = textfont.render(text, True, self.red) #black text&nbsp; &nbsp; &nbsp; &nbsp; textpos = (625, 60)&nbsp; &nbsp; &nbsp; &nbsp; gamePad.blit(text, textpos) #print the text&nbsp; &nbsp; &nbsp; &nbsp; pygame.display.update()&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; def drawObject(self,obj, x, y):&nbsp; &nbsp; &nbsp; &nbsp; global gamePad&nbsp; &nbsp; &nbsp; &nbsp; gamePad.blit(obj, (int(x), int(y)))&nbsp; &nbsp; def initGame(self):&nbsp; &nbsp; &nbsp; &nbsp; global gamePad, clock, instructions&nbsp; &nbsp; &nbsp; &nbsp; pygame.init()&nbsp; &nbsp; &nbsp; &nbsp; gamePad = pygame.display.set_mode((self.padWidth, self.padHeight))&nbsp; &nbsp; &nbsp; &nbsp; pygame.display.set_caption('shooting game') #the title of the game&nbsp; &nbsp; &nbsp; &nbsp; instructions = pygame.image.load('instructions.png') #import the background image&nbsp; &nbsp; &nbsp; &nbsp; clock = pygame.time.Clock()&nbsp; &nbsp; def runGame(self):&nbsp; &nbsp; &nbsp; &nbsp; global gamePad, clock, instructions&nbsp; &nbsp; &nbsp; &nbsp; onGame = False&nbsp; &nbsp; &nbsp; &nbsp; while not onGame:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for event in pygame.event.get():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if event.type in [pygame.QUIT]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pygame.quit()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sys.exit()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if event.type == pygame.MOUSEBUTTONDOWN: break&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.drawObject(instructions, 0, 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pygame.display.update()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clock.tick(60)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; pygame.quit()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python