神庙逃亡神庙逃亡游戏,神庙逃亡游戏规则

  神庙逃亡神庙逃亡游戏,神庙逃亡游戏规则

  这篇文章主要为大家介绍了如何利用计算机编程语言和Pygame动画制作一个神庙逃亡类似的小游戏。文中的示例代码讲解详细,感兴趣的小伙伴可以动手尝试一下

  

目录
游戏规则精灵类加载龙加载子弹添加玩家碰撞事件完整代码

  

游戏规则

  用简介及安装动画实现神庙逃亡类似的小游戏,当玩家移动的时候躲避子弹,如果子弹命中玩家或者名字龙都会减速,玩家躲避子弹使更多的子弹打在龙上,当玩家被龙抓到,则游戏输,如果龙被子弹减少速度,与玩家有一定的距离,则玩家获胜。

  

精灵类

  MySprite类(pygame.sprite.Sprite):

  def __init__(自身,目标):

  py游戏。雪碧。雪碧。_ _ init _ _(self)

  self.master_image=None

  self.frame=0

  self.old_frame=-1

  self.frame_width=1

  self.frame_height=1

  self.first_frame=0

  self.last_frame=0

  self.columns=1

  self.last_time=0

  def _getx(self):

  返回self.rect.x

  def _setx(自身,值):

  self.rect.x=值

  X=属性(_getx,_setx)

  def _gety(self):

  返回自我纠正

  定义_设置(自身,值):

  self.rect.y=值

  Y=属性(_gety,_sety)

  def _getpos(self):

  return self.rect.topleft

  def _setpos(自身,位置):

  self.rect.topleft=pos

  position=property(_getpos,_setpos)

  定义负载(自身,文件名,宽度,高度,列):

  自我。master _ image=py游戏。形象。加载(文件名)。convert_alpha()

  self.frame_width=宽度

  self.frame_height=高度

  self.rect=Rect(0,0,宽度,高度)

  self.columns=列

  rect=self。主人_图像。get _ rect()

  自我。last _ frame=(rect。宽度//宽度)*(矩形。高度//高度)-1

  定义更新(自身,当前时间,比率=30):

  如果当前时间自。上次时间费率:

  self.frame=1

  如果自我。陷害自己。最后_帧:

  self.frame=self.first_frame

  self.last_time=当前时间

  如果self.frame!=self.old_frame:

  frame _ x=(self。框架% self。列)*自身。框架_宽度

  frame _ y=(self。框架//自身。列)*自身。框架_高度

  rect=pygame .Rect(frame_x,frame_y,self.frame_wi

  dth, self.frame_height)

   self.image = self.master_image.subsurface(rect)

   self.old_frame = self.frame

   def __str__(self):

   return str(self.frame) + "," + str(self.first_frame) + \

   "," + str(self.last_frame) + "," + str(self.frame_width) + \

   "," + str(self.frame_height) + "," + str(self.columns) + \

   "," + str(self.rect)

  

  MySprite继承精灵类,因为在做动画时候,需要许多的参数,这里进行了初始化,master_image 图片的位置,frame、old_frame分别储存第一帧和上一帧的位置,frame_width和frame_height保存宽高,first和last保存帧的第一帧和最后一帧。columns保存列数。last_time时间变量。
X、Y、pos保存精灵的位置x和y的位置。

  load载入,赋予对应精灵的值,(rect.width // width) * (rect.height // height) - 1,通过给定的图片的宽高除以参数的值,-1就是初始的位置是从0开始的。

  update更新的时候,根据传入的时间和间隔频率,如果时间大于上次时间和间隔则改变帧数,如果帧数改变对应的图片位置也改变,实现动态变化。

  

  

加载龙

  

dragon = MySprite(screen)

  dragon.load("dragon.png", 260, 150, 3)

  dragon.position = 100, 230

  group.add(dragon)

  arrow_vel = 8.0

  game_over = False

  you_win = False

  player_jumping = False

  jump_vel = 0.0

  while True:

   framerate.tick(30)

   ticks = pygame.time.get_ticks()

   for event in pygame.event.get():

   if event.type == pygame.QUIT:

   sys.exit()

   keys = pygame.key.get_pressed()

   if keys[pygame.K_ESCAPE]:

   sys.exit()

   elif keys[pygame.K_SPACE]:

   if not player_jumping:

   player_jumping = True

   jump_vel = -8.0

   screen.blit(bg, (0, 0))

   if not game_over:

   group.update(ticks, 50)

   group.draw(screen)

   print_text(font, 350, 360, "Press SPACE to jump!")

   if game_over:

   print_text(font, 360, 100, "G A M E O V E R")

   if you_win:

   print_text(font, 330, 130, "YOU BEAT THE DRAGON")

   else:

   print_text(font, 330, 130, "THE DRAGON GOT YOU")

   pygame.display.update()

  

  加载龙,实现龙动画,这里没有龙的位移,玩家躲子弹或者命中子弹都会导致双方的x坐标减少,这里没使用对应的速度改变,减少游戏的开发难度。

  

  

加载子弹

  

def reset_arrow():

   y = random.randint(250, 350)

   arrow.position = 800, y

  arrow = MySprite(screen)

  arrow.load("flame.png", 40, 16, 1)

  arrow.position = 800, 320

  group.add(arrow)

  

  子弹因为只有一个,如果击中则需要重置,这里写了重置子弹的函数便于调用,random.randint(250, 350)给子弹一个随机的高度。使游戏与变化。

  

  

添加玩家

  

player = MySprite(screen)

  player.load("caveman.png", 50, 64, 8)

  player.first_frame = 1

  player.last_frame = 7

  player.position = 400, 303

  group.add(player)

  player_start_y = player.Y

  

  player_start_y记录玩家初始的y坐标判断玩家是否处于跳跃状态中。

  

  

碰撞事件

  

 if not game_over:

   arrow.X -= arrow_vel

   if arrow.X < 40:

   reset_arrow()

   if pygame.sprite.collide_rect(arrow, player):

   reset_arrow()

   player.X -= 10

   if pygame.sprite.collide_rect(arrow, dragon):

   reset_arrow()

   dragon.X -= 10

   if pygame.sprite.collide_rect(player, dragon):

   game_over = True

   if dragon.X < -100:

   you_win = True

   game_over = True

   if player_jumping:

   player.Y += jump_vel

   jump_vel += 0.5

   if player.Y > player_start_y:

   player_jumping = False

   player.Y = player_start_y

   jump_vel = 0.0

  

  这里需要检验玩家、子弹和龙两两相碰的事件。

  

  

完整代码

  

import sys, time, random, math, pygame

  from pygame.locals import *

  class MySprite(pygame.sprite.Sprite):

   def __init__(self, target):

   pygame.sprite.Sprite.__init__(self) # extend the base Sprite class

   self.master_image = None

   self.frame = 0

   self.old_frame = -1

   self.frame_width = 1

   self.frame_height = 1

   self.first_frame = 0

   self.last_frame = 0

   self.columns = 1

   self.last_time = 0

   # X property

   def _getx(self):

   return self.rect.x

   def _setx(self, value):

   self.rect.x = value

   X = property(_getx, _setx)

   # Y property

   def _gety(self):

   return self.rect.y

   def _sety(self, value):

   self.rect.y = value

   Y = property(_gety, _sety)

   # position property

   def _getpos(self):

   return self.rect.topleft

   def _setpos(self, pos):

   self.rect.topleft = pos

   position = property(_getpos, _setpos)

   def load(self, filename, width, height, columns):

   self.master_image = pygame.image.load(filename).convert_alpha()

   self.frame_width = width

   self.frame_height = height

   self.rect = Rect(0, 0, width, height)

   self.columns = columns

   # try to auto-calculate total frames

   rect = self.master_image.get_rect()

   self.last_frame = (rect.width // width) * (rect.height // height) - 1

   def update(self, current_time, rate=30):

   # update animation frame number

   if current_time > self.last_time + rate:

   self.frame += 1

   if self.frame > self.last_frame:

   self.frame = self.first_frame

   self.last_time = current_time

   # build current frame only if it changed

   if self.frame != self.old_frame:

   frame_x = (self.frame % self.columns) * self.frame_width

   frame_y = (self.frame // self.columns) * self.frame_height

   rect = pygame.Rect(frame_x, frame_y, self.frame_width, self.frame_height)

   self.image = self.master_image.subsurface(rect)

   self.old_frame = self.frame

   def __str__(self):

   return str(self.frame) + "," + str(self.first_frame) + \

   "," + str(self.last_frame) + "," + str(self.frame_width) + \

   "," + str(self.frame_height) + "," + str(self.columns) + \

   "," + str(self.rect)

  def print_text(font, x, y, text, color=(255, 255, 255)):

   imgText = font.render(text, True, color)

   screen.blit(imgText, (x, y))

  def reset_arrow():

   y = random.randint(250, 350)

   arrow.position = 800, y

  pygame.init()

  screen = pygame.display.set_mode((800, 600))

  pygame.display.set_caption("Escape Dragon")

  font = pygame.font.Font(None, 18)

  framerate = pygame.time.Clock()

  bg = pygame.image.load(background.png).convert_alpha()

  group = pygame.sprite.Group()

  dragon = MySprite(screen)

  dragon.load("dragon.png", 260, 150, 3)

  dragon.position = 100, 230

  group.add(dragon)

  player = MySprite(screen)

  player.load("caveman.png", 50, 64, 8)

  player.first_frame = 1

  player.last_frame = 7

  player.position = 400, 303

  group.add(player)

  arrow = MySprite(screen)

  arrow.load("flame.png", 40, 16, 1)

  arrow.position = 800, 320

  group.add(arrow)

  arrow_vel = 8.0

  game_over = False

  you_win = False

  player_jumping = False

  jump_vel = 0.0

  player_start_y = player.Y

  while True:

   framerate.tick(30)

   ticks = pygame.time.get_ticks()

   for event in pygame.event.get():

   if event.type == pygame.QUIT:

   sys.exit()

   keys = pygame.key.get_pressed()

   if keys[pygame.K_ESCAPE]:

   sys.exit()

   elif keys[pygame.K_SPACE]:

   if not player_jumping:

   player_jumping = True

   jump_vel = -8.0

   if not game_over:

   arrow.X -= arrow_vel

   if arrow.X < 40:

   reset_arrow()

   if pygame.sprite.collide_rect(arrow, player):

   reset_arrow()

   player.X -= 10

   if pygame.sprite.collide_rect(arrow, dragon):

   reset_arrow()

   dragon.X -= 10

   if pygame.sprite.collide_rect(player, dragon):

   game_over = True

   if dragon.X < -100:

   you_win = True

   game_over = True

   if player_jumping:

   player.Y += jump_vel

   jump_vel += 0.5

   if player.Y > player_start_y:

   player_jumping = False

   player.Y = player_start_y

   jump_vel = 0.0

   screen.blit(bg, (0, 0))

   if not game_over:

   group.update(ticks, 50)

   group.draw(screen)

   print_text(font, 350, 360, "Press SPACE to jump!")

   if game_over:

   print_text(font, 360, 100, "G A M E O V E R")

   if you_win:

   print_text(font, 330, 130, "YOU BEAT THE DRAGON")

   else:

   print_text(font, 330, 130, "THE DRAGON GOT YOU")

   pygame.display.update()

  

  以上就是Python+Pygame实现神庙逃亡游戏的详细内容,更多关于Python Pygame神庙逃亡的资料请关注盛行IT软件开发工作室其它相关文章!

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: