class State_GameOver(StateBase): def __init__(self,gameDisplay,gameWidth,gameHeight,FPS,stateID): StateBase.__init__(self,gameDisplay, gameWidth, gameHeight,FPS,stateID) self.themeSound = MySound("res/sounds/gameOverSound.ogg") self.myScoreRef = MyScore() def renderState(self): self.renderSP() self.renderScore() def updateState(self): self.playThemeSound(self.themeSound) if pygame.key.get_pressed()[K_6]: self.BG.resetCountAndDelay() self.themeSound.stop() self.setCurrentStateID(0) def initSP(self,path): self.BG = MySprite(self.gameDisplay,path) def renderSP(self): self.BG.setFadeInImg(1) self.BG.renderImgConvert(0,0) def renderScore(self): pass
def initMap1Layer1(self, gameDisplay): self.map1Layer1 = MySprite(gameDisplay, "res/images/map1Layer1.png") self.map1Layer1List = [ self.map1Layer1, self.map1Layer1, self.map1Layer1 ] self.map1Layer1PosXList = [0, 2 * self.gameWidth] self.map1Layer1PosYList = [0, 0]
def initMap1Layer2(self, gameDisplay): self.map1Layer2 = MySprite(gameDisplay, "res/images/map1Layer2.png") self.map1Layer2List = [ self.map1Layer2, self.map1Layer2, self.map1Layer2 ] self.map1Layer2PosXList = [0, 2 * self.gameWidth] self.map1Layer2PosYList = [0, 0]
class State_Menu(StateBase): def __init__(self, gameDisplay, gameWidth, gameHeight, FPS, stateID): StateBase.__init__(self, gameDisplay, gameWidth, gameHeight, FPS, stateID) self.initSP(gameDisplay) self.themeSound = MySound("res/sounds/menuTheme.ogg") def renderState(self): self.renderSP() def updateState(self): self.playThemeSound(self.themeSound) if pygame.key.get_pressed()[K_SPACE]: self.BG.resetCountAndDelay() self.themeSound.stop() self.setCurrentStateID(1) def initSP(self, gameDisplay): self.BG = MySprite(gameDisplay, "res/images/menuBG4.png") self.spaceTostartBG = MySprite(gameDisplay, "res/sprites/spaceTostartSP.png", 2, 580, 50) def renderSP(self): self.BG.setFadeInImg(1) self.BG.renderImgConvert(0, 0) if self.BG.alphaIn > 40: self.spaceTostartBG.loop(self.gameWidth / 2 - 280, self.gameHeight / 2 + 200, 10)
class State_Menu(StateBase): def __init__(self,gameDisplay,gameWidth,gameHeight,FPS,stateID): StateBase.__init__(self,gameDisplay, gameWidth, gameHeight,FPS,stateID) self.initSP(gameDisplay) self.themeSound = MySound("res/sounds/menuTheme.ogg") def renderState(self): self.renderSP() def updateState(self): self.playThemeSound(self.themeSound) if pygame.key.get_pressed()[K_SPACE]: self.BG.resetCountAndDelay() self.themeSound.stop() self.setCurrentStateID(1) def initSP(self,gameDisplay): self.BG = MySprite(gameDisplay,"res/images/menuBG4.png") self.spaceTostartBG = MySprite(gameDisplay,"res/sprites/spaceTostartSP.png",2,580,50) def renderSP(self): self.BG.setFadeInImg(1) self.BG.renderImgConvert(0,0) if self.BG.alphaIn > 40: self.spaceTostartBG.loop(self.gameWidth/2 - 280, self.gameHeight/2 +200,10)
def initSP(self): flySP = MySprite(self.gameDisplay,"res/sprites/justiceFlySP.png",2,225,175) slashSP = MySprite(self.gameDisplay,"res/sprites/justiceSlashSP.png",2,225,175) weapon1SP = MySprite(self.gameDisplay,"res/sprites/justiceWeapon1SP.png",2,225,175) weapon2SP = MySprite(self.gameDisplay,"res/sprites/justiceWeapon2SP.png",2,225,175) weapon3SP = MySprite(self.gameDisplay,"res/sprites/justiceWeapon3SP.png",2,225,175) shieldSP = MySprite(self.gameDisplay,"res/sprites/justiceShield.png",2,225,175) self.SPList = [flySP,weapon1SP,weapon2SP,weapon3SP,slashSP,shieldSP]
def getSprite(self): if not self.bloqueDestruido: sprite = MySprite(self.directorio + self.spriteBloque + ".png",self.x, self.y, 0) else: if self.tienePowerUp: sprite = MySprite(self.spritePowerUp,self.x, self.y, 0) if (not self.bloqueDestruido) or (self.tienePowerUp): sprite.listframes(1,1,50,50) sprite.setframe(1) #sprite = sf.Sprite(imagen) sprite.SetCenter(0,0) sprite.SetScale(self.escalaX,self.escalaY) return sprite
def initSP(self): flySP = MySprite(self.gameDisplay, "res/sprites/freedomFlySP.png", 2, 225, 175) slashSP = MySprite(self.gameDisplay, "res/sprites/freedomSlashSP.png", 2, 225, 175) weapon1SP = MySprite(self.gameDisplay, "res/sprites/freedomWeapon1SP.png", 2, 225, 175) weapon2SP = MySprite(self.gameDisplay, "res/sprites/freedomWeapon2SP.png", 2, 225, 175) weapon3SP = MySprite(self.gameDisplay, "res/sprites/freedomWeapon3SP.png", 2, 225, 175) weapon4SP = MySprite(self.gameDisplay, "res/sprites/freedomWeapon4SP.png", 2, 225, 175) shieldSP = MySprite(self.gameDisplay, "res/sprites/freedomShield.png", 2, 225, 175) self.SPList = [ flySP, weapon1SP, weapon2SP, weapon3SP, weapon4SP, slashSP, shieldSP ]
y = random.randint(250, 350) arrow.position = 800, y #main program begins pygame.init() screen = pygame.display.set_mode((800, 600)) pygame.display.set_caption("Escape The dragon Game") font = pygame.font.Font(None, 18) framerate = pygame.time.Clock() bg = pygame.image.load("pic/background.png").convert_alpha() group = pygame.sprite.Group() dragon = MySprite(screen) dragon.load("pic/dragon.png", 260, 150, 3) dragon.position = 100, 230 group.add(dragon) player = MySprite(screen) player.load("pic/caveman.png", 50, 64, 8) player.first_frame = 1 player.last_frame = 7 player.position = 400, 300 group.add(player) arrow = MySprite(screen) arrow.load("pic/flame.png", 40, 16, 1) arrow.position = 800, 320 group.add(arrow)
def getSprite(self): sprite = MySprite(self.spriteName, self.x, self.y, 10) sprite.t = 20.0 sprite.listframes(1, 7, 50, 50) sprite.setframe(1) return sprite
def initMap(self, gameDisplay): self.fadeOutBG = MySprite(gameDisplay, "res/images/fadeOut.png") self.initMap1Layer1(gameDisplay) self.initMap1Layer2(gameDisplay)
class State_GamePlay(StateBase): def __init__(self, gameDisplay, gameWidth, gameHeight, FPS, stateID): StateBase.__init__(self, gameDisplay, gameWidth, gameHeight, FPS, stateID) self.initMap(gameDisplay) self.themeSound = MySound("res/sounds/gamePlayTheme2.ogg") self.enemy1Num = 2 self.enemy2Num = 1 self.enemy3Num = 1 self.myClockRef = MyClock(FPS) self.setUpState(gameDisplay, gameWidth, gameHeight, FPS) self.cPlayerDead = 0 self.myScoreRef = MyScore() self.myTextWriterRef = MyTextWriter(gameDisplay, gameWidth, gameHeight) self.scoreSummonBoss = 2000 def renderState(self): self.renderMap() self.renderEnemy() self.renderPlayer() def updateState(self): self.myClockRef.update() self.playThemeSound(self.themeSound) self.updatePlayer() if self.summonEnemy or self.myClockRef.isSec(5): self.summonEnemy = True self.updateEnemy() self.checkCollide() for player in self.playerList: if player.score > self.scoreSummonBoss: self.summonBoss() self.updateScore() self.checkGameOver(self.gameDisplay, self.gameWidth, self.gameHeight, self.FPS) def summonBoss(self): for enemy in self.enemyList: enemy.isBoss = False def checkGameOver(self, gameDisplay, gameWidth, gameHeight, FPS): cPlayerDead = 0 for player in self.playerList: if player.HP <= 0: cPlayerDead += 1 if cPlayerDead == len(self.playerList): self.setUpState(gameDisplay, gameWidth, gameHeight, FPS) self.themeSound.stop() self.setCurrentStateID(self.nextStateId) def initSP(self, gameDisplay): pass def renderMapLayer(self, mapLayerList, mapLayerPosXList, mapLayerPosYList, speed): mapLayerList[0].renderImg(mapLayerPosXList[0], mapLayerPosYList[0]) mapLayerList[1].renderImg(mapLayerPosXList[1], mapLayerPosYList[1]) for i in range(len(mapLayerPosXList)): mapLayerPosXList[i] -= speed if mapLayerPosXList[i] < -2 * self.gameWidth: mapLayerPosXList[i] = 2 * self.gameWidth - speed def initMap(self, gameDisplay): self.fadeOutBG = MySprite(gameDisplay, "res/images/fadeOut.png") self.initMap1Layer1(gameDisplay) self.initMap1Layer2(gameDisplay) def initMap1Layer1(self, gameDisplay): self.map1Layer1 = MySprite(gameDisplay, "res/images/map1Layer1.png") self.map1Layer1List = [ self.map1Layer1, self.map1Layer1, self.map1Layer1 ] self.map1Layer1PosXList = [0, 2 * self.gameWidth] self.map1Layer1PosYList = [0, 0] def initMap1Layer2(self, gameDisplay): self.map1Layer2 = MySprite(gameDisplay, "res/images/map1Layer2.png") self.map1Layer2List = [ self.map1Layer2, self.map1Layer2, self.map1Layer2 ] self.map1Layer2PosXList = [0, 2 * self.gameWidth] self.map1Layer2PosYList = [0, 0] def renderMap(self): self.renderMapLayer(self.map1Layer2List, self.map1Layer2PosXList, self.map1Layer2PosYList, 1) self.renderMapLayer(self.map1Layer1List, self.map1Layer1PosXList, self.map1Layer1PosYList, 2) self.renderFadeIn() def renderFadeIn(self): self.fadeOutBG.setFadeOutImg(10) self.fadeOutBG.renderImgConvert(0, 0) def initEnemy(self, gameDisplay, gameWidth, gameHeight, FPS): self.enemyList = [] self.enemyBeamList = [] for i in range(self.enemy1Num): enemy1Ref = Enemy1(gameDisplay, gameWidth, gameHeight, FPS) self.enemyList.append(enemy1Ref) self.enemyBeamList.append(enemy1Ref.beam) for i in range(self.enemy2Num): enemy2Ref = Enemy2(gameDisplay, gameWidth, gameHeight, FPS) self.enemyList.append(enemy2Ref) self.enemyBeamList.append(enemy2Ref.beam) for i in range(self.enemy3Num): enemy3Ref = Enemy3(gameDisplay, gameWidth, gameHeight, FPS) self.enemyList.append(enemy3Ref) self.enemyBeamList.append(enemy3Ref.beam) enemyBoss1Ref = EnemyBoss1(gameDisplay, gameWidth, gameHeight, FPS) self.enemyList.append(enemyBoss1Ref) self.enemyBeamList.append(enemyBoss1Ref.beam) enemyBoss2Ref = EnemyBoss2(gameDisplay, gameWidth, gameHeight, FPS) self.enemyList.append(enemyBoss2Ref) self.enemyBeamList.append(enemyBoss2Ref.beam) enemyBoss3Ref = EnemyBoss3(gameDisplay, gameWidth, gameHeight, FPS) self.enemyList.append(enemyBoss3Ref) self.enemyBeamList.append(enemyBoss3Ref.beam) def renderEnemy(self): for enemy in self.enemyList: enemy.render() def updateEnemy(self): for enemy in self.enemyList: if not enemy.isBoss: enemy.update() def checkCollidePlayerAndEnemy(self): for player in self.playerList: for sp in player.SPList: for enemy in self.enemyList: if sp.isCollide(enemy.SP): if player.currentMotion == 5: enemy.HPdamage(player.power) if enemy.HP < 0: player.score += enemy.score else: if player.currentMotion != 6: player.HPdamage(enemy.power) def checkCollidePlayerAndEnemyBeam(self): for player in self.playerList: for sp in player.SPList: for beam in self.enemyBeamList: if sp.isCollide(beam.enemyBeamSP): if player.currentMotion != 6: player.HPdamage(beam.power) beam.resetPos() def checkCollidePlayerBeamAndEnemy(self): for player in self.playerList: for beam in player.beamList: for enemy in self.enemyList: if beam.beamSP.isCollide(enemy.SP): enemy.HPdamage(beam.power) beam.resetPos() if enemy.HP < 0: player.score += enemy.score def checkCollide(self): self.checkCollidePlayerAndEnemy() self.checkCollidePlayerAndEnemyBeam() self.checkCollidePlayerBeamAndEnemy() def renderPlayer(self): for player in self.playerList: player.render() def updatePlayer(self): for player in self.playerList: player.update() def initPlayer(self): pass def setUpState(self, gameDisplay, gameWidth, gameHeight, FPS): self.initPlayer(gameDisplay, gameWidth, gameHeight, FPS) self.initEnemy(gameDisplay, gameWidth, gameHeight, FPS) self.fadeOutBG.resetCountAndDelay() self.summonEnemy = False def updateScore(self): pass
def initMap(self,gameDisplay): self.fadeOutBG = MySprite(gameDisplay,"res/images/fadeOut.png") self.initMap1Layer1(gameDisplay) self.initMap1Layer2(gameDisplay)
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 initSP(self, gameDisplay): self.BG = MySprite(gameDisplay, "res/images/menuBG4.png") self.spaceTostartBG = MySprite(gameDisplay, "res/sprites/spaceTostartSP.png", 2, 580, 50)
def initSP(self, gameDisplay): self.singleModeSP = MySprite(gameDisplay, "res/images/singleMode.png") self.coopModeSP = MySprite(gameDisplay, "res/images/coopMode.png")
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()
def initSP(self,gameDisplay): self.BG = MySprite(gameDisplay,"res/images/menuBG4.png") self.spaceTostartBG = MySprite(gameDisplay,"res/sprites/spaceTostartSP.png",2,580,50)
def __init__(self): # Call the parent's constructor MySprite.__init__(self)
def two_window(self): screen = pygame.display.set_mode((1024, 615), 0, 32) pygame.display.set_caption("京城保卫战") # font = pygame.font.Font(None,18) framerate = pygame.time.Clock() background = pygame.image.load( "D:/tongcheng/game/com.jpg").convert_alpha() # 设置背景音乐 pygame.mixer.music.load("D:/tongcheng/music/ZhanDou1.mp3") # 设置鼠标样式 mouse = pygame.image.load( "D:/tongcheng/game/shubiao.png").convert_alpha() text = pygame.font.SysFont("宋体", 50) text_fmt = text.render("You must guard the barrier", 1, (255, 255, 255)) text_fmt1 = text.render('ESC exit game', 1, (255, 255, 255)) game_font = pygame.font.SysFont('宋体', 50, True) self.cat_ao1 = MySprite(screen) self.cat_ao1.load("D:/tongcheng/game/long.png", 80, 110, 8) self.cat_ao1.position = 0, 230 self.cat_ao1.first_frame = 8 self.cat_ao1.last_frame = 15 Y = random.randint(120, 260) #120 190 260 cat_fhg = MySprite(screen) cat_fhg.load("D:/tongcheng/game/fhss.png", 125, 95, 4) cat_fhg.position = 850, Y cat_fhg.first_frame = 0 cat_fhg.last_frame = 3 Y = random.randint(120, 260) cat_fhg1 = MySprite(screen) cat_fhg1.load("D:/tongcheng/game/fhss.png", 125, 95, 4) cat_fhg1.position = 850, Y cat_fhg1.first_frame = 0 cat_fhg1.last_frame = 3 Y = random.randint(120, 260) cat_fhg2 = MySprite(screen) cat_fhg2.load("D:/tongcheng/game/fhss.png", 125, 95, 4) cat_fhg2.position = 850, Y cat_fhg2.first_frame = 0 cat_fhg2.last_frame = 3 cat_fhg_list = [cat_fhg1, cat_fhg2] cat_fhg_list.append(cat_fhg) self.catgroup_s = pygame.sprite.Group() self.catgroup_s.add(cat_fhg, cat_fhg1, cat_fhg2) self.catgroup_z = pygame.sprite.Group() self.catgroup_z.add(self.cat_ao1) score = 0 cat_fhg_dict = dict() FH_SCORE = 100 while True: pygame.init() # 清屏 # screen.fill((0,0,100)) x = random.randint(1, 10) cat_fhg.X -= x x = random.randint(1, 10) cat_fhg1.X -= x x = random.randint(1, 10) cat_fhg2.X -= x if pygame.mixer.music.get_busy() == False: pygame.mixer.music.play() if len(self.catgroup_s) == 0: Y = random.randint(120, 260) cat_fhg.position = 850, Y Y = random.randint(120, 260) cat_fhg1.position = 850, Y Y = random.randint(120, 260) cat_fhg2.position = 850, Y self.catgroup_s.add(cat_fhg, cat_fhg1, cat_fhg2) # 游戏失败 if cat_fhg.X <= 0 or cat_fhg2.X <= 0 or cat_fhg1.X <= 0: self.score = score self.gameover() # 游戏胜利 if score >= 10000: self.gamevictory() # 设置帧的速率 framerate.tick(15) # 得到以毫秒为间隔的时间 ticks = pygame.time.get_ticks() #接收键盘事件 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]: self.cat_ao1.direction = 6 elif keys[K_RIGHT] or keys[K_d]: self.cat_ao1.direction = 2 elif keys[K_DOWN] or keys[K_s]: self.cat_ao1.direction = 4 elif keys[K_LEFT] or keys[K_a]: self.cat_ao1.direction = 3 elif keys[K_x]: self.cat_ao1.direction = 5 else: #玩家静止 self.cat_ao1.direction = 1 # 接收鼠标当前悬浮位置 if event.type == MOUSEMOTION: mouse_x, mouse_y = event.pos #键盘输入esc退出 key = pygame.key.get_pressed() if key[pygame.K_ESCAPE]: exit() #角色的状态帧的起点终点取决于方向direction self.cat_ao1.first_frame = self.cat_ao1.direction * self.cat_ao1.columns self.cat_ao1.last_frame = self.cat_ao1.first_frame + self.cat_ao1.columns - 1 # self.frame #??? if self.cat_ao1.frame < self.cat_ao1.first_frame: self.cat_ao1.frame = self.cat_ao1.first_frame #算出移动距离, 实施移动玩家坐标, 移动距离和 方向 和 速度(速度定义为V, 单位帧移动像素距离) 有关 V = 10 if self.cat_ao1.direction == 6: self.cat_ao1.Y += -V elif self.cat_ao1.direction == 2: self.cat_ao1.X += V elif self.cat_ao1.direction == 4: self.cat_ao1.Y += V elif self.cat_ao1.direction == 3: self.cat_ao1.X += -V elif self.cat_ao1.direction == 5: cat_fhg_dict = pygame.sprite.groupcollide( self.catgroup_s, self.catgroup_z, True, False) score += len(cat_fhg_dict) * FH_SCORE # 计算得分 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)) # 更新显示屏幕 self.catgroup_z.update(ticks, 5) self.catgroup_z.draw(screen) self.catgroup_s.update(ticks, 120) self.catgroup_s.draw(screen) screen.blit(text_fmt, (0, 0)) screen.blit(text_fmt1, (750, 0)) screen.blit(game_font.render('SCORE: %d' % score, True, [0, 0, 0]), [20, 50]) pygame.display.update()
def initSP(self, path, total, width, height): self.SP = MySprite(self.gameDisplay, path, total, width, height)
class State_GamePlay(StateBase): def __init__(self,gameDisplay,gameWidth,gameHeight,FPS,stateID): StateBase.__init__(self,gameDisplay, gameWidth, gameHeight,FPS,stateID) self.initMap(gameDisplay) self.themeSound = MySound("res/sounds/gamePlayTheme2.ogg") self.enemy1Num = 2 self.enemy2Num = 1 self.enemy3Num = 1 self.myClockRef = MyClock(FPS) self.setUpState(gameDisplay,gameWidth,gameHeight,FPS) self.cPlayerDead = 0 self.myScoreRef = MyScore() self.myTextWriterRef = MyTextWriter(gameDisplay, gameWidth, gameHeight) self.scoreSummonBoss = 2000 def renderState(self): self.renderMap() self.renderEnemy() self.renderPlayer() def updateState(self): self.myClockRef.update() self.playThemeSound(self.themeSound) self.updatePlayer() if self.summonEnemy or self.myClockRef.isSec(5): self.summonEnemy = True self.updateEnemy() self.checkCollide() for player in self.playerList: if player.score > self.scoreSummonBoss: self.summonBoss() self.updateScore() self.checkGameOver(self.gameDisplay, self.gameWidth,self.gameHeight,self.FPS) def summonBoss(self): for enemy in self.enemyList: enemy.isBoss = False def checkGameOver(self,gameDisplay, gameWidth, gameHeight, FPS): cPlayerDead = 0 for player in self.playerList: if player.HP <= 0 : cPlayerDead += 1 if cPlayerDead == len(self.playerList): self.setUpState(gameDisplay, gameWidth, gameHeight, FPS) self.themeSound.stop() self.setCurrentStateID(self.nextStateId) def initSP(self,gameDisplay): pass def renderMapLayer(self,mapLayerList,mapLayerPosXList,mapLayerPosYList,speed): mapLayerList[0].renderImg(mapLayerPosXList[0],mapLayerPosYList[0]) mapLayerList[1].renderImg(mapLayerPosXList[1],mapLayerPosYList[1]) for i in range(len(mapLayerPosXList)): mapLayerPosXList[i] -= speed if mapLayerPosXList[i] < -2*self.gameWidth: mapLayerPosXList[i] = 2*self.gameWidth-speed def initMap(self,gameDisplay): self.fadeOutBG = MySprite(gameDisplay,"res/images/fadeOut.png") self.initMap1Layer1(gameDisplay) self.initMap1Layer2(gameDisplay) def initMap1Layer1(self,gameDisplay): self.map1Layer1 = MySprite(gameDisplay,"res/images/map1Layer1.png") self.map1Layer1List = [self.map1Layer1,self.map1Layer1,self.map1Layer1] self.map1Layer1PosXList = [0,2*self.gameWidth] self.map1Layer1PosYList = [0,0] def initMap1Layer2(self,gameDisplay): self.map1Layer2 = MySprite(gameDisplay,"res/images/map1Layer2.png") self.map1Layer2List = [self.map1Layer2,self.map1Layer2,self.map1Layer2] self.map1Layer2PosXList = [0,2*self.gameWidth] self.map1Layer2PosYList = [0,0] def renderMap(self): self.renderMapLayer(self.map1Layer2List,self.map1Layer2PosXList,self.map1Layer2PosYList,1) self.renderMapLayer(self.map1Layer1List,self.map1Layer1PosXList,self.map1Layer1PosYList,2) self.renderFadeIn() def renderFadeIn(self): self.fadeOutBG.setFadeOutImg(10) self.fadeOutBG.renderImgConvert(0, 0) def initEnemy(self,gameDisplay,gameWidth,gameHeight,FPS): self.enemyList = [] self.enemyBeamList = [] for i in range(self.enemy1Num): enemy1Ref = Enemy1(gameDisplay,gameWidth,gameHeight,FPS) self.enemyList.append(enemy1Ref) self.enemyBeamList.append(enemy1Ref.beam) for i in range(self.enemy2Num): enemy2Ref = Enemy2(gameDisplay,gameWidth,gameHeight,FPS) self.enemyList.append(enemy2Ref) self.enemyBeamList.append(enemy2Ref.beam) for i in range(self.enemy3Num): enemy3Ref = Enemy3(gameDisplay,gameWidth,gameHeight,FPS) self.enemyList.append(enemy3Ref) self.enemyBeamList.append(enemy3Ref.beam) enemyBoss1Ref = EnemyBoss1(gameDisplay,gameWidth,gameHeight,FPS) self.enemyList.append(enemyBoss1Ref) self.enemyBeamList.append(enemyBoss1Ref.beam) enemyBoss2Ref = EnemyBoss2(gameDisplay,gameWidth,gameHeight,FPS) self.enemyList.append(enemyBoss2Ref) self.enemyBeamList.append(enemyBoss2Ref.beam) enemyBoss3Ref = EnemyBoss3(gameDisplay,gameWidth,gameHeight,FPS) self.enemyList.append(enemyBoss3Ref) self.enemyBeamList.append(enemyBoss3Ref.beam) def renderEnemy(self): for enemy in self.enemyList: enemy.render() def updateEnemy(self): for enemy in self.enemyList: if not enemy.isBoss: enemy.update() def checkCollidePlayerAndEnemy(self): for player in self.playerList: for sp in player.SPList: for enemy in self.enemyList: if sp.isCollide(enemy.SP): if player.currentMotion == 5: enemy.HPdamage(player.power) if enemy.HP < 0: player.score += enemy.score else: if player.currentMotion != 6: player.HPdamage(enemy.power) def checkCollidePlayerAndEnemyBeam(self): for player in self.playerList: for sp in player.SPList: for beam in self.enemyBeamList: if sp.isCollide(beam.enemyBeamSP): if player.currentMotion != 6: player.HPdamage(beam.power) beam.resetPos() def checkCollidePlayerBeamAndEnemy(self): for player in self.playerList: for beam in player.beamList: for enemy in self.enemyList: if beam.beamSP.isCollide(enemy.SP): enemy.HPdamage(beam.power) beam.resetPos() if enemy.HP < 0: player.score += enemy.score def checkCollide(self): self.checkCollidePlayerAndEnemy() self.checkCollidePlayerAndEnemyBeam() self.checkCollidePlayerBeamAndEnemy() def renderPlayer(self): for player in self.playerList: player.render() def updatePlayer(self): for player in self.playerList: player.update() def initPlayer(self): pass def setUpState(self,gameDisplay,gameWidth,gameHeight,FPS): self.initPlayer(gameDisplay, gameWidth, gameHeight, FPS) self.initEnemy(gameDisplay,gameWidth,gameHeight,FPS) self.fadeOutBG.resetCountAndDelay() self.summonEnemy = False def updateScore(self): pass
@time: 2019/5/27 0027 9:15 @desc:test sprite ''' import pygame, sys from pygame.locals import * from MySprite import MySprite from common import print_text pygame.init() screen = pygame.display.set_mode((800, 600), 0, 32) pygame.display.set_caption("Sprite Animation Demo") font = pygame.font.Font(None, 18) framerate = pygame.time.Clock() #crete the dragon sprite dragon = MySprite(screen) dragon.load("pic/dragon.png", 260, 150, 3) group = pygame.sprite.Group() group.add(dragon) while True: framerate.tick(30) ticks = pygame.time.get_ticks() for even in pygame.event.get(): if even.type == pygame.QUIT: sys.exit() key = pygame.key.get_pressed() if key[K_ESCAPE]: sys.exit() screen.fill((0, 0, 100)) group.update(ticks)
def dh_png(self): self.cat = MySprite(screen) # 传入帧的位置 帧的宽度高度 文件位置 以及帧数 self.cat.load("D:/tongcheng/game/hy0.png", 35, 40, 9) self.cat.position = 200, 200 self.cat1 = MySprite(screen) self.cat1.load("D:/tongcheng/game/hy0.png", 35, 40, 9) self.cat2 = MySprite(screen) self.cat2.load("D:/tongcheng/game/hy0.png", 35, 40, 9) self.cat3 = MySprite(screen) self.cat3.load("D:/tongcheng/game/hy0.png", 35, 40, 9) self.cat4 = MySprite(screen) self.cat4.load("D:/tongcheng/game/hy0.png", 35, 40, 9) self.cat5 = MySprite(screen) self.cat5.load("D:/tongcheng/game/hy0.png", 35, 40, 9) self.cat6 = MySprite(screen) self.cat6.load("D:/tongcheng/game/hy0.png", 35, 40, 9) self.cat7 = MySprite(screen) self.cat7.load("D:/tongcheng/game/hy0.png", 35, 40, 9) self.cat_ao = MySprite(screen) self.cat_ao.load("D:/tongcheng/game/zhengmian.png", 90, 95, 4)
def initSP(self,path): self.BG = MySprite(self.gameDisplay,path)
def getSprite(self): sprite = MySprite(self.spritePlayer,self.iniX+52, self.iniY+52, 10) #sprite.listframes(12, 6, 44, 49) sprite.listframes(4,4,40,50) sprite.setframe(2) return sprite
def initEnemySP(self,path,total,width,height): self.enemyBeamSP = MySprite(self.gameDisplay,path,total,width,height)
zombie_group.add(zombie) 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)