欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

Python+Pygame实现神庙逃亡游戏

时间:2022-11-08 09:50:01|栏目: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()

上一篇:Python学习之集合的常用方法总结

栏    目:Python代码

下一篇:Python垃圾邮件的逻辑回归分类示例详解

本文标题:Python+Pygame实现神庙逃亡游戏

本文地址:http://www.codeinn.net/misctech/218456.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有