def add_zombie(): zombie = MySprite(screen) zombie.load("pic/zombie walk.png", 96, 96, 8) zombie.position = random.randint(0, 700), random.randint(0, 500) zombie.direction = random.randint(0, 3) * 2 zombie_group.add(zombie)
def main(): pygame.init() # 初始化窗口 screen = pygame.display.set_mode((1088, 664), 0, 32) # 设置标题 pygame.display.set_caption("五军都督府") font = pygame.font.Font(None, 18) framerate = pygame.time.Clock() # 背景图片 background = pygame.image.load("D:/tongcheng/game/lo.png").convert_alpha() mouse = pygame.image.load("D:/tongcheng/game/shubiao.png").convert_alpha() # 背景音乐 pygame.mixer.music.load("D:/tongcheng/music/TianGong.mp3") pygame.mixer.music.play() cat = MySprite(screen) cat.load("D:/tongcheng/game/hy0.png", 35, 40, 9) cat.position = 300, 90 cat1 = MySprite(screen) cat1.load("D:/tongcheng/game/hy0.png", 35, 40, 9) cat1.position = 760, 90 cat2 = MySprite(screen) cat2.load("D:/tongcheng/game/hy0.png", 35, 40, 9) cat2.position = 85, 200 cat3 = MySprite(screen) cat3.load("D:/tongcheng/game/hy0.png", 35, 40, 9) cat3.position = 85, 430 cat4 = MySprite(screen) cat4.load("D:/tongcheng/game/hy0.png", 35, 40, 9) cat4.position = 300, 540 cat5 = MySprite(screen) cat5.load("D:/tongcheng/game/hy0.png", 35, 40, 9) cat5.position = 760, 540 cat6 = MySprite(screen) cat6.load("D:/tongcheng/game/hy0.png", 35, 40, 9) cat6.position = 980, 430 cat7 = MySprite(screen) cat7.load("D:/tongcheng/game/hy0.png", 35, 40, 9) cat7.position = 980, 200 cat_fh = MySprite(screen) cat_fh.load("D:/tongcheng/game/fhs.png", 180, 145, 4) cat_fh.position = 850, 70 cat_fh.first_frame = 0 cat_fh.last_frame = 3 cat_lo = MySprite(screen) cat_lo.load("D:/tongcheng/game/long.png", 80, 110, 8) cat_lo.position = 100, 50 cat_lo.first_frame = 0 cat_lo.last_frame = 7 catgroup = pygame.sprite.Group() catgroup.add(cat, cat1, cat2, cat3, cat4, cat5, cat6, cat7) catgroup1 = pygame.sprite.Group() catgroup1.add(cat_fh) catgroup3 = pygame.sprite.Group() catgroup3.add(cat_lo) while True: # 清屏 screen.fill((0, 0, 100)) # 设置帧的速率 framerate.tick(15) ticks = pygame.time.get_ticks() # 使其可以循环播放背景音乐 if pygame.mixer.music.get_busy() == False: pygame.mixer.music.play() #接收键盘事件 for event in pygame.event.get(): keys = pygame.key.get_pressed() if event.type == pygame.QUIT: sys.exit() #上下左右 elif keys[K_UP] or keys[K_w]: cat_lo.direction = 6 elif keys[K_RIGHT] or keys[K_d]: cat_lo.direction = 2 elif keys[K_DOWN] or keys[K_s]: cat_lo.direction = 4 elif keys[K_LEFT] or keys[K_a]: cat_lo.direction = 3 else: #玩家静止 cat_lo.direction = 0 # 接收鼠标当前悬浮位置 if event.type == MOUSEMOTION: mouse_x, mouse_y = event.pos #键盘输入esc退出 key = pygame.key.get_pressed() if key[pygame.K_ESCAPE]: exit() #角色的状态帧的起点终点取决于方向direction cat_lo.first_frame = cat_lo.direction * cat_lo.columns cat_lo.last_frame = cat_lo.first_frame + cat_lo.columns - 1 # self.frame #??? if cat_lo.frame < cat_lo.first_frame: cat_lo.frame = cat_lo.first_frame #算出移动距离, 实施移动玩家坐标, 移动距离和 方向 和 速度(速度定义为V, 单位帧移动像素距离) 有关 V = 10 if cat_lo.direction == 6: # X、Y 从哪来 #??? cat_lo.Y += -V elif cat_lo.direction == 2: cat_lo.X += V elif cat_lo.direction == 4: cat_lo.Y += V elif cat_lo.direction == 3: cat_lo.X += -V elif cat_lo.X >= 800: w = Two() w.two_window() screen.blit(background, (0, 0)) # 鼠标的x,y坐标 m_x, m_y = pygame.mouse.get_pos() # 隐藏鼠标 pygame.mouse.set_visible(False) m_x -= mouse.get_width() / 3 m_y -= mouse.get_height() / 3 #用其他图形代替鼠标 screen.blit(mouse, (m_x, m_y)) # 更新显示屏幕 catgroup.update(ticks, 120) catgroup.draw(screen) catgroup1.update(ticks, 250) catgroup1.draw(screen) catgroup3.update(ticks, 40) catgroup3.draw(screen) pygame.display.update()
pygame.init() screen = pygame.display.set_mode((800, 600)) pygame.display.set_caption("Zombie Mob") font = pygame.font.Font(None, 36) timer = pygame.time.Clock() pygame.time.set_timer(USEREVENT + 1, 3000) #create sprite groups player_group = pygame.sprite.Group() zombie_group = pygame.sprite.Group() health_group = pygame.sprite.Group() #create the player sprite player = MySprite(screen) player.load("pic/farmer walk.png", 96, 96, 8) player.position = 80, 80 player.direction = 4 player_group.add(player) #create the zombie sprite zombie_image = pygame.image.load("pic/zombie walk.png").convert_alpha() for n in range(0, 10): add_zombie() #create heath sprite health = MySprite(screen) health.load("pic/health.png", 32, 32, 1) health.position = 400, 300 health_group.add(health) game_over = False player_moving = False