列表中敌人之间的Pygame/Python测试?
我有一个列表,包含列表中敌人之间的Pygame/Python测试?,python,artificial-intelligence,pygame,Python,Artificial Intelligence,Pygame,我有一个列表,包含敌人的类,我让他们使用for循环和敌人类中名为move()的函数跟随玩家 但是当有多个敌人时,他们在跟随玩家时会变得混乱,因此我决定将此添加到for循环中: for enmy in enemies: pygame.draw.circle(screen, (255,0,0), (enmy.x-enmy.rad,enmy.y-enmy.rad), enmy.rad, 0) for enmy2 in enemies: if enmy2 is not enmy
敌人的类
,我让他们使用for
循环和敌人类中名为move()
的函数跟随玩家
但是当有多个敌人时,他们在跟随玩家时会变得混乱,因此我决定将此添加到for
循环中:
for enmy in enemies:
pygame.draw.circle(screen, (255,0,0), (enmy.x-enmy.rad,enmy.y-enmy.rad), enmy.rad, 0)
for enmy2 in enemies:
if enmy2 is not enmy:
if not enmy.rect.colliderect(enmy2.rect):
enmy.move()
enmy.rect=pygame.Rect(enmy.x-enmy.rad*2,enmy.y-enmy.rad*2,enmy.rad*2,enmy.rad*2)
pygame.draw.rect(screen, (0,0,255), enmy.rect, 2)
因此,我希望这将测试列表中除自身之外的所有实例,但当对象单独存在时,它不会移动!(如果有两个,它们会碰撞并停止)
以下是全部代码(不需要额外的文件c:)
导入pygame
从随机导入randint
从pygame.locals导入*
pygame.init()
screen=pygame.display.set_模式((640480))
clock=pygame.time.clock()
px=35
py=35
prect=pygame.Rect(px-10,py-10,20,20)
类敌人(对象):
定义初始化(self,x,y):
self.x=x
self.y=y
self.rad=randint(5,10)
self.rect=pygame.rect(0,0,0)
def移动(自我):
如果self.x>px:
self.x-=1
如果self.y经过深思熟虑和玩弄(也玩),我设法做到了以下几点
import pygame
import random
from pygame.locals import *
pygame.init()
screen=pygame.display.set_mode((640,480))
clock=pygame.time.Clock()
px=35
py=35
prect=pygame.Rect(px-10,py-10,20,20)
class Enemy(object):
def __init__(self,x,y):
self.x=x
self.y=y
self.rad=random.randint(5,10)
self.rect=pygame.Rect(0,0,0,0)
self.x_dir = random.choice(('left','right'))
self.y_dir = random.choice(('up','down'))
def move(self, mode='chase'):
if mode=='chase':
if self.x>px:
self.x-=1
elif self.x<px:
self.x+=1
if self.y<py:
self.y+=1
elif self.y>py:
self.y-=1
else: # roam around
# Move for x direction
if self.x_dir == 'left':
if self.x > 1:
self.x -= 1
else:
self.x_dir = 'right'
self.x += 1
else:
if self.x < px - 1:
self.x += 1
else:
self.x_dir = 'left'
self.x -= 1
# Now move for y direction
if self.y_dir == 'up':
if self.y > 1:
self.y -= 1
else:
self.y_dir = 'down'
self.y += 1
else:
if self.y < py - 1:
self.y += 1
else:
self.y_dir = 'up'
self.y -= 1
enemies=[Enemy(50,60),Enemy(200,100), Enemy(200,400), Enemy(200,200), Enemy(200,400), Enemy(200,200)]
roam = {} # Dict to track relative roam/chase
roam_count = {} # Dict to track time for which roaming
max_roam = {}
max_chasing = len(enemies) // 3
cur_chasing = 0
for i, enmy in enumerate(enemies):
if cur_chasing < max_chasing:
roam[i] = 'chase'
cur_chasing += 1
else:
roam[i] = 'roam'
roam_count[i] = 0
max_roam[i] = random.randint(100, 500)
while True:
screen.fill((200,230,200))
key=pygame.key.get_pressed()
if key[K_UP]:
py-=2
if key[K_DOWN]:
py+=2
if key[K_RIGHT]:
px+=2
if key[K_LEFT]:
px-=2
for e in pygame.event.get():
if e.type==QUIT:
exit()
prect=pygame.Rect(px-20,py-20,20,20)
for e_1, enmy in enumerate(enemies):
pygame.draw.circle(screen, (255,0,0), (enmy.x-enmy.rad,enmy.y-enmy.rad), enmy.rad, 0)
moved_once = False
for e_2, enmy2 in enumerate(enemies):
if enmy2 is not enmy:
if enmy.rect.colliderect(enmy2.rect):
if roam[e_2] == roam[e_1] == 'roam':
if cur_chasing < max_chasing:
roam[e_1] = 'chase'
elif roam[e_2] == roam[e_1] == 'chase':
roam[e_2] = 'roam'
cur_chasing -= 1
if roam[e_1] == 'roam':
roam_count[e_1] += 1
enmy.move('roam')
if roam_count[e_1] > max_roam[e_1]:
roam_count[e_1] = 0
if cur_chasing < max_chasing:
roam[e_1] = 'chase'
else:
enmy.move('chase')
else:
if not moved_once:
if roam[e_1] == 'roam':
roam_count[e_1] += 1
enmy.move('roam')
if roam_count[e_1] > max_roam[e_1]:
roam_count[e_1] = 0
if cur_chasing < max_chasing:
roam[e_1] = 'chase'
else:
enmy.move('chase')
moved_once = True
enmy.rect=pygame.Rect(enmy.x-enmy.rad*2,enmy.y-enmy.rad*2,enmy.rad*2,enmy.rad*2)
pygame.draw.rect(screen, (0,0,255), enmy.rect, 2)
pygame.draw.circle(screen, (0,0,255), (px-10,py-10), 10, 0)
pygame.draw.rect(screen, (255,0,0), prect, 2)
clock.tick(80)
pygame.display.flip()
导入pygame
随机输入
从pygame.locals导入*
pygame.init()
screen=pygame.display.set_模式((640480))
clock=pygame.time.clock()
px=35
py=35
prect=pygame.Rect(px-10,py-10,20,20)
类敌人(对象):
定义初始化(self,x,y):
self.x=x
self.y=y
self.rad=random.randint(5,10)
self.rect=pygame.rect(0,0,0)
self.x_dir=random.choice(('left','right'))
self.y_dir=random.choice(('up','down'))
def移动(自,模式='chase'):
如果模式=='chase':
如果self.x>px:
self.x-=1
elif self.x 1:
self.x-=1
其他:
self.x_dir='right'
self.x+=1
其他:
如果self.x1:
self.y-=1
其他:
self.y_dir='down'
self.y+=1
其他:
如果self.ymax_漫游[e_1]:
漫游计数[e_1]=0
如果当前跟踪<最大跟踪:
漫游[e_1]=“追逐”
其他:
enmy.move(‘追逐’)
其他:
如果一次未移动\u:
如果漫游[e_1]=“漫游”:
漫游计数[e_1]+=1
enmy.move(‘漫游’)
如果漫游计数[e_1]>max_漫游[e_1]:
漫游计数[e_1]=0
如果当前跟踪<最大跟踪:
漫游[e_1]=“追逐”
其他:
enmy.move(‘追逐’)
moved_once=True
enmy.rect=pygame.rect(enmy.x-enmy.rad*2,enmy.y-enmy.rad*2,enmy.rad*2,enmy.rad*2)
pygame.draw.rect(屏幕,(0,0255),enmy.rect,2)
pygame.draw.circle(屏幕,(0,0255),(px-10,py-10),10,0)
pygame.draw.rect(屏幕,(255,0,0),prect,2)
时钟滴答(80)
pygame.display.flip()
我在不同的地方发表了评论。希望你能从中得到要点和一些想法。经过深思熟虑和玩弄(也玩),我成功地做到了以下几点
import pygame
import random
from pygame.locals import *
pygame.init()
screen=pygame.display.set_mode((640,480))
clock=pygame.time.Clock()
px=35
py=35
prect=pygame.Rect(px-10,py-10,20,20)
class Enemy(object):
def __init__(self,x,y):
self.x=x
self.y=y
self.rad=random.randint(5,10)
self.rect=pygame.Rect(0,0,0,0)
self.x_dir = random.choice(('left','right'))
self.y_dir = random.choice(('up','down'))
def move(self, mode='chase'):
if mode=='chase':
if self.x>px:
self.x-=1
elif self.x<px:
self.x+=1
if self.y<py:
self.y+=1
elif self.y>py:
self.y-=1
else: # roam around
# Move for x direction
if self.x_dir == 'left':
if self.x > 1:
self.x -= 1
else:
self.x_dir = 'right'
self.x += 1
else:
if self.x < px - 1:
self.x += 1
else:
self.x_dir = 'left'
self.x -= 1
# Now move for y direction
if self.y_dir == 'up':
if self.y > 1:
self.y -= 1
else:
self.y_dir = 'down'
self.y += 1
else:
if self.y < py - 1:
self.y += 1
else:
self.y_dir = 'up'
self.y -= 1
enemies=[Enemy(50,60),Enemy(200,100), Enemy(200,400), Enemy(200,200), Enemy(200,400), Enemy(200,200)]
roam = {} # Dict to track relative roam/chase
roam_count = {} # Dict to track time for which roaming
max_roam = {}
max_chasing = len(enemies) // 3
cur_chasing = 0
for i, enmy in enumerate(enemies):
if cur_chasing < max_chasing:
roam[i] = 'chase'
cur_chasing += 1
else:
roam[i] = 'roam'
roam_count[i] = 0
max_roam[i] = random.randint(100, 500)
while True:
screen.fill((200,230,200))
key=pygame.key.get_pressed()
if key[K_UP]:
py-=2
if key[K_DOWN]:
py+=2
if key[K_RIGHT]:
px+=2
if key[K_LEFT]:
px-=2
for e in pygame.event.get():
if e.type==QUIT:
exit()
prect=pygame.Rect(px-20,py-20,20,20)
for e_1, enmy in enumerate(enemies):
pygame.draw.circle(screen, (255,0,0), (enmy.x-enmy.rad,enmy.y-enmy.rad), enmy.rad, 0)
moved_once = False
for e_2, enmy2 in enumerate(enemies):
if enmy2 is not enmy:
if enmy.rect.colliderect(enmy2.rect):
if roam[e_2] == roam[e_1] == 'roam':
if cur_chasing < max_chasing:
roam[e_1] = 'chase'
elif roam[e_2] == roam[e_1] == 'chase':
roam[e_2] = 'roam'
cur_chasing -= 1
if roam[e_1] == 'roam':
roam_count[e_1] += 1
enmy.move('roam')
if roam_count[e_1] > max_roam[e_1]:
roam_count[e_1] = 0
if cur_chasing < max_chasing:
roam[e_1] = 'chase'
else:
enmy.move('chase')
else:
if not moved_once:
if roam[e_1] == 'roam':
roam_count[e_1] += 1
enmy.move('roam')
if roam_count[e_1] > max_roam[e_1]:
roam_count[e_1] = 0
if cur_chasing < max_chasing:
roam[e_1] = 'chase'
else:
enmy.move('chase')
moved_once = True
enmy.rect=pygame.Rect(enmy.x-enmy.rad*2,enmy.y-enmy.rad*2,enmy.rad*2,enmy.rad*2)
pygame.draw.rect(screen, (0,0,255), enmy.rect, 2)
pygame.draw.circle(screen, (0,0,255), (px-10,py-10), 10, 0)
pygame.draw.rect(screen, (255,0,0), prect, 2)
clock.tick(80)
pygame.display.flip()
导入pygame
随机输入
从pygame.locals导入*
pygame.init()
screen=pygame.display.set_模式((640480))
clock=pygame.time.clock()
px=35
py=35
prect=pygame.Rect(px-10,py-10,20,20)
类敌人(对象):
定义初始化(self,x,y):
self.x=x
self.y=y
self.rad=random.randint(5,10)
self.rect=pygame.rect(0,0,0)
self.x_dir=random.choice(('left','right'))
self.y_dir=random.choice(('up','down'))
def移动(自,模式='chase'):
如果模式=='chase':
如果self.x>px:
self.x-=1
elif self.x 1:
all_queue = pygame.sprite.Group()
good_queue = pygame.sprite.Group()
bad_queue = pygame.sprite.Group()
…##fancy and engaging code above
all_queue.update() ##will call every update() for all sprites within, if you're using sprite.update()
dict_of_hits = pygame.sprite.collidegroup(good_queue, bad_queue, False, False)
all_queue.update()
for good_sprite in good_queue:
hit_list = pygame.sprite.spritecollide(good_sprite, bad_queue, False)
for sprite in some_queue:
hit_list = pygame.sprite.spritecollide(sprite, some_other_queue, False)
if hit_list:
##start killing guys, assigning points, changing momentum, etc