2017-11-22 3 views
1

파이 플라이 파일과 파이 게임이 포함 된 별도의 파일이 있습니다. 둘 다 별도로 작동합니다. 플라스크를 결합하여 웹 페이지에서 내 링크를 누르면 외부 파일을 실행하기 시작합니다. 어떻게 호출합니까?파이썬 플라스크 게임이 초기화되지 않습니다

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

여기 당신이 당신의 게임을 호출 할 두 가지 방법이 약간 다른 것 같다 주요 게임 파일

+0

현재 어떻게됩니까? –

+0

pygame.error : 비디오 시스템이 초기화되지 않았습니다. – zarrexx

+0

게임을 실행하면 완벽하게 작동합니다. – zarrexx

답변

1

입니다.

$ python going.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() 

당신은 몇 가지를 놓치고 같은 것 같다 성공적인 방법, 설정. 내 생각 엔 귀하의 going.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() 
+0

불행히도 나는 같은 오류가 발생합니다. pygame.error : 비디오 시스템이 초기화되지 않았습니다. – zarrexx

+0

나는 명령을 엉망으로 만들었지 만 고쳐졌습니다. 또한 오류가 발생할 때마다 스택 추적을 따르십시오. 유용한 정보가 많이 있습니다. –

+0

이제는 창문이 열리고 작은 문제는 하나뿐입니다. "UnboundLocalError : 할당하기 전에 로컬 변수 'background'가 참조되었습니다." 주문 변경을 시도했으나 작동하지 않습니다. – zarrexx

관련 문제