学习 Python 之 Pygame 开发魂斗罗

Posted _DiMinisH

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了学习 Python 之 Pygame 开发魂斗罗相关的知识,希望对你有一定的参考价值。

学习 Python 之 Pygame 开发魂斗罗(四)

继续编写魂斗罗

在上次的博客学习 Python 之 Pygame 开发魂斗罗(二)中,我们完成了角色的移动和跳跃,下面我们来继续写魂斗罗。

下面是图片的素材

链接:https://pan.baidu.com/s/1X7tESkes_O6nbPxfpHD6hQ?pwd=hdly
提取码:hdly

1. 创建子弹类

发射子弹首先要有子弹类
我们来创建一下

import pygame
from Constants import *

class Bullet(pygame.sprite.Sprite):

    def __init__(self, person):
        pygame.sprite.Sprite.__init__(self)
        self.images = [
            loadImage('../Image/Bullet/bullet1.png')
        ]
        self.index = 0
        self.image = self.images[self.index]
        # 速度
        self.xSpeed = 1
        self.ySpeed = 1
        self.rect = pygame.Rect(person.rect)

        # 销毁开关
        self.isDestroy = False

    def move(self):
        self.rect.x += self.xSpeed
        self.rect.y += self.ySpeed
        self.checkBullet()

    def draw(self, window):
        window.blit(self.image, self.rect)

    def checkBullet(self):
        toDestroy = False
        if self.rect.top < 0 or self.rect.top > 600:
            toDestroy = True
        if self.rect.left < 0 or self.rect.right > 900:
            toDestroy = True
        if toDestroy:
            self.isDestroy = True

2. 根据玩家方向和状态设置子弹发射的位置

由于玩家的方向和状态不一样,所以玩家开火后,子弹发射的位置也不一样

下面我们来看看角色状态和发射子弹的位置

(1). 站立向右发射子弹

(x,y) 是图片的位置,我们计算出,此状态下,子弹发射的位置是 (x+24,y+11)


下面我们来设置一下

self.rect.x += 24 * 2.5
self.rect.y += 11 * 2.5
self.ySpeed = 0
self.xSpeed = 7

2.5是人物放大的倍数,加载图像的时候把图像放大了2.5倍,所以设置子弹发射位置的时候,同样要乘以2.5

if person.isStanding:
	# 判断方向
	# 方向向右
    if person.direction == Direction.RIGHT:
    	# 向上
        if person.isUp:
            pass
        # 向右
        else:
            self.rect.x += 24 * 2.5
            self.rect.y += 11 * 2.5
            self.ySpeed = 0
            self.xSpeed = 7
    # 方向向左
    else:
        if person.isUp:
            pass
        else:
            pass

elif person.isSquating and not person.isWalking:
    if person.direction == Direction.RIGHT:
        pass
    else:
        pass

elif person.isWalking:
    if person.direction == Direction.RIGHT:
        if person.isUp:
            pass
        elif person.isDown:
            pass
        else:
            pass
    else:
        if person.isUp:
            pass
        elif person.isDown:
            pass
        else:
            pass

elif person.isJumping or person.state == State.FALL:
    if person.direction == Direction.RIGHT:
        pass
    else:
        pass

(2). 站立向左发射子弹

self.rect.y += 11 * 2.5
self.ySpeed = 0
self.xSpeed = -7
# 根据角色的方向和状态设置子弹发射的位置
# 角色站着
if person.isStanding:
	# 判断方向
    if person.direction == Direction.RIGHT:
    	# 向上
        if person.isUp:
            pass
        else:
            self.rect.x += 24 * 2.5
            self.rect.y += 11 * 2.5
            self.ySpeed = 0
            self.xSpeed = 7
    else:
        if person.isUp:
            pass
        else:
            self.rect.y += 11 * 2.5
            self.ySpeed = 0
            self.xSpeed = -7

elif person.isSquating and not person.isWalking:
    if person.direction == Direction.RIGHT:
        pass
    else:
        pass

elif person.isWalking:
    if person.direction == Direction.RIGHT:
        if person.isUp:
            pass
        elif person.isDown:
            pass
        else:
            pass
    else:
        if person.isUp:
            pass
        elif person.isDown:
            pass
        else:
            pass

elif person.isJumping or person.state == State.FALL:
    if person.direction == Direction.RIGHT:
        pass
    else:
        pass

(3). 站立朝上发射子弹

站立向右朝上发射子弹

self.rect.x += 10 * 2.5
self.rect.y += -1 * 2.5
self.ySpeed = -7
self.xSpeed = 0

这里我微微调整了一下y的值,方向向上,所以子弹的y是逐渐减小的,所以y速度是负数

# 根据角色的方向和状态设置子弹发射的位置
# 角色站着
if person.isStanding:
	# 判断方向
    if person.direction == Direction.RIGHT:
    	# 向上
        if person.isUp:
            self.rect.x += 10 * 2.5
			self.rect.y += -1 * 2.5
			self.ySpeed = -7
			self.xSpeed = 0
        else:
            self.rect.x += 24 * 2.5
            self.rect.y += 11 * 2.5
            self.ySpeed = 0
            self.xSpeed = 7
    else:
        if person.isUp:
            pass
        else:
            self.rect.y += 11 * 2.5
            self.ySpeed = 0
            self.xSpeed = -7

elif person.isSquating and not person.isWalking:
    if person.direction == Direction.RIGHT:
        pass
    else:
        pass

elif person.isWalking:
    if person.direction == Direction.RIGHT:
        if person.isUp:
            pass
        elif person.isDown:
            pass
        else:
            pass
    else:
        if person.isUp:
            pass
        elif person.isDown:
            pass
        else:
            pass

elif person.isJumping or person.state == State.FALL:
    if person.direction == Direction.RIGHT:
        pass
    else:
        pass

站立向左朝上发射子弹也是同样的计算方式

self.rect.x += 10 * 2.5
self.rect.y += -1 * 2.5
self.ySpeed = -7
self.xSpeed = 0
# 根据角色的方向和状态设置子弹发射的位置
# 角色站着
if person.isStanding:
	# 判断方向
    if person.direction == Direction.RIGHT:
    	# 向上
        if person.isUp:
            self.rect.x += 10 * 2.5
			self.rect.y += -1 * 2.5
			self.ySpeed = -7
			self.xSpeed = 0
        else:
            self.rect.x += 24 * 2.5
            self.rect.y += 11 * 2.5
            self.ySpeed = 0
            self.xSpeed = 7
    else:
        if person.isUp:
            self.rect.x += 10 * 2.5
            self.rect.y += -1 * 2.5
            self.ySpeed = -7
            self.xSpeed = 0
        else:
            self.rect.y += 11 * 2.5
            self.ySpeed = 0
            self.xSpeed = -7

elif person.isSquating and not person.isWalking:
    if person.direction == Direction.RIGHT:
        pass
    else:
        pass

elif person.isWalking:
    if person.direction == Direction.RIGHT:
        if person.isUp:
            pass
        elif person.isDown:
            pass
        else:
            pass
    else:
        if person.isUp:
            pass
        elif person.isDown:
            pass
        else:
            pass

elif person.isJumping or person.state == State.FALL:
    if person.direction == Direction.RIGHT:
        pass
    else:
        pass

(4). 蹲下发射子弹

向右示意图

self.rect.x += 34 * 2.5
self.rect.y += 25 * 2.5
self.ySpeed = 0
self.xSpeed = 7
# 根据角色的方向和状态设置子弹发射的位置
# 角色站着
if person.isStanding:
	# 判断方向
    if person.direction == Direction.RIGHT:
    	# 向上
        if person.isUp:
            self.rect.x += 10 * 2.5
			self.rect.y += -1 * 2.5
			self.ySpeed = -7
			self.xSpeed = 0
        else:
            self.rect.x += 24 * 2.5
            self.rect.y += 11 * 2.5
            self.ySpeed = 0
            self.xSpeed = 7
    else:
        if person.isUp:
            self.rect.x += 10 * 2.5
            self.rect.y += -1 * 2.5
            self.ySpeed = -7
            self.xSpeed = 0
        else:
            self.rect.y += 11 * 2.5
            self.ySpeed = 0
            self.xSpeed = -7

elif person.isSquating and not person.isWalking:
    if person.direction == Direction.RIGHT:
        self.rect.x += 34 * 2.5
		self.rect.y += 25 * 2.5
		self.ySpeed = 0
		self.xSpeed = 7
    else:
        pass

elif person.isWalking:
    if person.direction == Direction.RIGHT:
        if person.isUp:
            pass
        elif person.isDown:
            pass
        else:
            pass
    else:
        if person.isUp:
            pass
        elif person.isDown:
            pass
        else:
            pass

elif person.isJumping or person.state == State.FALL:
    if person.direction == Direction.RIGHT:
        pass
    else:
        pass

同样的,设置向左的发射位置

self.rect.y += 25 * 2.5
self.ySpeed = 0
self.xSpeed = -7
# 根据角色的方向和状态设置子弹发射的位置
# 角色站着
if person.isStanding:
	# 判断方向
    if person.direction == Direction.RIGHT:
    	# 向上
        if person.isUp:
            self.rect.x += 10 * 2.5
			self.rect.y += -1 * 2.5
			self.ySpeed = -7
			self.xSpeed = 0
        else:
            self.rect.x += 24 * 2.5
            self.rect.y += 11 * 2.5
            self.ySpeed = 0
            self.xSpeed = 7
    else:
        if person.isUp:
            self.rect.x += 10 * 2.5
            self.rect.y += -1 * 2.5
            self.ySpeed = -7
            self.xSpeed = 0
        else:
            self.rect.y += 11 * 2.5
            self.ySpeed = 0
            self.xSpeed = -7

elif person.isSquating and not person.isWalking:
    if person.direction == Direction.RIGHT:
        self.rect.x += 34 * 2.5
		self.rect.y += 25 * 2.5
		self.ySpeed = 0
		self.xSpeed = 7
    else:
        self.rect.y += 25 * 2.5
		self.ySpeed = 0
		self.xSpeed = -7

elif person.isWalking:
    if person.direction == Direction.RIGHT:
        if person.isUp:
            pass
        elif person.isDown:
            pass
        else:
            pass
    else:
        if person.isUp:
            pass
        elif person.isDown:
            pass
        else:
            pass

elif person.isJumping or person.state == State.FALL:
    if person.direction == Direction.RIGHT:
        pass
    else:
        pass

(5). 向斜方发射子弹

向斜右上方发射子弹示意图

self.rect.x += 20 * 2.5
self.rect.y += -1 * 2.5
self.ySpeed = -7
self.xSpeed = 7

向斜左上方发射子弹时的子弹位置

self.rect.x += -3 * 2.5
self.rect.y += -1 * 2.5
self.ySpeed = -7
self.xSpeed = -7

向斜右下方发射子弹示意图

self.rect.x += 21 * 2.5
self.rect.y += 20 * 2.5
self.ySpeed = 7
self.xSpeed = 7

向斜左下方发射子弹时的子弹位置

self.rect.x += -3 * 2.5
self.rect.y += 20 * 2.5
self.ySpeed = 7
self.xSpeed = -7

(6). 奔跑时发射子弹

奔跑时发射子弹和站立发射子弹是一样的

向右

self.rect.x += 24 * 2.5
self.rect.y += 11 * 2.5
self.ySpeed = 0
self.xSpeed = 7

向左

self.rect.y += 11 * 2.5
self.ySpeed = 0
self.xSpeed = -7

完整的代码

if person.isStanding:
    if person.direction == Direction.RIGHT:
        if person.isUp:
            self.rect.x += 10 * 2.5
            self.rect.y += -1 * 2.5
            self.ySpeed = -7
            self.xSpeed = 0
        else:
            self.rect.x += 24 * 2.5
            self.rect.y += 11 * 2.5
            self.ySpeed = 0
            self.xSpeed = 7
    else:
        if person.isUp:
            self.rect.x += 10 * 2.5
            self.rect.y += -1 * 2.5
            self.ySpeed = -7
            self.xSpeed = 0
        else:
            self.rect.y += 11 * 2.5
            self.ySpeed = 0
            self.xSpeed = -7

elif person.isSquating and not person.isWalking:
    if person.direction == Direction.RIGHT:
        self.rect.x += 34 * 2.5
        self.rect.y += 25 * 2.5
        self.ySpeed = 0
        self.xSpeed = 7
    else:
        self.rect.y += 25 * 2.5
        self.ySpeed = 0
        self.xSpeed = -7

elif person.isWalking:
    if person.direction == Direction.RIGHT:
        if person.isUp:
            self.rect.x += 20 * 2.5
            self.rect.y += -1 * 2.5
            self.ySpeed = -7
            self.xSpeed = 7
        elif person.isDown:
            self.rect.x += 21 * 2.5
            self.rect.y += 20 * 2.5
            self.ySpeed = 7
            self.xSpeed = 7
        else:
            self以上是关于学习 Python 之 Pygame 开发魂斗罗的主要内容,如果未能解决你的问题,请参考以下文章

学习 Python 之 Pygame 开发魂斗罗

学习 Python 之 Pygame 开发魂斗罗

学习 Python 之 Pygame 开发魂斗罗

学习 Python 之 Pygame 开发魂斗罗

学习 Python 之 Pygame 开发魂斗罗

学习 Python 之 Pygame 开发魂斗罗