【发布时间】:2020-08-15 07:51:54
【问题描述】:
我正在使用 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()
这个是说明页
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()
所以,我希望,当从主页按下按钮时,它会移动到说明页面。
【问题讨论】:
-
您可以使用代码
from FileName import *将一个文件导入另一个文件(将FileName替换为python文件的名称)。这将使您可以访问 python 文件中的所有函数。 -
你最好把所有代码放在一个 python 文件中。
-
哦,但它不起作用,谢谢你
-
@talktalk 将它们导入一个文件,然后执行以下操作:stackoverflow.com/a/63319083/6486738
标签: python python-3.x button pygame