Python 如何在pygame中同时生成两个文件?

Python 如何在pygame中同时生成两个文件?,python,python-3.x,button,pygame,Python,Python 3.x,Button,Pygame,我正在使用pygame制作一个游戏,我将每个部分制作在一个单独的文件中,例如主页、说明页、实际游戏等等,我不知道如何将它们放在一起。我曾考虑过使用 此代码 from graphics import* w = GraphWin("Window", 600,400) playing = True while playing: click = w.getMouse() potato = click.getX() carrot = click.getY()

我正在使用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()
这是说明页

import pygame
import sys
import random 
from time import sleep


padWidth = 500 #the width the of game 
padHeight = 600 # the length of the game
red = (255,0,0)

def writeExit(text):
    global gamePad
    textfont = pygame.font.Font('Ranchers-Regular.ttf', 20) #textfont of the game message 
    text = textfont.render(text, True, red) #black text
    textpos = (625, 60)
    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, instructions
    pygame.init()
    gamePad = pygame.display.set_mode((padWidth, padHeight))
    pygame.display.set_caption('shooting game') #the title of the game
    instructions = pygame.image.load('instructions.png') #import the background image
    clock = pygame.time.Clock()

def runGame():
    global gamePad, clock, instructions
    onGame = False
    while not onGame:
        for event in pygame.event.get():
            if event.type in [pygame.QUIT]:
                pygame.quit()
                sys.exit()
        drawObject(instructions, 0, 0)
        pygame.display.update()

        clock.tick(60)
        
    pygame.quit()

initGame()
runGame()

因此,我希望,当按下主页上的按钮时,它将移动到说明页。

要组合文件,您需要进行两个关键更改:

  • 将其他源文件导入主文件
  • 将导入的源转换为类,以防止变量和函数重叠
这是工作代码。起始文件是gamex.py,导入的文件是ghome.py和ginstructions.py

gamex.py

from graphics import *

# import home and instructions
from ghome import home
from ginstructions import instructions


# call home screen
h = home()  # create instance of home
h.initGame()
h.runGame()

# call instructions screen
i = instructions()  # create instance of instructions
i.initGame()
i.runGame()


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()    
金丝雀

import pygame
import sys
import random 
from time import sleep

class instructions():
    def __init__(self):
        self.padWidth = 500 #the width the of game 
        self.padHeight = 600 # the length of the game
        self.red = (255,0,0)

    def writeExit(self,text):
        global gamePad
        textfont = pygame.font.Font('Ranchers-Regular.ttf', 20) #textfont of the game message 
        text = textfont.render(text, True, self.red) #black text
        textpos = (625, 60)
        gamePad.blit(text, textpos) #print the text
        pygame.display.update()
        
    def drawObject(self,obj, x, y):
        global gamePad
        gamePad.blit(obj, (int(x), int(y)))

    def initGame(self):
        global gamePad, clock, instructions
        pygame.init()
        gamePad = pygame.display.set_mode((self.padWidth, self.padHeight))
        pygame.display.set_caption('shooting game') #the title of the game
        instructions = pygame.image.load('instructions.png') #import the background image
        clock = pygame.time.Clock()

    def runGame(self):
        global gamePad, clock, instructions
        onGame = False
        while not onGame:
            for event in pygame.event.get():
                if event.type in [pygame.QUIT]:
                    pygame.quit()
                    sys.exit()
            if event.type == pygame.MOUSEBUTTONDOWN: break
            self.drawObject(instructions, 0, 0)
            pygame.display.update()

            clock.tick(60)
            
        pygame.quit()

您可以使用代码
从文件名导入*
(将
文件名
替换为python文件名)将一个文件导入到另一个文件。这将使您能够访问python文件中的所有函数。不过,您最好将所有代码放在一个python文件中。噢,但它不起作用,谢谢tho@talktalk将它们导入到一个文件中,然后执行以下操作:
from graphics import* 
import pygame
import sys
import random 
from time import sleep

class home():
    def __init__(self):
        self.padWidth = 500 #the width the of game 
        self.padHeight = 600 # the length of the game
        self.white = (255,255,255)
        self.black = (0,0,0)
        self.red = (255,0,0)

    def writeIns(self, text):
        global gamePad
        textfont = pygame.font.Font('Ranchers-Regular.ttf', 29) #textfont of the game message 
        text = textfont.render(text, True, self.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(self):
        global gamePad, clock, background
        pygame.init()
        gamePad = pygame.display.set_mode((self.padWidth, self.padHeight))
        pygame.display.set_caption('Shooting game') #the title of the game
        clock = pygame.time.Clock()

    def runGame(self):
        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()
            if event.type == pygame.MOUSEBUTTONDOWN: break

#            self.drawObject(background, 0, 0) #display the background 
            pygame.draw.rect(gamePad, self.black, (120,400,250,70))
            self.writeIns('INSTRUCTIONS')
            pygame.display.update()
            clock.tick(60)

        pygame.quit()
import pygame
import sys
import random 
from time import sleep

class instructions():
    def __init__(self):
        self.padWidth = 500 #the width the of game 
        self.padHeight = 600 # the length of the game
        self.red = (255,0,0)

    def writeExit(self,text):
        global gamePad
        textfont = pygame.font.Font('Ranchers-Regular.ttf', 20) #textfont of the game message 
        text = textfont.render(text, True, self.red) #black text
        textpos = (625, 60)
        gamePad.blit(text, textpos) #print the text
        pygame.display.update()
        
    def drawObject(self,obj, x, y):
        global gamePad
        gamePad.blit(obj, (int(x), int(y)))

    def initGame(self):
        global gamePad, clock, instructions
        pygame.init()
        gamePad = pygame.display.set_mode((self.padWidth, self.padHeight))
        pygame.display.set_caption('shooting game') #the title of the game
        instructions = pygame.image.load('instructions.png') #import the background image
        clock = pygame.time.Clock()

    def runGame(self):
        global gamePad, clock, instructions
        onGame = False
        while not onGame:
            for event in pygame.event.get():
                if event.type in [pygame.QUIT]:
                    pygame.quit()
                    sys.exit()
            if event.type == pygame.MOUSEBUTTONDOWN: break
            self.drawObject(instructions, 0, 0)
            pygame.display.update()

            clock.tick(60)
            
        pygame.quit()