Python+Pygame实现神庙逃亡游戏

作者:我的天才女友 时间:2022-06-12 16:26:28 

游戏规则

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

Python+Pygame实现神庙逃亡游戏

精灵类

class MySprite(pygame.sprite.Sprite):
   def __init__(self, target):
       pygame.sprite.Sprite.__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):
       return self.rect.x

def _setx(self, value):
       self.rect.x = value

X = property(_getx, _setx)

def _gety(self):
       return self.rect.y

def _sety(self, value):
       self.rect.y = value

Y = property(_gety, _sety)

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
       rect = self.master_image.get_rect()
       self.last_frame = (rect.width // width) * (rect.height // height) - 1

def update(self, current_time, rate=30):
       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

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)

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()

来源:https://blog.csdn.net/qq_40801987/article/details/124871408

标签:Python,Pygame,神庙逃亡,游戏
0
投稿

猜你喜欢

  • 详解Oracle修改字段类型方法总结

    2024-01-20 22:40:26
  • oracle ORA-01114、ORA-27067错误解决方法

    2023-07-15 18:36:39
  • XHTML1.0规范:您是否为img图片标签赋予alt属性

    2009-09-21 11:11:00
  • 怎样在不同版本SQL Server中存储数据

    2009-01-20 13:11:00
  • OpenCV半小时掌握基本操作之图像轮廓

    2022-08-22 12:43:13
  • MSSQL数据库排序规则如何更改

    2023-07-01 11:09:58
  • Vue配置文件vue.config.js配置前端代理方式

    2023-07-02 16:39:06
  • 巧妙的自关联运用

    2012-10-07 10:55:58
  • python字典get()方法用法分析

    2023-04-01 23:50:41
  • asp让网站自动识别手机访问跳转至手机网站

    2014-12-06 09:36:02
  • golang实现多协程下载文件(支持断点续传)

    2024-01-31 23:54:34
  • python如何实现API的调用详解

    2023-12-26 18:24:07
  • python安装PIL模块时Unable to find vcvarsall.bat错误的解决方法

    2023-05-17 04:33:11
  • 浅谈Python实现2种文件复制的方法

    2021-09-24 00:25:02
  • numpy拼接矩阵的实现

    2021-02-09 06:35:07
  • mysql中全连接full join...on...的用法说明

    2024-01-13 19:44:22
  • python中pivot()函数基础知识点

    2023-12-18 15:28:06
  • js实现做通讯录的索引滑动显示效果和滑动显示锚点效果

    2024-04-16 09:23:00
  • 实例讲解Python的函数闭包使用中应注意的问题

    2022-05-17 23:29:53
  • PHP引用的调用方法分析

    2023-11-06 15:23:23
  • asp之家 网络编程 m.aspxhome.com