【发布时间】:2020-06-09 02:46:43
【问题描述】:
我有一个敌人会发射弹丸,但只向右侧射击我希望它向任何位置的玩家射击带我把如何做的步骤告诉我,那太好了,谢谢
这就是我所做的,它只向右侧射击我希望它在玩家所在的任何位置向玩家射击
for shootss in shootsright:
if shootss.x < 500 and shootss.x > 0:
shootss.x += 7
else:
shootsright.pop(shootsright.index(shootss))
if len(shootsright) < 1:
shootsright.append(Bools(round(enemyshoots1.x+enemyshoots1.width-107),round(enemyshoots1.y + enemyshoots1.height-50),(0,0,0)))
这是我的子弹课
# enemys bullets
ksud = pygame.image.load("heart.png")
class Bools(object):
def __init__(self, x, y,color):
self.x = x
self.y = y
self.ksud = pygame.image.load("heart.png")
self.hitbox = self.ksud.get_rect()
self.rect = self.ksud.get_rect()
self.rect.topleft = (self.x,self.y)
self.speed = 10
self.color = color
self.hitbox = (self.x + 57, self.y + 33, 29, 52) # NEW
def draw(self, window):
self.rect.topleft = (self.x,self.y)
player_rect = self.ksud.get_rect(center = self.rect.center)
player_rect.centerx += 0 # 10 is just an example
player_rect.centery += 0 # 15 is just an example
window.blit(self.ksud, player_rect)
self.hitbox = (self.x + 97, self.y + 33, 10, 10) # NEW
window.blit(self.ksud,self.rect)
我的敌人班
shotsright = pygame.image.load("shooting2.png")
shotsleft = pygame.image.load("shooting1.png")
class enemyshoot:
def __init__(self,x,y,height,width,color):
self.x = x
self.y =y
self.height = height
self.width = width
self.color = color
self.shootsright = pygame.image.load("shooting2.png")
self.shotsleft = pygame.image.load("shooting1.png")
self.shootsright = pygame.transform.scale(self.shootsright,(self.shootsright.get_width()//3,self.shootsright.get_height()//3))
self.shotsleft = pygame.transform.scale(self.shotsleft,(self.shotsleft.get_width()//3,self.shotsleft.get_height()//3))
self.rect = pygame.Rect(x,y,height,width)
self.health = 10
self.hitbox = (self.x + -20, self.y + 30, 31, 57)
def draw(self):
self.rect.topleft = (self.x,self.y)
window.blit(self.shootsright,self.rect)
self.hits = (self.x + 20, self.y, 28,60)
pygame.draw.rect(window, (255,0,0), (self.hitbox[0], self.hitbox[1] - 60, 50, 10)) # NEW
pygame.draw.rect(window, (0,255,0), (self.hitbox[0], self.hitbox[1] - 60, 50 - (5 * (10 - self.health)), 10))
self.hitbox = (self.x + 60, self.y + 80, 81, 87)
black = (0,0,0)
enemyshoots1 = enemyshoot(1100,240,100,100,black)
enemyshooting = [enemyshoots1]
这是我的完整代码
script
【问题讨论】:
-
你有很多行代码,你应该试着清理一下,例如,你可以将每个精灵/对象放在一个列表中,所以当你想要滚动时,你只需要循环遍历一个列表而不是所有这些循环,对于绘图也是如此。您也可以尝试创建一个大师班,然后在该班的基础上构建其他课程,您也有很多空白行,您可能会减少很多行