我如何让敌人在这个程序中移动?类中的对象!Python Pygame
因此,我正试图找出如何使敌人移动,因为这是我最不需要做的事情!我已经在这里呆了一个小时了,我该如何移动敌人!!!我尝试过更新坐标,但结果不是很好,所以有人能给我正确的代码/修复它吗?如果没有,至少有人能告诉我下一步该怎么办我如何让敌人在这个程序中移动?类中的对象!Python Pygame,python,class,animation,graphics,pygame,Python,Class,Animation,Graphics,Pygame,因此,我正试图找出如何使敌人移动,因为这是我最不需要做的事情!我已经在这里呆了一个小时了,我该如何移动敌人!!!我尝试过更新坐标,但结果不是很好,所以有人能给我正确的代码/修复它吗?如果没有,至少有人能告诉我下一步该怎么办 class Enemy(object): def __init__(self,pos): enemies.append(self) self.rect = pygame.Rect(x, y, 10, 10) def moveE
class Enemy(object):
def __init__(self,pos):
enemies.append(self)
self.rect = pygame.Rect(x, y, 10, 10)
def moveEnemy(self, dx, dy):
# Move each axis separately. Note that this checks for collisions both times.
if dx != 0:
self.XandY(dx, 0)
if dy != 0:
self.XandY(0, dy)
def XandYEnemy(self, dx, dy):
# Move the rect
self.rect.x += dx
self.rect.y += dy
for wall in walls:
if self.rect.colliderect(wall.rect):
if dx > 0: # Moving right; Hit the left side of the wall
self.rect.right = wall.rect.left
if dx < 0: # Moving left; Hit the right side of the wall
self.rect.left = wall.rect.right
if dy > 0: # Moving down; Hit the top side of the wall
self.rect.bottom = wall.rect.top
if dy < 0: # Moving up; Hit the bottom side of the wall
self.rect.top = wall.rect.bottom
class Wall(object):
def __init__(self, pos):
walls.append(self)
self.rect = pygame.Rect(pos[0], pos[1], 16, 16)
#Variables
walls = []
enemies = []
player = Player()
pygame.init()
# Set the width and height of the screen [width, height]
size = (700, 285)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Karl's Game")
#Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
#Loading pictures
level = [
"WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
"W WW WW W W",
"W WW WW WW W W W",
"W WW WW WWWWWWWW WWW WWWW WWWW WWW W",
"W WW WW WWWWWWWW WWW WWWW WWWW WWW W",
"W WW WW WWWWWWWW WWW WWWW WWWW WWW W",
"W WW WW WWWWWWWW WWW WWWW WWWW WWW W",
"W WW WW WWWWWWWW WWW WWWW WWWW WWW W",
"W B WW WWWW WWW WWWW WWWW WWW W",
"W WW WWWW WWW WWWW WWWW WWW W",
"W WW WW WWWWWWWW WWW WWWW WWWW WWW W",
"W WW WW WWWWWWWW WWW WWWW WWWW WWW W",
"W WW WW WWWWWWWW WWW WWWW WWWW WWW W",
"W WW WW WWWWWWWW WWW WWWW WWWW WWW W",
"W WW WW WWWWWWWW WWW WWWW WWWW WWW W",
"W WW WW WWW W WWW W",
"W WW WW WWW W WWWE W",
"WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
]
# Turns the string above into the maze and W = Wall E = Exit
x = y = 0
for row in level:
for col in row:
if col == "W":
Wall((x, y))
if col == "B":
Enemy((x,y))
if col == "E":
end_rect = pygame.Rect(x, y, 32, 16)
x += 16
y += 16
x = 0
# -------- Main Program Loop -----------
while not done:
# --- Limit to 60 frames per second
clock.tick(60)
# --- Main event loop
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done = True # Flag that we are done so we exit this loop
key = pygame.key.get_pressed()
if key[pygame.K_LEFT]:
player.move(-2, 0)
if key[pygame.K_RIGHT]:
player.move(2, 0)
if key[pygame.K_UP]:
player.move(0, -2)
if key[pygame.K_DOWN]:
player.move(0, 2)
if player.rect.colliderect(end_rect):
go = 2
# --- Drawing code should go here
if go == 1:
screen.blit(back, [0, 0])
for wall in walls:
pygame.draw.rect(screen, NEON, wall.rect)
pygame.draw.rect(screen, (255, 0, 0), end_rect)
pygame.draw.rect(screen, (0, 200, 0), player.rect)
for enemy in enemies:
pygame.draw.rect(screen, PURPLE, enemy.rect)
# --- Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# Close the window and quit.
# If you forget this line, the program will 'hang'
# on exit if running from IDLE.
pygame.quit()
类敌人(对象):
定义初始(自我,位置):
敌人。附加(自我)
self.rect=pygame.rect(x,y,10,10)
def移动敌人(自身、dx、dy):
#分别移动每个轴。请注意,这会检查两次碰撞。
如果dx!=0:
self.XandY(dx,0)
如果是dy!=0:
self.XandY(0,dy)
def XandYEnemy(self、dx、dy):
#移动矩形
self.rect.x+=dx
自校正y+=dy
对于墙中墙:
如果self.rect.collide rect(wall.rect):
如果dx>0:#向右移动;击中墙的左侧
self.rect.right=wall.rect.left
如果dx<0:#向左移动;击中墙壁的右侧
self.rect.left=wall.rect.right
如果dy>0:#向下移动;击中墙的顶部
self.rect.bottom=wall.rect.top
如果dy<0:#向上移动;击中墙的底部
self.rect.top=wall.rect.bottom
类墙(对象):
定义初始(自我,位置):
墙。附加(自身)
self.rect=pygame.rect(位置[0],位置[1],16,16)
#变数
墙=[]
敌人=[]
player=player()
pygame.init()
#设置屏幕的宽度和高度[宽度,高度]
大小=(700285)
screen=pygame.display.set_模式(大小)
pygame.display.set_标题(“卡尔的游戏”)
#循环,直到用户单击关闭按钮。
完成=错误
#用于管理屏幕更新的速度
clock=pygame.time.clock()
#加载图片
级别=[
“WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW,
“W W W W W W W W”,
“wwwwwwwww”,
“W WW WW WW WW WW WW WW WW WW WW WW WW W”,
“W WW WW WW WW WW WW WW WW WW WW WW WW W”,
“W WW WW WW WW WW WW WW WW WW WW WW WW W”,
“W WW WW WW WW WW WW WW WW WW WW WW WW W”,
“W WW WW WW WW WW WW WW WW WW WW WW WW W”,
“W B WW WW WW WW WW WW WW WW WW W”,
“W WW WW WW WW WW WW WW WW WW W”,
“W WW WW WW WW WW WW WW WW WW WW WW WW W”,
“W WW WW WW WW WW WW WW WW WW WW WW WW W”,
“W WW WW WW WW WW WW WW WW WW WW WW WW W”,
“W WW WW WW WW WW WW WW WW WW WW WW WW W”,
“W WW WW WW WW WW WW WW WW WW WW WW WW W”,
“W WW WW WW WW WW W”,
“WW WW WW WW WW WW WE W”,
“WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW,
]
#将上面的字符串变成迷宫,W=墙E=出口
x=y=0
对于级别中的行:
对于行中的列:
如果列==“W”:
墙(x,y))
如果列==“B”:
敌人((x,y))
如果列==“E”:
end_rect=pygame.rect(x,y,32,16)
x+=16
y+=16
x=0
#-----主程序循环-----------
虽然没有这样做:
#---限制为每秒60帧
时钟滴答(60)
#---主事件循环
对于pygame.event.get()中的事件:#用户执行了某些操作
if event.type==pygame.QUIT:#如果用户单击关闭
done=True#标记我们已完成,因此我们退出此循环
key=pygame.key.get_pressed()
如果键[pygame.K_左]:
玩家移动(-2,0)
如果键[pygame.K_RIGHT]:
玩家移动(2,0)
如果键[pygame.K_UP]:
玩家移动(0,-2)
如果键[pygame.K_DOWN]:
玩家移动(0,2)
如果player.rect.collide rect(end_rect):
go=2
#---绘图代码应该在这里
如果go==1:
screen.blit(返回[0,0])
对于墙中墙:
pygame.draw.rect(屏幕、霓虹灯、墙.rect)
pygame.draw.rect(屏幕,(255,0,0),end_rect)
pygame.draw.rect(屏幕,(0,200,0),player.rect)
对于敌人中的敌人:
pygame.draw.rect(屏幕,紫色,敌人.rect)
#---继续,用我们绘制的内容更新屏幕。
pygame.display.flip()
#关闭窗口并退出。
#如果您忘记这一行,程序将“挂起”
#如果从怠速运行,则在退出时。
pygame.quit()
为了获得更好的帮助,请尝试将此范围缩小到导致您出现问题的部分,并提供一个非常精确的错误描述。在这里,我已尽可能缩小范围:)我没有看到从任何地方调用的移动敌人
。。。另外,在敌方的\uuuu init\uuuu
中,从未使用过pos
。我删除了它,因为每当我调用它时,它表示敌方尚未初始化或创建,当我尝试创建它时,它表示需要2个参数,但我不知道另一个参数是什么。。。此外,初始化在级别字符串下的for循环中使用。您需要修复敌人
的初始化
,然后需要从某处调用移动敌人
。还有很多其他的小改动可以提高代码的质量,比如不将元素添加到它们的\uuuu init\uuuu
中的gloabl列表中。在继续之前解决这些主要问题。这是一个很好的习惯,在开始做其他事情之前先修好bug。