闪电侠接金币的NPC类

python npc ai move 闪电侠接金币的NPC类

python npc ai move 闪电侠接金币的NPC类

。非完家控制的角色叫NPC。这个NPC比较简单,有兴趣可以让它有时候也反向,设置一定的机率即可。
下面代码预览:

"""
   NPC类,本程序实现由电脑控制的AI角色类。
   这个角色自动移动,碰到屏幕边缘就反弹,同时换相反方向的序列造型。
"""
import math
import time
import pygame
from random  import randint
from pygame.locals import *

class NPC(pygame.sprite.Sprite):
    """电脑控制的AI角色类"""
    def __init__(self,rms,lms,screen):
        """
           rms:向右造型序列
           lms:向左造型序列
           screen:所在屏幕
        """
        self.shapes_right = rms
        self.shapes_left = lms
         
    def setshape(self):
        """设置造型"""

    def update(self):
        """更新坐标"""
        
    def bounce_on_edge(self):
        """到了边缘就反弹"""
      

if __name__ == "__main__":

    screen = pygame.display.set_mode((960,720))
    pygame.display.set_caption("闪电侠接金币的NPC类")
    images = [f"im2/flash{i}.png" for i in range(1,4)]
    images = [pygame.image.load(im) for im in images]
    # 向右造型序列
    images_right = [pygame.transform.rotozoom(im,0,0.4) for im in images]
 
    # 向左造型序列
    images_left = [pygame.transform.flip(im,True,False) for im in images_right]

    bg = pygame.image.load("space.png")
    AI= NPC(images_right,images_left,screen)
    
    clock = pygame.time.Clock()
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == QUIT:running=False                   
            
        AI.update()

        screen.blit(bg,(0,0))
        screen.blit(AI.image,AI.rect)
        pygame.display.update()
    pygame.quit()        

如需要查看完整源代码,请

成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)

关于李兴球

李兴球的博客是Python创意编程原创博客
此条目发表在pygame, python分类目录。将固定链接加入收藏夹。