2014-07-16 71 views
-4

我有一段代碼,以前工作正常,但已被調整,現在不起作用。我發現這是因爲block.speed_x在遊戲循環開始時被設置爲0,但我不知道爲什麼。這個變量如何被設置爲0?

prints = [] 
#imports and inits 
import pygame 
pygame.init() 
# Global constants ============================================================= 
BLACK = ( 0, 0, 0) 
WHITE = (255, 255, 255) 
GREEN = ( 0, 255, 0) 
RED = (255, 0, 0) 
BLUE = ( 0, 0, 255) 
#Functions====================================================================== 
def text(text,xy): 
    def draw_letter(letter,xy): 
     if letter ==' ': 
      l=pygame.image.load(('letters/space.png')).convert() 
     elif letter == '?': 
      l=pygame.image.load(('letters/QM.png')).convert() 
     elif letter == '.': 
      l=pygame.image.load(('letters/dot.png')).convert() 
     else: 
      l=pygame.image.load(('letters/'+letter+'.png')).convert() 
     l.set_colorkey(WHITE) 
     screen.blit(l,xy) 
    def word(text,xy): 
     loc=0 
     for letter in text: 
      draw_letter(letter,[(xy[0]+loc),xy[1]]) 
      loc+=15 
    word(text,xy) 

class shape: #basic shape 
    color=255, 255, 255 
    location=[0,0] 
    speed_x = 0 
    speed_y = 0 

    def update_pos(self): 
     self.location[0]+=self.speed_x 
     self.location[1]+=self.speed_y 

    def move(self,x,y,speed): 
     if speed==0: 
      self.location=[x,y] 
     else: 
      speed_x = speed 
      speed_y = speed 
      distance_y = y - self.location[1] 
      distance_x = x - self.location[0] 
      distance_y2 = self.location[1] - y 
      distance_x2 = self.location[0] - x 

      if x > self.location[0] and not self.location[0]>=x: 
       ratio = distance_x/distance_y 
       speed_x = ratio * speed 
       self.speed_x=speed_x 
       prints.append(block.speed_x) #temp print 
      else: self.speed_x=0 

      if y > self.location[1] and not self.location[1]>=y: 
       ratio = distance_y/distance_x 
       speed_y = ratio * speed 
       self.speed_y=speed_y 
      else: self.speed_y=0 

      if x < self.location[0] and not self.location[0]<=x: 
       print('X') 
       ratio = distance_x/distance_y 
       speed_x = ratio * speed 
       self.speed_x=speed_x*-1 
       prints.append(block.speed_x) #temp print 
      else: self.speed_x=0; 

      if y < self.location[1] and not self.location[1]<=y: 
       ratio = distance_y/distance_x 
       speed_y = ratio * speed 
       self.speed_y=speed_y *-1 
      else: self.speed_y=0 

class rectangle(shape): 
    size=[0,0] 

    def draw(self): 
     pygame.draw.rect(screen,self.color,[self.location,self.size]) 
     self.update_pos() 
#=============VARIABLES========================================================= 
block=rectangle() 
block.color=RED 
block.location=[50,50] 
block.size=[50,50] 
#=============================================================================== 
size = (700, 500) 
screen = pygame.display.set_mode(size) #set screen size and create screen 
pygame.display.set_caption("Dud's game") #name of screen 
done = False 
clock = pygame.time.Clock() 
prints.append(block.speed_x) #temp print 
# -------- Main Program Loop ----------- 
while not done: 
    # EVENTS =================================================================== 
    prints.append(block.speed_x) #temp print. <------ seems to get set to 0 here 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      done = True 
    # LOGIC ==================================================================== 
    mouse_pos = pygame.mouse.get_pos() 
    block.move(200,200,3) 
    # DRAW ===================================================================== 
    screen.fill(WHITE) 

    block.draw() 
    text('hello',[10,10]) 

    pygame.display.flip() 
    # END ====================================================================== 
    clock.tick(30) 
pygame.quit() 
print(prints) 

就我所知,block.speed_x在循環開始時被設置爲0。我只告訴它= 0的唯一時間是block=rectangle(),默認情況下類將速度設置爲0,而在移動時,如果矩形已經到達了應該在其處的擋塊,則停止:else: self.speed_x=0;

臨時打印的結果:

[0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0, 3.0, 0] 
+0

在'move'方法中,你有'self.speed_x = 0'。 – BrenBarn

+0

你也將它設置爲0:'else:self.speed_x = 0' – SiHa

+0

是的,忘記了這一點。這應該是在那裏,並沒有造成問題。 – Thedudxo

回答

0

我想問題可能是在這裏:

block.move(200,200,3)將設置在功能上爲200 X,則你評估第一個條件,並計算速度,因爲x是200並且是> self.location [0],其值爲50.

這是正確的,但是在第二個條件下再檢查第二條件那麼x將不會滿足該條件(因爲它相反),因此速度將被重置爲零。

if x > self.location[0] and not self.location[0]>=x: 
     ratio = distance_x/distance_y 
     speed_x = ratio * speed 
     self.speed_x=speed_x 
     prints.append(block.speed_x) #temp print 
    else: self.speed_x=0 

    if y > self.location[1] and not self.location[1]>=y: 
     ratio = distance_y/distance_x 
     speed_y = ratio * speed 
     self.speed_y=speed_y 
    else: self.speed_y=0 

    if x < self.location[0] and not self.location[0]<=x: 
     print('X') 
     ratio = distance_x/distance_y 
     speed_x = ratio * speed 
     self.speed_x=speed_x*-1 
     prints.append(block.speed_x) #temp print 
    else: self.speed_x=0; 

    if y < self.location[1] and not self.location[1]<=y: 
     ratio = distance_y/distance_x 
     speed_y = ratio * speed 
     self.speed_y=speed_y *-1 
    else: self.speed_y=0 

我沒有得到一個雙重檢查的原因在if語句的每一個(反正我是不碰的),但我想你應該嘗試一個如果 - elif的 - else語句,請嘗試以下操作:

if x > self.location[0] and not self.location[0]>=x: 
    ratio = distance_x/distance_y 
    speed_x = ratio * speed 
    self.speed_x=speed_x 
    prints.append(block.speed_x) #temp print 

elif x < self.location[0] and not self.location[0]<=x: 
    print('X') 
    ratio = distance_x/distance_y 
    speed_x = ratio * speed 
    self.speed_x=speed_x*-1 
    prints.append(block.speed_x) #temp print 

else: self.speed_x=0 


if y > self.location[1] and not self.location[1]>=y: 
    ratio = distance_y/distance_x 
    speed_y = ratio * speed 
    self.speed_y=speed_y 

elif y < self.location[1] and not self.location[1]<=y: 
    ratio = distance_y/distance_x 
    speed_y = ratio * speed 
    self.speed_y=speed_y *-1 

else: self.speed_y=0 

希望它有幫助!

+0

我不認爲這有什麼區別。問題是block.speed_x(或self.speed_x)被設置爲0。 – Thedudxo

+0

我的建議是基於這樣一個事實,即對於x有兩個條件,對於y有兩個條件。程序只會觸發這兩種情況之一的代碼,因此其中一種情況總是會將self.speed重置爲零。希望它很快解決! –

+1

這解決了問題,但引發了另一個,只有其中一個if語句將返回true時,其中2個需要(一個x和一個y) – Thedudxo