2014-07-16 3 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) 등의 기능에 X를 설정합니다 (200,200,3) 첫 번째 조건을 계산하면 x가 200이고> self.location [0]이 50이되므로 속도가 계산됩니다.

이것은 맞지만 두 번째 if 조건을 다시 확인하고 그러면 x는 그 조건을 만족하지 못하기 때문에 (반대편이기 때문에) 따라서 속도 0으로 재설정됩니다.

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를 0으로 재설정합니다. 희망이 곧 해결됩니다! –

+1

이것은 문제를 해결하지만 다른 문제를 일으키고, 그 중 2 개가 (a x와 y)가 필요할 때 true를 반환합니다. – Thedudxo

관련 문제