잘 작동했지만 익숙하지 않은 코드 섹션이 있는데 지금은 작동하지 않습니다. 블록 루프가 시작될 때 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]
'move' 메소드 안에'self.speed_x = 0'가 있습니다. – BrenBarn
여기서도 0으로 설정합니다. 'else : self.speed_x = 0' – SiHa
네, 잊어 버렸습니다. 그곳에 있어야하고 문제를 일으키지 않습니다. – Thedudxo