Pygame 敌人不会向下移动 - 塔防游戏 [英] Pygame Enemys Wont Move Down - Tower Defense Game

查看:58
本文介绍了Pygame 敌人不会向下移动 - 塔防游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,对于我的塔防游戏中敌人的最后 2 个检查点,敌人不会在第二次最后一次向下移动,直到他们损坏塔

解决方案

我建议沿点列表移动蝎子 (spider_move).使用一个变量来说明下一个点的索引 (spider_move_to) 并将蝎子移动到这个点 (next_p).每次到达一个点时,就增加索引(spider_move_to += 1).如果列表中没有更多点,则停止移动.要移动蝎子,请计算 spider 的当前位置与列表中的点 (next_p).根据 spider.speed 增加或减少蝎子 (spider.x, spider.y) 的位置,取决于 dxdy:

spider_move_to = 0蜘蛛移动= [(240, 330), (240, 550), (540, 550), (540, 160),(770, 160), (770, 550), (870, 550)]跑步游戏 = 真运行游戏时:# [...]如果spider_move_to <镜头(蜘蛛移动):next_p = spider_move[spider_move_to]dx, dy = (next_p[0] - spider.x, next_p[1] - spider.y)如果 dx >蜘蛛速度:蜘蛛.x += 蜘蛛.speedelif dx <-spider.speed:蜘蛛.x -= 蜘蛛.速度别的:蜘蛛.x = next_p[0]如果 dy >蜘蛛速度:蜘蛛.y += 蜘蛛.速度elif dy <-spider.speed:蜘蛛.y -= 蜘蛛.速度别的:Spider.y = next_p[1]如果 Spider.x == next_p[0] 和 spider.y == next_p[1]:Spider_move_to += 1# [...]


如果您有多个蝎子,那么我建议将 spider_move_to 设为 spider

类的属性

类蜘蛛:def __init__(self,x,y,height,width,color):# [...]self.spider_move_to

使用属性代替全局变量:

spider_move = [(240, 330), (240, 550), (540, 550), (540, 160),(770, 160), (770, 550), (870, 550)]跑步游戏 = 真运行游戏时:# [...]对于蜘蛛中的蜘蛛:如果spider.spider_move_to <镜头(蜘蛛移动):next_p = spider_move[spider.spider_move_to]dx, dy = (next_p[0] - spider.x, next_p[1] - spider.y)# [...]如果 Spider.x == next_p[0] 和 spider.y == next_p[1]:蜘蛛 .spider_move_to += 1# [...]

my problem here is that for my last 2 check points for my enemy for my tower defense game the enemys wont move down for the second last go untile they damage the tower video they just move forward idk why they just move out from there
its the same for the last one to




    # 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


my full code


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,y,height,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, self.y, 46,60)
        pygame.draw.rect(window, (255,0,0), (self.hitbox[0], self.hitbox[1] - 40, 40, 13)) # NEW
        pygame.draw.rect(window, (231, 76, 60), (self.hitbox[0], self.hitbox[1] - 40, 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,50,(23, 32, 42))
            spiders.append(new_platforms)


class tower:
    def __init__(self,x,y,height,width,color):
        self.x = x
        self.y =y
        self.height = height
        self.width = width
        self.color = color
        self.rect = pygame.Rect(x,y,height,width)
        self.health = 10
        self.hitbox = (self.x + -18, self.y, 46,60)
        
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        self.hitbox = (self.x + -18, self.y, 46,60)
        pygame.draw.rect(window, (255,0,0), (self.hitbox[0], self.hitbox[1] - 40, 140, 25)) # NEW
        pygame.draw.rect(window, (46, 204, 113), (self.hitbox[0], self.hitbox[1] - 40, 140 - (5 * (10 - self.health)), 25))


tower1 = tower(875,450,50,50,red)


# check points for the player to turn
class check():
    def __init__(self,x,y,height,width,color):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.color = color
        self.rect = pygame.Rect(x,y,height,width)
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(window,self.color,self.rect)

check1 = check(250,366,50,50,red)
check2 = check(250,566,50,50,red)
check3 = check(540,566,50,50,red)
check4 = check(780,166,50,50,red)
check5 = check(780,550,50,50,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,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()



解决方案

I recommend to move the scorpion along a list of points (spider_move). Use a variable that states the index of the next point (spider_move_to) and move the scorpion to this point (next_p). Every time when a point is reached, then increment the index (spider_move_to += 1). If no more point is in the list, then stop the movement. To move the scorpion, compute the difference ( dx, dy) between the current position of spider and the point from the list (next_p). Increment or decrement to position of the scorpion (spider.x, spider.y) by spider.speed, dependent on the amount of dx and dy:

spider_move_to = 0
spider_move = [
   (240, 330), (240, 550), (540, 550), (540, 160),
   (770, 160), (770, 550), (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

   # [...]


If you have multiple scorpions, then I recommend to make spider_move_to an attribute of the class spider

class spider:
    def __init__(self,x,y,height,width,color):
        # [...] 

        self.spider_move_to

Use the attribute instead of the global variable:

spider_move = [
   (240, 330), (240, 550), (540, 550), (540, 160),
   (770, 160), (770, 550), (870, 550)]

runninggame = True
while runninggame:
   # [...]

   for spider in spiders:
       if spider.spider_move_to < len(spider_move):
           next_p = spider_move[spider.spider_move_to] 
           dx, dy = (next_p[0] - spider.x, next_p[1] - spider.y)

           # [...]

           if spider.x == next_p[0] and spider.y == next_p[1]:
               spider .spider_move_to += 1

   # [...]  

这篇关于Pygame 敌人不会向下移动 - 塔防游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆