如何在pygame中一起制作两个文件?

如何解决如何在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)
red = (255,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,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) #display the background 

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

        writeIns('INSTRUCTIONS')
        
        pygame.display.update()

        clock.tick(60)

    pygame.quit()
       

initGame()
runGame()

这是说明页面

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)

def writeExit(text):
    global gamePad
    textfont = pygame.font.Font('Ranchers-Regular.ttf',20) #textfont of the game message 
    text = textfont.render(text,red) #black text
    textpos = (625,60)
    gamePad.blit(text,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,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)
        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()    

ghome.py

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)
        self.red = (255,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,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) #display the background 
            pygame.draw.rect(gamePad,self.black,(120,400,250,70))
            self.writeIns('INSTRUCTIONS')
            pygame.display.update()
            clock.tick(60)

        pygame.quit()

ginstructions.py

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)

    def writeExit(self,20) #textfont of the game message 
        text = textfont.render(text,self.red) #black text
        textpos = (625,60)
        gamePad.blit(text,textpos) #print the text
        pygame.display.update()
        
    def drawObject(self,obj,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,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)
            pygame.display.update()

            clock.tick(60)
            
        pygame.quit()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?