我有一段代码,以前工作正常但已经过调整,现在还不起作用。我发现这是因为在游戏循环开始时block.speed_x被设置为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 :(得分:0)
我认为问题可能在这里:
block.move(200,200,3)将函数上的x设置为200.然后,您评估第一个条件并计算速度,因为x是200并且是> self.location [0],其值为50.
这是正确的,但是下面的几行再次检查第二个if条件然后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语句进行双重检查(无论如何我都没有接触过那些),但我认为你应该尝试使用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
希望它有所帮助!