Python 更改ememy';是什么颜色表示它正在损坏?
我正在编写一个代码,在它受到伤害时,我需要帮助改变敌人的体色#就像我的地雷船。我需要添加不同的图像来实现这一点,还是可以通过编码来实现??我甚至想让地精(敌人)显示打玩家的动画,我有这些图像,我只想让敌人显示敌人ehen,玩家离他2或3像素,在玩家距离地精5或6像素半径后靠近他!救命啊!我知道这有点长,但仍然! 这是我的敌人阶级:Python 更改ememy';是什么颜色表示它正在损坏?,python,pygame,Python,Pygame,我正在编写一个代码,在它受到伤害时,我需要帮助改变敌人的体色#就像我的地雷船。我需要添加不同的图像来实现这一点,还是可以通过编码来实现??我甚至想让地精(敌人)显示打玩家的动画,我有这些图像,我只想让敌人显示敌人ehen,玩家离他2或3像素,在玩家距离地精5或6像素半径后靠近他!救命啊!我知道这有点长,但仍然! 这是我的敌人阶级: class enemy(object): walkRight = [pygame.image.load('R1E.png'), pygame.image.lo
class enemy(object):
walkRight = [pygame.image.load('R1E.png'), pygame.image.load('R2E.png'), pygame.image.load('R3E.png'), pygame.image.load('R4E.png'), pygame.image.load('R5E.png'), pygame.image.load('R6E.png'), pygame.image.load('R7E.png'), pygame.image.load('R8E.png'), pygame.image.load('R9E.png'), pygame.image.load('R10E.png'), pygame.image.load('R11E.png')]
walkLeft = [pygame.image.load('L1E.png'), pygame.image.load('L2E.png'), pygame.image.load('L3E.png'), pygame.image.load('L4E.png'), pygame.image.load('L5E.png'), pygame.image.load('L6E.png'), pygame.image.load('L7E.png'), pygame.image.load('L8E.png'), pygame.image.load('L9E.png'), pygame.image.load('L10E.png'), pygame.image.load('L11E.png')]
在本章中,从8到结尾是攻击图像:
def __init__(self, x, y, width, height, end):
self.x = x
self.y = y
self.width = width
self.height = height
self.end = end
self.path = [self.x, self.end]
self.walkCount = 0
self.vel = 3
self.hitbox = (self.x + 17, self.y + 2, 31, 57)
self.health = 10
self.visible = True
def draw(self,win):
self.move()
if self.visible:
if self.walkCount + 1 >= 33:
self.walkCount = 0
if self.vel > 0:
win.blit(self.walkRight[self.walkCount //3], (self.x, self.y))
self.walkCount += 1
else:
win.blit(self.walkLeft[self.walkCount //3], (self.x, self.y))
self.walkCount += 1
pygame.draw.rect(win, (255,0,0), (self.hitbox[0], self.hitbox[1] - 20, 50, 10))
pygame.draw.rect(win, (0,128,0), (self.hitbox[0], self.hitbox[1] - 20, 50 - (5 * (10 - self.health)), 10))
self.hitbox = (self.x + 17, self.y + 2, 31, 57)
#pygame.draw.rect(win, (255,0,0), self.hitbox,2) #It makes a rectangle arount the goblin
def move(self):
if self.vel > 0:
if self.x + self.vel < self.path[1]:
self.x += self.vel
else:
self.vel = self.vel * -1
self.walkCount = 0
else:
if self.x - self.vel > self.path[0]:
self.x += self.vel
else:
self.vel = self.vel * -1
self.walkCount = 0
def hit(self):
if self.health > 0:
self.health -= 1
else:
self.visible = False
def redrawGameWindow():
win.blit(bg, (0,0))
text = font.render('Score : ' + str(score), 1, (0,0,0))
win.blit(text, (201, 12))
man.draw(win)
goblin.draw(win)
for bullet in bullets:
bullet.draw(win)
pygame.display.update()
定义初始(自、x、y、宽度、高度、终点):
self.x=x
self.y=y
self.width=宽度
自我高度=高度
self.end=结束
self.path=[self.x,self.end]
self.walkCount=0
self.vel=3
self.hitbox=(self.x+17,self.y+2,31,57)
自我健康=10
self.visible=True
def抽签(自我,赢):
self.move()
如果自可见:
如果self.walkCount+1>=33:
self.walkCount=0
如果self.vel>0:
blit(self.walkRight[self.walkCount//3],(self.x,self.y))
self.walkCount+=1
其他:
blit(self.walkLeft[self.walkCount//3],(self.x,self.y))
self.walkCount+=1
pygame.draw.rect(赢,(255,0,0),(self.hitbox[0],self.hitbox[1]-20,50,10))
pygame.draw.rect(赢,(0128,0),(self.hitbox[0],self.hitbox[1]-20,50-(5*(10-自我健康)),10))
self.hitbox=(self.x+17,self.y+2,31,57)
#pygame.draw.rect(赢,(255,0,0),self.hitbox,2)#它在地精周围形成一个矩形
def移动(自我):
如果self.vel>0:
如果self.x+self.velself.path[0]:
self.x+=self.vel
其他:
self.vel=self.vel*-1
self.walkCount=0
def命中(自身):
如果self.health>0:
自我健康-=1
其他:
self.visible=False
def重画游戏窗口():
温·布利特(背景,(0,0))
text=font.render('Score:'+str(Score),1,(0,0,0))
win.blit(文本,(201,12))
男子平局(胜利)
地精。平局(赢)
对于子弹中的子弹:
子弹。抽签(赢)
pygame.display.update()
您实际上需要一个图像
还有一张面具图像。mak图像只有两种颜色,黑色和白色。遮罩的白色区域定义图像中需要更改颜色的区域:
以下函数使用对象图像
,pygame.Surface
对象maskImage
和颜色(新颜色
)来创建新曲面。在新曲面中,由maskImage
定义的区域的颜色更改为newColor
:
def changColor(图像、maskImage、newColor):
coloredImage=pygame.Surface(image.get_size())
颜色图像填充(新颜色)
masked=maskImage.copy()
蒙版。设置颜色键((0,0,0))
蒙面。blit(彩色图像,(0,0),无,pygame。混合)
finalImage=image.copy()
finalImage.blit(屏蔽,(0,0),无)
返回最终授权
最简单的例子:
导入pygame
def changColor(图像、maskImage、新颜色):
coloredImage=pygame.Surface(image.get_size())
颜色图像填充(新颜色)
masked=maskImage.copy()
蒙版。设置颜色键((0,0,0))
蒙面。blit(彩色图像,(0,0),无,pygame。混合)
finalImage=image.copy()
finalImage.blit(屏蔽,(0,0),无)
返回最终授权
pygame.init()
window=pygame.display.set_模式((40484))
image=pygame.image.load('avatar64.png')。convert_alpha()
maskImage=pygame.image.load('avatar64mask.png')。convert_alpha()
颜色=[]
对于范围(0、360、60)内的色调:
colors.append(pygame.Color(0))
颜色[-1].hsla=(色调,100,50,100)
图像=[changColor(图像,maskImage,c)表示c的颜色]
clock=pygame.time.clock()
nextColorTime=0
运行=真
运行时:
时钟滴答(60)
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
窗口填充((255,255,255))
对于i,枚举中的图像(图像):
blit(图像,(10+i*64,10))
pygame.display.flip()
pygame.quit()
退出()