Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python烧瓶游戏未初始化_Python_Flask_Pygame - Fatal编程技术网

Python烧瓶游戏未初始化

Python烧瓶游戏未初始化,python,flask,pygame,Python,Flask,Pygame,我有我的烧瓶文件和一个单独的包含pygame的文件。两者都是分开工作的,我如何组合烧瓶,以便当我按下网页上的链接时,它开始运行外部文件,我将如何调用它 from flask import Flask, render_template import going app = Flask(__name__) @app.route('/') def index(): return render_template('index.html') @app.route('/my-link/') def my

我有我的烧瓶文件和一个单独的包含pygame的文件。两者都是分开工作的,我如何组合烧瓶,以便当我按下网页上的链接时,它开始运行外部文件,我将如何调用它

from flask import Flask, render_template
import going
app = Flask(__name__)

@app.route('/')
def index():
return render_template('index.html')

@app.route('/my-link/')
def my_link():
   return going.main()

if __name__ == '__main__':
   app.run(debug=True)
目前,我正在尝试运行main()方法来初始化程序

import pygame
##1100 * 800
size = [1100, 800]
score1 = 0
score2 = 0
blue = (100, 149, 237)
black = (0, 0, 0)
brown = (165,42,42)
white = (255, 255, 255)
green =(0,100,0)
red = (255,0,0)
dark_red = (200,0,0)
grey = (100,100,100)
other_grey = (0,0,100)
background = 'Mahogany.jpg'
pass_count = 0
player = 1
clock = pygame.time.Clock()

class Player(object):
    def ___init__(self,id):
        self.id = 1
    def quitGame(self):
        pygame.quit()
        quit()
    def pass_turn(self):
        global pass_count
        pass_count += 1
        if pass_count == 2:
            quitGame()      
    def score(player_text, score):
        return player_text + str(score) 


class Stone(object):
    def __init__(self,board,position,color):
        self.board = board
        self.position = position
        self.color = color
        self.placeStone()
    def placeStone(self):
        coords = (self.position[0] * 50,  self.position[1] * 50)
        pygame.draw.circle(self.board,self.color,coords,20,0)

        pygame.display.update()

class Board(object):    
    def draw_board(self):
        for i in range(12):
                for j in range(12):
                    rect = pygame.Rect(55 + (50 * i), 100 + (50 * j), 50, 50)
                    pygame.draw.rect(background, blue, rect, 1)
        screen.blit(background, (0,0))
        pygame.display.update()

    def text_objects(self,text, font):
        textSurface = font.render(text, True, black)

        return textSurface, textSurface.get_rect()

    def button(self,msg,x,y,w,h,ic,ac,action = None):
        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()
        if x+w > mouse[0] > x and y+h > mouse[1] > y:
            pygame.draw.rect(screen, ac,(x,y,w,h))

            if click[0] == 1 and action != None:
                action()
        else:
            pygame.draw.rect(screen, ic,(x,y,w,h))

        smallText = pygame.font.Font("freesansbold.ttf",20)
        textSurf, textRect = self.text_objects(msg, smallText)
        textRect.center = ( (x+(w/2)), (y+(h/2)) )
        screen.blit(textSurf, textRect)
    def game_intro(self):

        intro = True
        while intro:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    quit()

            screen.blit(background, (0,0))
            largeText = pygame.font.SysFont("comicsansms",60)
            TextSurf, TextRect = self.text_objects("GONLINE", largeText)
            TextRect.center = ((1100/2),(800/2))
            screen.blit(TextSurf, TextRect)

            self.button("Play!",200,500,100,100,grey,other_grey,self.play_game)
            self.button("Quit!",700,500,100,100,red,dark_red,Player.quitGame)

            pygame.display.update()
            clock.tick(15)
    def play_game(self):
        width = 20
        height = 20 
        space_between = 5 

        global player
        finish = False
        self.draw_board()
        while not finish:


            for event in pygame.event.get():
                if event.type == pygame.QUIT:  
                    finish = True  
                elif event.type == pygame.MOUSEBUTTONDOWN and player == 1:
                    position = pygame.mouse.get_pos()
                    if (event.button == 1) and (position[0] > 55 and position[0] < 710) and (position[1] > 100 and position[1] < 750):
                        x = int(round(((position[0]) / 50.0), 0))
                        y = int(round(((position[1]) / 50.0), 0))
                        Stone(screen,(x,y),white)

                        player = 2
                elif event.type == pygame.MOUSEBUTTONDOWN and player == 2:
                    position = pygame.mouse.get_pos()
                    if (event.button == 1) and(position[0] > 55 and position[0] < 710) and (position[1] > 100 and position[1] < 750):
                        x = int(round(((position[0]) / 50.0), 0))
                        y = int(round(((position[1] ) / 50.0), 0))
                        Stone(screen,(x,y),black)

                        player = 1




            clock.tick(60)
            self.button("Pass!",750,200,100,100,grey,other_grey,Player.pass_turn)
            self.button("Quit!",950,200,100,100,red,dark_red,Player.quitGame)
            self.button(score("Player 1: ", score1),750,400,300,110,white,white)
            self.button(score("Player 2: ",score2),750,600,300,110,white, white)
            pygame.display.update()


        pygame.quit()

def main():
    player = Player()
    board = Board()
    board.game_intro()  

if __name__ == "__main__":
    pygame.init()
    screen = pygame.display.set_mode(size, 0, 32) 
    pygame.display.set_caption("Go_Online")
    background = pygame.image.load(background).convert()
    main()
导入pygame
##1100 * 800
大小=[1100800]
分数1=0
分数2=0
蓝色=(100149237)
黑色=(0,0,0)
布朗=(165,42,42)
白色=(255,255,255)
绿色=(0100,0)
红色=(255,0,0)
深红色=(200,0,0)
灰色=(100100)
其他灰色=(0,0100)
背景='Mahogany.jpg'
通过计数=0
玩家=1
clock=pygame.time.clock()
类播放器(对象):
定义初始化(self,id):
self.id=1
游戏名称(自我):
pygame.quit()
退出
def通过掉头(自身):
全局通过计数
通过计数+=1
如果通过计数=2:
quitGame()
def分数(玩家文本,分数):
返回玩家_text+str(分数)
石类(实物):
定义初始值(自身、电路板、位置、颜色):
self.board=board
self.position=位置
self.color=颜色
砂石
def砂石(自身):
坐标=(自身位置[0]*50,自身位置[1]*50)
pygame.draw.circle(self.board,self.color,coords,20,0)
pygame.display.update()
班级委员会(对象):
def牵引板(自):
对于范围(12)内的i:
对于范围(12)内的j:
rect=pygame.rect(55+(50*i),100+(50*j),50,50)
pygame.draw.rect(背景,蓝色,rect,1)
屏幕光点(背景,(0,0))
pygame.display.update()
def text_对象(自身、文本、字体):
textSurface=font.render(文本,真,黑色)
返回textSurface,textSurface.get_rect()
def按钮(自身、消息、x、y、w、h、ic、ac、动作=无):
mouse=pygame.mouse.get_pos()
click=pygame.mouse.get_pressed()
如果x+w>鼠标[0]>x和y+h>鼠标[1]>y:
pygame.draw.rect(屏幕,ac,(x,y,w,h))
如果单击[0]==1并执行操作!=无:
行动()
其他:
pygame.draw.rect(屏幕,ic,(x,y,w,h))
smallText=pygame.font.font(“freesansbold.ttf”,20)
textSurf,textRect=self.text\u对象(msg,smallText)
textRect.center=((x+(w/2)),(y+(h/2)))
screen.blit(textSurf,textRect)
def游戏介绍(自我):
简介=正确
而简介:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
退出
屏幕光点(背景,(0,0))
largeText=pygame.font.SysFont(“comicsansms”,60)
TextSurf,TextRect=self.text\u对象(“GONLINE”,largeText)
TextRect.center=((1100/2)、(800/2))
screen.blit(TextSurf,TextRect)
self.button(“Play!”,200500100100,灰色,其他灰色,self.Play\u游戏)
self.button(“退出!”,700500100100,红色,深红色,玩家退出游戏)
pygame.display.update()
时钟滴答(15)
def游戏(自我):
宽度=20
高度=20
间距_=5
全球玩家
完成=错误
自绘制板()
虽然没有完成:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
完成=真
elif event.type==pygame.MOUSEBUTTONDOWN和player==1:
position=pygame.mouse.get_pos()
如果(event.button==1)和(位置[0]>55且位置[0]<710)和(位置[1]>100且位置[1]<750):
x=int(圆形((位置[0])/50.0,0))
y=int(圆形((位置[1])/50.0,0))
石头(屏风,(x,y),白色)
玩家=2
elif event.type==pygame.MOUSEBUTTONDOWN和player==2:
position=pygame.mouse.get_pos()
如果(event.button==1)和(位置[0]>55且位置[0]<710)和(位置[1]>100且位置[1]<750):
x=int(圆形((位置[0])/50.0,0))
y=int(圆形((位置[1])/50.0,0))
石头(屏风,(x,y),黑色)
玩家=1
时钟滴答(60)
self.button(“传球!”,750200100100,灰色,其他灰色,玩家。传球/转身)
self.button(“退出!”,9502000100100,红色,深红色,玩家退出游戏)
self.按钮(分数(“玩家1:”,分数1),750400300110,白色,白色)
self.按钮(分数(“玩家2:”,分数2),750600300110,白色,白色)
pygame.display.update()
pygame.quit()
def main():
player=player()
董事会
棋盘游戏介绍()
如果名称=“\uuuuu main\uuuuuuuu”:
pygame.init()
screen=pygame.display.set_模式(大小0,32)
pygame.display.set_标题(“联机”)
background=pygame.image.load(background.convert())
main()

这是主要的游戏文件

看来你尝试调用游戏的两种方式略有不同

成功的方式,似乎是
$python go.py

运行此代码

if __name__ == "__main__":
    pygame.init()
    screen = pygame.display.set_mode(size, 0, 32) 
    pygame.display.set_caption("Go_Online")
    background = pygame.image.load(background).convert()
    main()
触发时,烧瓶路径将运行此

return going.main()
您缺少一些设置。我猜你的
go.py
的底部应该是这样的

def main():
    pygame.init()
    screen = pygame.display.set_mode(size, 0, 32) 
    pygame.display.set_caption("Go_Online")
    background = pygame.image.load(background).convert()
    player = Player()
    board = Board()
    board.game_intro()  


if __name__ == '__main__':
    main()

当前发生了什么?pygame.error:视频系统未初始化单独运行游戏效果很好。您能否演示如何运行它,使其完美运行?另外,
going
的内容可能会有所帮助。我可以私下发给你吗?这是一项大学作业。我不想发布iTunes,但我也会遇到同样的错误。pygame.error:视频系统未初始化。我搞乱了订单,但已修复。另外,每当你得到一个e时,跟踪你的堆栈跟踪