亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在pygame中將兩個文件放在一起?

如何在pygame中將兩個文件放在一起?

當年話下 2023-05-23 10:19:47
我正在使用 pygame 制作游戲,我將每個部分制作在一個單獨的文件中,例如主頁、說明頁面、實際游戲等,但我不知道如何將它們放在一起。我考慮過使用這段代碼from graphics import*w = GraphWin("Window", 600,400)playing = Truewhile 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 pygameimport sysimport random from time import sleeppadWidth = 500 #the width the of game padHeight = 600 # the length of the gamewhite = (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()
查看完整描述

1 回答

?
PIPIONE

TA貢獻1829條經驗 獲得超9個贊

要合并這些文件,您需要進行兩個關鍵更改:

  • 將其他源文件導入主文件

  • 將導入的源代碼轉換為類以防止變量和函數重疊

這是工作代碼。起始文件是 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,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()

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,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()


查看完整回答
反對 回復 2023-05-23
  • 1 回答
  • 0 關注
  • 206 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號