2017-05-27 61 views
-2

我寫一個小遊戲閃避和我的想法是創造一種通電的,這將重置對象(在這種情況下Spikeball)的起點,讓他們兩倍big.When玩家點擊它打印出「命中」的塊,但不知何故值不會改變!我正在使用python 2.7。變化的變量沒有任何影響

這裏是我的代碼:

import pygame 
import os 
import shutil 
import time 
from PIL import Image,ImageDraw 
import random 

    pygame.init() 

    display_hight = 1100 
    display_width = 1100 

    h=0 
    g=0 
    r=0 
    t=0 
    j=0 
    k=0 
    m=0 
    l=0 
    q=0 
    w=0 
    f=0 
    s=0 

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

    gameDisplay = pygame.display.set_mode((display_width,display_hight)) 
    pygame.display.set_caption("ZOMPS") 
    clock = pygame.time.Clock() 

    mydir = os.path.dirname('/home/arne/Desktop/Python-project/Game/Demonsave.png') 
    playerImg = pygame.image.load(os.path.join(mydir,'Demonsave.png')) 
    playerImg = pygame.transform.scale(playerImg,(180,200)) 

    mydir1 = os.path.dirname('/home/arne/Desktop/Python-project/Block.png') 
    blockImg = pygame.image.load(os.path.join(mydir1,'Block.png')) 


    def spikeball(m,l,spike_width,spike_hight,color): 
     pygame.draw.ellipse(gameDisplay,color,[m,l,spike_width,spike_hight]) 

    def spikeball2(j,k,spike_width,spike_hight,color): 
     pygame.draw.ellipse(gameDisplay,color,[j,k,spike_width,spike_hight]) 

    def spikeball3(q,w,spike_width,spike_hight,color): 
     pygame.draw.ellipse(gameDisplay,color,[q,w,spike_width,spike_hight]) 

    def spikeball1(f,s,spike_width,spike_hight,color): 
     pygame.draw.ellipse(gameDisplay,color,[f,s,spike_width,spike_hight]) 

    def spikeball4(r,t,spike_width,spike_hight,color): 
     pygame.draw.ellipse(gameDisplay,color,[r,t,spike_width,spike_hight]) 

    def power(): 
      p=1 
      if p == 1: 
        spike_width=160 
        spike_hight=160 
        q=-100 
      w=random.randrange(90,display_width-90) 
      r=random.randrange(90,display_width-90) 
       t=-200 
       f=random.randrange(90,display_width-90) 
       s=-400 
       start_m=random.randrange(90,display_width-90) 
       start_l =-800 

      else: 
        print('fail') 


    def lost(): 
     message_display('You lost!') 

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

    def message_display(text): 
     largeText = pygame.font.Font('freesansbold.ttf',50) 
     TextSurf,TextRect =text_objects(text,largeText) 
     TextRect.center = (display_width/2,display_hight/2) 
     gameDisplay.blit(TextSurf,TextRect) 
     pygame.display.update() 
     time.sleep(2) 
     game_loop() 

    def player(x,y): 
     gameDisplay.blit(playerImg,(x,y)) 

    def block(g,h): 
     gameDisplay.blit(blockImg,(g,h)) 

    def game_loop(): 
     j=random.randrange(0,display_width) 
     k=-300 
     x = display_width*0.4 
     y = display_hight*0.75 
     x_change=0 
     f=random.randrange(90,display_width-90) 
     s=-400 
     start_m=random.randrange(90,display_width-90) 
     start_l =-800 
     spike_speed=random.randrange(5,10) 
     spike_speed3=random.randrange(5,10) 
     spike_speed2=random.randrange(5,10) 
     spike_speed1=random.randrange(5,10) 
     spike_speed4=random.randrange(5,10) 
     spike_width=80 
     spike_hight=80 
     w=-100 
     q=random.randrange(0,display_width) 
     r=random.randrange(90,display_width-90) 
     t=-200 
     g=random.randrange(90,display_width-90) 
     h=-1300 
     block_speed=random.randrange(3,5) 
     GameExit = False 

     while not GameExit: 
      for event in pygame.event.get(): 
       if event.type == pygame.QUIT: 
        GameExit =True 
       elif event.type == pygame.KEYDOWN: 
        if event.key == pygame.K_d: 
         x_change+=4 
        elif event.key == pygame.K_a: 
         x_change-=4 
        elif event.key == pygame.K_ESCAPE: 
         quit() 
       elif event.type == pygame.KEYUP: 
        if event.key== pygame.K_a or pygame.K_d: 
         x_change=0 
      if y+50 < h+40 and y+160 >h: 
         if x+60 >g and x+60 < g+40 or x+101 > g and x + 101 < g+40: 
          power() 
          print('hit') 

      x+=x_change 
      k+=spike_speed2 
      s+=spike_speed3 
      q+=spike_speed1 
      t+=spike_speed4 
      gameDisplay.fill(red) 
      player(x,y) 
      block(g,h) 
      spikeball(start_m,start_l,spike_width,spike_hight,black) 
      spikeball1(w,q,spike_width,spike_hight,black) 
      spikeball2(j,k,spike_width,spike_hight,black) 
      spikeball3(f,s,spike_width,spike_hight,black) 
      spikeball3(r,t,spike_width,spike_hight,black) 
      start_l+=spike_speed 
      h+=block_speed 

      if x > display_width or x < 0: 
       x = display_width*0.4 
       y = display_hight*0.75 

      elif k > display_hight: 
       j=random.randrange(90,display_width-90) 
       k=-100 
       spike_speed2=random.randrange(6,12) 

      elif h > display_hight: 
       g=random.randrange(90,display_width-90) 
       h=-900 
       block_speed=random.randrange(1,6) 

      elif t > display_hight: 
       r=random.randrange(90,display_width-90) 
       t=-100 
       spike_speed4=random.randrange(6,12) 

      elif start_l > display_hight: 
       start_l =-100 
       start_m=random.randrange(90,display_width-90) 
       spike_speed=random.randrange(5,10) 

      elif s > display_hight: 
       s=-100 
       f=random.randrange(90,display_width-90) 
       spike_speed3=random.randrange(6,12) 

      elif q > display_hight: 
       q=-100 
       w=random.randrange(90,display_width-90) 
       spike_speed1=random.randrange(6,12) 

      elif y+50 < start_l+spike_hight and y+160 >start_l: 
       if x+60 >start_m and x+60 < start_m+spike_width or x+101 > start_m and x + 101 < start_m+spike_width: 
        lost() 
      elif y+50 < k+spike_hight and y+160 >k: 
       if x+60 >j and x+60 < j+spike_width or x+101 > j and x + 101 <j+spike_width: 
        lost() 
      elif y+50 < s+spike_hight and y+160 >s: 
       if x+60 >f and x+60 < f+spike_width or x+101 > f and x + 101 < f+spike_width: 
        lost() 
      elif y+50 < q+spike_hight and y+160 >q: 
       if x+60 >w and x+60 < w+spike_width or x+101 > w and x + 101 < w+spike_width: 
        lost() 
      elif y+50 < t+spike_hight and y+160 >t: 
       if x+60 >r and x+60 < r+spike_width or x+101 > r and x + 101 < r+spike_width: 
        lost() 

      pygame.display.update() 
      clock.tick(60) 


    game_loop() 
    pygame.quit() 
    quit() 

謝謝您的幫助!

+0

#1不是一個調試服務。如果你想問一個問題,你必須創建一個[mcve]。我們無法閱讀200多行代碼來爲您查找錯誤。 –

回答

1

您的壓痕看起來不正確,但它看起來像你試圖建立一個函數中的全局變量。

可以看出here,您可以使用此:

globvar = 0 

def set_globvar_to_one(): 
    global globvar # Needed to modify global copy of globvar 
    globvar = 1 

def print_globvar(): 
    print(globvar)  # No need for global declaration to read value of globvar 

set_globvar_to_one() 
print_globvar()  # Prints 1 

但使用全局變量是一種不好的做法,你可能會使用類的更好。 Python是一種面向對象的語言,它非常易於使用。你可以找到文檔here