我有一段代碼,以前工作正常,但已被調整,現在不起作用。我發現這是因爲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