如何解决Pygame敌人不会向下移动-塔防游戏
我的问题是,在我的塔防游戏中,我的敌人的最后2个检查点,敌人不会倒数第二次走下去,直到他们损坏了塔video,他们才向前前进,为什么他们只是从那里出来
最后一个是一样的
# move the enemy
for spider in spiders:
if spider.x < 230:
spider.x += spider.speed
elif spider.y < 160:
spider.x += spider.speed
elif spider.x > 540:
spider.y -= spider.speed
elif spider.y < 566:
spider.y += spider.speed
elif spider.y > 290:
spider.x += spider.speed
elif spider.y > 566:
spider.x += spider.speed
# THIS part isnt working IDK why if the enemy is close to the 4th check point it should go down but it wont??
elif spider.x < 628:
spider.y += spider.speed
# idk if this part will work because the last second part didnt work
elif spider.y < 550:
spider.x += spider.speed
我的完整代码
import pygame,random
pygame.init()
# window
window = pygame.display.set_mode((1000,700),pygame.NOFRAME)
red = (0,255,0)
#
class spider:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.color = color
self.speed = 13
self.rect = pygame.Rect(x,width)
self.health = 10
self.hitBox = (self.x + 20,self.y,26,60)
# image frame
self.image = [pygame.image.load("weak (1).png"),pygame.image.load("weak (2).png"),pygame.image.load("weak (3).png"),pygame.image.load("weak (4).png"),pygame.image.load("weak (5).png"),pygame.image.load("weak (6).png"),pygame.image.load("weak (7).png"),pygame.image.load("weak (8).png"),pygame.image.load("weak (9).png"),pygame.image.load("weak (10).png"),pygame.image.load("weak (11).png"),pygame.image.load("weak (12).png"),pygame.image.load("weak (13).png"),pygame.image.load("weak (14).png"),pygame.image.load("weak (15).png"),pygame.image.load("weak (16).png"),pygame.image.load("weak (17).png"),pygame.image.load("weak (18).png"),pygame.image.load("weak (19).png")]
self.image = [pygame.transform.scale(image,(image.get_width()//2,image.get_height()//2)) for image in self.image]
self.anim_index = 0
self.walkrights = 0
self.fps = 40
self.clock = pygame.time.Clock()
self.direction = "up"
self.direction = "right"
def draw(self):
self.rect.topleft = (self.x,self.y)
self.clock.tick(self.fps)
image_list = self.image
if self.anim_index >= len(image_list):
self.anim_index = 0
player_image = image_list[self.anim_index]
self.anim_index += 1
player_rect = player_image.get_rect(center = self.rect.center)
player_rect.centerx += 3 # 10 is just an example
player_rect.centery += -10# 15 is just an example
window.blit(player_image,player_rect)
self.hitBox = (self.x + -18,46,60)
pygame.draw.rect(window,(255,0),(self.hitBox[0],self.hitBox[1] - 40,40,13)) # NEW
pygame.draw.rect(window,(231,76,60),80 - (5 * (10 - self.health)),13))
spiders = []
platformGroup = pygame.sprite.Group
level = [
" "," "," "," "," "," c c c c c c c c c "," "," "," "," "," "]
for iy,row in enumerate(level):
for ix,col in enumerate(row):
if col == "c":
new_platforms = spider(ix*-24,iy*46,50,(23,32,42))
spiders.append(new_platforms)
class tower:
def __init__(self,color):
self.x = x
self.y =y
self.height = height
self.width = width
self.color = color
self.rect = pygame.Rect(x,width)
self.health = 10
self.hitBox = (self.x + -18,60)
def draw(self):
self.rect.topleft = (self.x,self.y)
self.hitBox = (self.x + -18,140,25)) # NEW
pygame.draw.rect(window,(46,204,113),140 - (5 * (10 - self.health)),25))
tower1 = tower(875,450,red)
# check points for the player to turn
class check():
def __init__(self,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.color = color
self.rect = pygame.Rect(x,width)
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window,self.color,self.rect)
check1 = check(250,366,red)
check2 = check(250,566,red)
check3 = check(540,red)
check4 = check(780,166,red)
check5 = check(780,550,red)
checks = [check1,check2,check3,check4,check5]
# the background for my gameee
bg = pygame.image.load("bg2.png")
# redraw window
def draw():
window.fill((0,0))
window.blit(bg,(0,0))
for spider in spiders:
spider.draw()
for check in checks:
check.draw()
tower1.draw()
clock = pygame.time.Clock()
fps = 60
# the main loop
runninggame = True
while runninggame:
clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
runninggame = False
# move the enemy
for spider in spiders:
if spider.x < 230:
spider.x += spider.speed
elif spider.y < 160:
spider.x += spider.speed
elif spider.x > 540:
spider.y -= spider.speed
elif spider.y < 566:
spider.y += spider.speed
elif spider.y > 290:
spider.x += spider.speed
elif spider.y > 566:
spider.x += spider.speed
# THIS part isnt working IDK why if the enemy is close to the 4th check point it should go down but it wont??
elif spider.x < 628:
spider.y += spider.speed
# idk if this part will work because the last second part didnt work
elif spider.y < 550:
spider.x += spider.speed
# redraw the window
draw()
pygame.display.update()
pygame.quit()
解决方法
一位YouTuber Tech With Tim在12小时livestream中用pygame创建了一个塔防游戏。您将在此github-link上找到他的代码,并且可以找出错误的出处。
注意:此github代码中不包含资产,该资产不包含资产(图像和声音),因此您可以创建资产文件夹并在游戏脚本中引用图像和声音。
,在查看您的代码时,我注意到了这种奇怪情况
elif spider.x > 540:
spider.y -= spider.speed
...
elif spider.x < 628:
spider.y += spider.speed
这是基于您提供的订单。第二个elif永远不会运行,因为蜘蛛永远不会运行-x表示我们将永远无法获得 540始终为真,这也意味着由于Elif始终运行第一个真实条件,因此我们无法实际获取将导致条件 540之前运行的值。我建议修改代码,以通过将self.checkpoint添加为每个蜘蛛的属性来消除这种情况的可能性。然后,当到达检查点时,请检查是否存在
elif spider.x > 540 and (spider.checkpoint = 3 or spider.checkpoint = 4):
if spider.checkpoint != 4:
spider.checkpoint = 4
spider.y -= spider.speed
对于每个检查点,它将在第一次运行时将其递增到下一个检查点,并保持在那里,以便在到达下一个检查点时,只需将每个数字加1,以便蜘蛛程序始终知道其要行驶到的检查点。 / p>
,我建议将蝎子沿点列表(setState()
)移动。使用声明下一个点(spider_move
)索引的变量,并将蝎子移动到该点(spider_move_to
)。每次到达一点时,然后增加索引(next_p
)。如果列表中没有其他点,则停止运动。要移动蝎子,请计算spider_move_to += 1
当前位置和列表中的点(dx
)之间的差(dy
,spider
)。根据{{1}}和next_p
的数量,将蝎子(spider.x
,spider.y
)的位置增加或减少spider.speed
:
dx
如果您有多个蝎子,那么我建议将dy
设为类spider_move_to = 0
spider_move = [
(240,330),(240,550),(540,160),(770,(870,550)]
runninggame = True
while runninggame:
# [...]
if spider_move_to < len(spider_move):
next_p = spider_move[spider_move_to]
dx,dy = (next_p[0] - spider.x,next_p[1] - spider.y)
if dx > spider.speed:
spider.x += spider.speed
elif dx < -spider.speed:
spider.x -= spider.speed
else:
spider.x = next_p[0]
if dy > spider.speed:
spider.y += spider.speed
elif dy < -spider.speed:
spider.y -= spider.speed
else:
spider.y = next_p[1]
if spider.x == next_p[0] and spider.y == next_p[1]:
spider_move_to += 1
# [...]
的属性
spider_move_to
使用属性代替全局变量:
spider
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。