2017-03-07 4 views
0

私はpygameを使ってPythonでゲームを書いています。ゲームループ内の変数とその後のgameloopの関数を定義すると、

import pygame 
import random 
pygame.init() 


white=(255,255,255) 
black = (0,0,0) 
red = (255,0,0) 
green = (0,255,0) 
blue = (0,0,255) 



display_width=800 
display_hieght=600 


gameDisplay= pygame.display.set_mode((display_width,display_hieght)) 
pygame.display.set_caption('slither') 







clock=pygame.time.Clock() 
font=pygame.font.SysFont(None,25) 

lead_x_change = 0 
lead_y_change = 0 

def message_to_screen(msg,colour,x,y): 
    screen_text = font.render(msg,True,colour) 
    gameDisplay.blit(screen_text,[x,y]) 
def gameloop(): 
    gameExit = False 
    gameOver = False 
    lead_x = display_width/2 
    lead_y = display_hieght - 5 
    blocksize=int(10) 
    FPS = 100 
    score=0 

    def shoot(event): 
     pos = pygame.mouse.get_pos() 
     if pos >= (590, 400): 
      lead_x_change += 1 
      lead_y_change += 0.1 
     if pos >= (590, 400) and pos <= (580, 0): 
      lead_x_change += 1 
      lead_y_change += 0.2 

    while not gameExit: 
     pygame.display.update() 
     while gameOver == True: 
      gameDisplay.fill(white) 
      message_to_screen('Game over, press Y to play again or N to quit',red,display_width*0.25,display_hieght/2) 
      pygame.display.update() 

      for event in pygame.event.get(): 
       if event.type == pygame.KEYDOWN: 
        if event.key== pygame.K_n: 
         gameExit = True 
         gameOver = False 

     for event in pygame.event.get(): 
      if event.type==pygame.QUIT: 
       gameExit = True 
      if event.type==pygame.MOUSEBUTTONDOWN: 
        shoot(pygame.MOUSEBUTTONUP) 




     if lead_x >display_width -50: 
      lead_x=display_width - 50 
     elif lead_x <0: 
      lead_x=0 

     lead_x += lead_x_change 
     lead_y += lead_y_change 



     gameDisplay.fill(white) 
     block=pygame.draw.rect(gameDisplay,red,(lead_x,lead_y,50,5)) 
     obj=pygame.draw.circle(gameDisplay,black,(425,595),blocksize) 
     pygame.Rect(block) 
     pygame.Rect(obj) 



     message_to_screen('score='+str(score),black,0,0) 

     pygame.display.update() 


    clock.tick(FPS) 


    pygame.quit() 
    quit() 
gameloop() 

このコードでは、変数lead_x_changelead_y_change中に書かれています:私はここ

は、コードの重要な部分である私のゲームループにし、いくつかの変数と私のゲームループの一部の機能を作りますシュート機能はまだ参照されていません。

+0

完全なコードを表示してください。理由なくグローバル変数を使用することはお勧めしませんし、むしろ関数に必要な引数を渡します。 – skrx

答えて

0

lead_x_changeおよびlead_y_changeは、gameloop関数のローカル変数であり、その関数内でしかアクセスできません。 shoot関数はそれらにアクセスできません。つまり、+=オペレータが実行しようとしているものです。

shootにアクセス権を持たせたい場合は、グローバル変数にすることをお勧めします。

lead_x_change = 0 # global variable 
lead_y_change = 0 # global variable 

def gameloop(): 
    gameExit = False 
    gameOver = False 
    lead_x = display_width/2 
    lead_y = display_hieght - 5 
    blocksize=int(10) 
    FPS = 100 
    score=0 

def shoot(event): 
    global lead_x_change, lead_y_change 
    # Variable in global scope can be only read by a function, unless specially declared as global (above) 
    pos = pygame.mouse.get_pos() 
    if pos >= (590, 400): 
     lead_x_change += 1 
     lead_y_change += 0.1 
    if pos >= (590, 400) and pos <= (580, 0): 
     lead_x_change += 1 
     lead_y_change += 0.2 

globalキーワードにdocumentationを参照してください。

関連する問題