中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

政府網(wǎng)站建設(shè)預(yù)算百度百科詞條入口

政府網(wǎng)站建設(shè)預(yù)算,百度百科詞條入口,知乎免費閱讀網(wǎng)站,網(wǎng)站源碼文件安裝教程學(xué)習(xí) Python 之 Pygame 開發(fā)魂斗羅(三)繼續(xù)編寫魂斗羅1. 角色站立2. 角色移動3. 角色跳躍4. 角色下落繼續(xù)編寫魂斗羅 在上次的博客學(xué)習(xí) Python 之 Pygame 開發(fā)魂斗羅(二)中,我們完成了角色的創(chuàng)建和更新,現(xiàn)…

學(xué)習(xí) Python 之 Pygame 開發(fā)魂斗羅(三)

    • 繼續(xù)編寫魂斗羅
      • 1. 角色站立
      • 2. 角色移動
      • 3. 角色跳躍
      • 4. 角色下落

繼續(xù)編寫魂斗羅

在上次的博客學(xué)習(xí) Python 之 Pygame 開發(fā)魂斗羅(二)中,我們完成了角色的創(chuàng)建和更新,現(xiàn)在具體實現(xiàn)一下更新函數(shù)中的角色狀態(tài)函數(shù)

1. 角色站立

在寫角色站立函數(shù)時,先把其他狀態(tài)函數(shù)注釋了,方便測試
在這里插入圖片描述

在角色站立函數(shù)中,首先設(shè)置當(dāng)前角色的狀態(tài)

站立時,其他狀態(tài)都是False,只有isStanding = True

# 設(shè)置角色狀態(tài)
self.isStanding = True
self.isWalking = False
self.isJumping = False
self.isSquating = False
self.isUp = False
self.isDown = False
self.isFiring = False

其次,修改人物的速度,站立時速度均為0

# 設(shè)置速度
self.ySpeed = 0
self.xSpeed = 0

再次,我們設(shè)置按鍵響應(yīng)事件

# 按下A鍵
if keys[pygame.K_a]:# A按下,角色方向向左self.direction = Direction.LEFT# 改變角色的狀態(tài),角色進入移動狀態(tài)self.state = State.WALK# 設(shè)置站立狀態(tài)為False,移動狀態(tài)為Trueself.isStanding = Falseself.isWalking = True# 向左移動,速度為負數(shù),這樣玩家的x坐標是減小的self.xSpeed = -PLAYER_X_SPEED
# 按下D鍵
elif keys[pygame.K_d]:# D按下,角色方向向右self.direction = Direction.RIGHT# 改變角色的狀態(tài),角色進入移動狀態(tài)self.state = State.WALK# 設(shè)置站立狀態(tài)為False,移動狀態(tài)為Trueself.isStanding = Falseself.isWalking = True# 向右移動,速度為正數(shù)self.xSpeed = PLAYER_X_SPEED
# 按下k鍵
elif keys[pygame.K_k]:# K按下,角色進入跳躍狀態(tài),但是不會改變方向self.state = State.JUMP# 設(shè)置站立狀態(tài)為False,跳躍狀態(tài)為True# 不改變移動狀態(tài),因為移動的時候也可以跳躍self.isStanding = Falseself.isJumping = True# 設(shè)置速度,速度為負數(shù),因為角色跳起后,要下落self.ySpeed = self.jumpSpeed
# 沒有按下按鍵
else:# 沒有按下按鍵,角色依然是站立狀態(tài)self.state = State.STANDself.isStanding = True# 按下w鍵
if keys[pygame.K_w]:# W按下,角色向上,改變方向狀態(tài)self.isUp = Trueself.isStanding = Trueself.isDown = Falseself.isSquating = False
# 按下s鍵
elif keys[pygame.K_s]:# S按下,角色蹲下,改變方向狀態(tài),并且蹲下狀態(tài)設(shè)置為Trueself.isUp = Falseself.isStanding = Falseself.isDown = Trueself.isSquating = True

完整的角色類

import pygame
from Constants import *class PlayerOne(pygame.sprite.Sprite):def __init__(self, currentTime):pygame.sprite.Sprite.__init__(self)# 加載角色圖片self.standRightImage = loadImage('../Image/Player/Player1/Right/stand.png')self.standLeftImage = loadImage('../Image/Player/Player1/Left/stand.png')self.upRightImage = loadImage('../Image/Player/Player1/Up/upRight(small).png')self.upLeftImage = loadImage('../Image/Player/Player1/Up/upLeft(small).png')self.downRightImage = loadImage('../Image/Player/Player1/Down/down.png')self.downLeftImage = loadImage('../Image/Player/Player1/Down/down.png', True)self.obliqueUpRightImages = [loadImage('../Image/Player/Player1/Up/rightUp1.png'),loadImage('../Image/Player/Player1/Up/rightUp2.png'),loadImage('../Image/Player/Player1/Up/rightUp3.png'),]self.obliqueUpLeftImages = [loadImage('../Image/Player/Player1/Up/rightUp1.png', True),loadImage('../Image/Player/Player1/Up/rightUp2.png', True),loadImage('../Image/Player/Player1/Up/rightUp3.png', True),]self.obliqueDownRightImages = [loadImage('../Image/Player/Player1/ObliqueDown/1.png'),loadImage('../Image/Player/Player1/ObliqueDown/2.png'),loadImage('../Image/Player/Player1/ObliqueDown/3.png'),]self.obliqueDownLeftImages = [loadImage('../Image/Player/Player1/ObliqueDown/1.png', True),loadImage('../Image/Player/Player1/ObliqueDown/2.png', True),loadImage('../Image/Player/Player1/ObliqueDown/3.png', True),]# 角色向右的全部圖片self.rightImages = [loadImage('../Image/Player/Player1/Right/run1.png'),loadImage('../Image/Player/Player1/Right/run2.png'),loadImage('../Image/Player/Player1/Right/run3.png')]# 角色向左的全部圖片self.leftImages = [loadImage('../Image/Player/Player1/Left/run1.png'),loadImage('../Image/Player/Player1/Left/run2.png'),loadImage('../Image/Player/Player1/Left/run3.png')]# 角色跳躍的全部圖片self.upRightImages = [loadImage('../Image/Player/Player1/Jump/jump1.png'),loadImage('../Image/Player/Player1/Jump/jump2.png'),loadImage('../Image/Player/Player1/Jump/jump3.png'),loadImage('../Image/Player/Player1/Jump/jump4.png'),]self.upLeftImages = [loadImage('../Image/Player/Player1/Jump/jump1.png', True),loadImage('../Image/Player/Player1/Jump/jump2.png', True),loadImage('../Image/Player/Player1/Jump/jump3.png', True),loadImage('../Image/Player/Player1/Jump/jump4.png', True),]self.rightFireImages = [loadImage('../Image/Player/Player1/Right/fire1.png'),loadImage('../Image/Player/Player1/Right/fire2.png'),loadImage('../Image/Player/Player1/Right/fire3.png'),]self.leftFireImages = [loadImage('../Image/Player/Player1/Right/fire1.png', True),loadImage('../Image/Player/Player1/Right/fire2.png', True),loadImage('../Image/Player/Player1/Right/fire3.png', True),]# 角色左右移動下標self.imageIndex = 0# 角色跳躍下標self.upImageIndex = 0# 角色斜射下標self.obliqueImageIndex = 0# 上一次顯示圖片的時間self.runLastTimer = currentTimeself.fireLastTimer = currentTime# 選擇當(dāng)前要顯示的圖片self.image = self.standRightImage# 獲取圖片的rectself.rect = self.image.get_rect()# 設(shè)置角色的狀態(tài)self.state = State.STAND# 角色的方向self.direction = Direction.RIGHT# 速度self.xSpeed = PLAYER_X_SPEEDself.ySpeed = 0self.jumpSpeed = -11# 人物當(dāng)前的狀態(tài)標志self.isStanding = Falseself.isWalking = Falseself.isJumping = Trueself.isSquating = Falseself.isFiring = False# 重力加速度self.gravity = 0.7self.isUp = Falseself.isDown = Falsedef update(self, keys, currentTime):# 更新站或者走的狀態(tài)# 根據(jù)狀態(tài)響應(yīng)按鍵if self.state == State.STAND:self.standing(keys, currentTime)# elif self.state == State.WALK:#   self.walking(keys, currentTime)# elif self.state == State.JUMP:#     self.jumping(keys, currentTime)# elif self.state == State.FALL:#     self.falling(keys, currentTime)# 更新位置# 記錄前一次的位置坐標pre = self.rect.xself.rect.x += self.xSpeedself.rect.y += self.ySpeed# 如果x位置小于0了,就不能移動,防止人物跑到屏幕左邊if self.rect.x <= 0:self.rect.x = pre# 更新動畫# 跳躍狀態(tài)if self.isJumping:# 根據(jù)方向if self.direction == Direction.RIGHT:# 方向向右,角色加載向右跳起的圖片self.image = self.upRightImages[self.upImageIndex]else:# 否則,方向向左,角色加載向左跳起的圖片self.image = self.upLeftImages[self.upImageIndex]# 角色蹲下if self.isSquating:if self.direction == Direction.RIGHT:# 加載向右蹲下的圖片self.image = self.downRightImageelse:# 加載向左蹲下的圖片self.image = self.downLeftImage# 角色站著if self.isStanding:if self.direction == Direction.RIGHT:if self.isUp:# 加載向右朝上的圖片self.image = self.upRightImageelif self.isDown:# 加載向右蹲下的圖片self.image = self.downRightImageelse:# 加載向右站著的圖片self.image = self.standRightImageelse:# 向左也是同樣的效果if self.isUp:self.image = self.upLeftImageelif self.isDown:self.image = self.downLeftImageelse:self.image = self.standLeftImage# 角色移動if self.isWalking:if self.direction == Direction.RIGHT:if self.isUp:# 加載斜右上的圖片self.image = self.obliqueUpRightImages[self.obliqueImageIndex]elif self.isDown:# 加載斜右下的圖片self.image = self.obliqueDownRightImages[self.obliqueImageIndex]else:# 加載向右移動的圖片,根據(jù)開火狀態(tài)是否加載向右開火移動的圖片if self.isFiring:self.image = self.rightFireImages[self.imageIndex]else:self.image = self.rightImages[self.imageIndex]else:if self.isUp:self.image = self.obliqueUpLeftImages[self.obliqueImageIndex]elif self.isDown:self.image = self.obliqueDownLeftImages[self.obliqueImageIndex]else:if self.isFiring:self.image = self.leftFireImages[self.imageIndex]else:self.image = self.leftImages[self.imageIndex]def standing(self, keys, currentTime):"""角色站立"""# 設(shè)置角色狀態(tài)self.isStanding = Trueself.isWalking = Falseself.isJumping = Falseself.isSquating = Falseself.isUp = Falseself.isDown = Falseself.isFiring = False# 設(shè)置速度self.ySpeed = 0self.xSpeed = 0# 按下A鍵if keys[pygame.K_a]:# A按下,角色方向向左self.direction = Direction.LEFT# 改變角色的狀態(tài),角色進入移動狀態(tài)self.state = State.WALK# 設(shè)置站立狀態(tài)為False,移動狀態(tài)為Trueself.isStanding = Falseself.isWalking = True# 向左移動,速度為負數(shù),這樣玩家的x坐標是減小的self.xSpeed = -PLAYER_X_SPEED# 按下D鍵elif keys[pygame.K_d]:# D按下,角色方向向右self.direction = Direction.RIGHT# 改變角色的狀態(tài),角色進入移動狀態(tài)self.state = State.WALK# 設(shè)置站立狀態(tài)為False,移動狀態(tài)為Trueself.isStanding = Falseself.isWalking = True# 向右移動,速度為正數(shù)self.xSpeed = PLAYER_X_SPEED# 按下k鍵elif keys[pygame.K_k]:# K按下,角色進入跳躍狀態(tài),但是不會改變方向self.state = State.JUMP# 設(shè)置站立狀態(tài)為False,跳躍狀態(tài)為True# 不改變移動狀態(tài),因為移動的時候也可以跳躍self.isStanding = Falseself.isJumping = True# 設(shè)置速度,速度為負數(shù),因為角色跳起后,要下落self.ySpeed = self.jumpSpeed# 沒有按下按鍵else:# 沒有按下按鍵,角色依然是站立狀態(tài)self.state = State.STANDself.isStanding = True# 按下w鍵if keys[pygame.K_w]:# W按下,角色向上,改變方向狀態(tài)self.isUp = Trueself.isStanding = Trueself.isDown = Falseself.isSquating = False# 按下s鍵elif keys[pygame.K_s]:# S按下,角色蹲下,改變方向狀態(tài),并且蹲下狀態(tài)設(shè)置為Trueself.isUp = Falseself.isStanding = Falseself.isDown = Trueself.isSquating = True

完成角色站立后,我們試一下效果怎么樣

在主類中創(chuàng)建角色,并放入pygame.sprite.Group中

class MainGame:player1 = NoneallSprites = None

在__init__()函數(shù)中添加代碼

# 初始化角色
MainGame.player1 = PlayerOne(pygame.time.get_ticks())
# 設(shè)置角色的初始位置
# 這里設(shè)置為(0,80),可以實現(xiàn)一開始玩家掉下來的動畫,目前沒有實現(xiàn)掉落,所以直接設(shè)置為(80,300)
# MainGame.player1.rect.x = 80
# MainGame.player1.rect.bottom = 0
MainGame.player1.rect.x = 80
MainGame.player1.rect.bottom = 300# 把角色放入組中,方便統(tǒng)一管理
MainGame.allSprites = pygame.sprite.Group(MainGame.player1)

之后在循環(huán)中調(diào)用角色的update函數(shù)

為了方便,我把物體的更新全部放在一起,創(chuàng)建一個update()函數(shù)

在主類中添加函數(shù)

def update(self, window):# 更新物體currentTime = pygame.time.get_ticks()MainGame.allSprites.update(self.keys, currentTime)# 顯示物體MainGame.allSprites.draw(window)

pygame.sprite.Group()中的物體,可以統(tǒng)一更新,這就是它的方便之處

因為魂斗羅中玩家移動的時候,場景中的物體也是要移動的,所以地圖是一個長條狀,當(dāng)玩家向右移動時,實際上是地圖向左移動,玩家不動,創(chuàng)建中的物體向左移動,如果不把全部物體放到組中,不好統(tǒng)一管理

完整的主類代碼

import sys
import pygame
from Constants import *
from PlayerOne import PlayerOneclass MainGame:player1 = NoneallSprites = Nonewindow = Nonedef __init__(self):# 初始化展示模塊pygame.display.init()SCREEN_SIZE = (SCREEN_WIDTH, SCREEN_HEIGHT)# 初始化窗口MainGame.window = pygame.display.set_mode(SCREEN_SIZE)# 設(shè)置窗口標題pygame.display.set_caption('魂斗羅角色')# 是否結(jié)束游戲self.isEnd = False# 獲取按鍵self.keys = pygame.key.get_pressed()# 幀率self.fps = 60self.clock = pygame.time.Clock()# 初始化角色MainGame.player1 = PlayerOne(pygame.time.get_ticks())# 設(shè)置角色的初始位置# 這里設(shè)置為(0,80),可以實現(xiàn)一開始玩家掉下來的動畫MainGame.player1.rect.x = 80MainGame.player1.rect.bottom = 300# 把角色放入組中,方便統(tǒng)一管理MainGame.allSprites = pygame.sprite.Group(MainGame.player1)def run(self):while not self.isEnd:# 設(shè)置背景顏色pygame.display.get_surface().fill((0, 0, 0))# 游戲場景和景物更新函數(shù)self.update(MainGame.window)# 獲取窗口中的事件self.getPlayingModeEvent()# 更新窗口pygame.display.update()# 設(shè)置幀率self.clock.tick(self.fps)fps = self.clock.get_fps()caption = '魂斗羅 - {:.2f}'.format(fps)pygame.display.set_caption(caption)else:sys.exit()def getPlayingModeEvent(self):# 獲取事件列表for event in pygame.event.get():# 點擊窗口關(guān)閉按鈕if event.type == pygame.QUIT:self.isEnd = True# 鍵盤按鍵按下elif event.type == pygame.KEYDOWN:self.keys = pygame.key.get_pressed()# 鍵盤按鍵抬起elif event.type == pygame.KEYUP:self.keys = pygame.key.get_pressed()def update(self, window):# 更新物體currentTime = pygame.time.get_ticks()MainGame.allSprites.update(self.keys, currentTime)# 顯示物體MainGame.allSprites.draw(window)if __name__ == '__main__':MainGame().run()

我們現(xiàn)在運行一下,看看效果

在這里插入圖片描述
我們發(fā)現(xiàn)角色處于跳躍的樣子,并且向前移動,這不符合我們的預(yù)期,預(yù)期應(yīng)該是站在那里

原因在于角色一開始創(chuàng)建時的狀態(tài)不對
在這里插入圖片描述
當(dāng)前沒有實現(xiàn)falling()函數(shù),所以下落狀態(tài)沒有辦法顯示,所以我們修改一下,修改角色類中__init__()函數(shù)

# 設(shè)置角色的狀態(tài)
self.state = State.FALL 改為 self.state = State.STAND

在這里插入圖片描述
改完后運行一下,看看結(jié)果

在這里插入圖片描述
按a和d鍵玩家會一直移動,不會停下來

在這里插入圖片描述
到此,我們就寫好了玩家站立函數(shù)了,下面我們來寫玩家移動函數(shù)

完整的玩家類__init__()函數(shù)代碼

def __init__(self, currentTime):pygame.sprite.Sprite.__init__(self)# 加載角色圖片self.standRightImage = loadImage('../Image/Player/Player1/Right/stand.png')self.standLeftImage = loadImage('../Image/Player/Player1/Left/stand.png')self.upRightImage = loadImage('../Image/Player/Player1/Up/upRight(small).png')self.upLeftImage = loadImage('../Image/Player/Player1/Up/upLeft(small).png')self.downRightImage = loadImage('../Image/Player/Player1/Down/down.png')self.downLeftImage = loadImage('../Image/Player/Player1/Down/down.png', True)self.obliqueUpRightImages = [loadImage('../Image/Player/Player1/Up/rightUp1.png'),loadImage('../Image/Player/Player1/Up/rightUp2.png'),loadImage('../Image/Player/Player1/Up/rightUp3.png'),]self.obliqueUpLeftImages = [loadImage('../Image/Player/Player1/Up/rightUp1.png', True),loadImage('../Image/Player/Player1/Up/rightUp2.png', True),loadImage('../Image/Player/Player1/Up/rightUp3.png', True),]self.obliqueDownRightImages = [loadImage('../Image/Player/Player1/ObliqueDown/1.png'),loadImage('../Image/Player/Player1/ObliqueDown/2.png'),loadImage('../Image/Player/Player1/ObliqueDown/3.png'),]self.obliqueDownLeftImages = [loadImage('../Image/Player/Player1/ObliqueDown/1.png', True),loadImage('../Image/Player/Player1/ObliqueDown/2.png', True),loadImage('../Image/Player/Player1/ObliqueDown/3.png', True),]# 角色向右的全部圖片self.rightImages = [loadImage('../Image/Player/Player1/Right/run1.png'),loadImage('../Image/Player/Player1/Right/run2.png'),loadImage('../Image/Player/Player1/Right/run3.png')]# 角色向左的全部圖片self.leftImages = [loadImage('../Image/Player/Player1/Left/run1.png'),loadImage('../Image/Player/Player1/Left/run2.png'),loadImage('../Image/Player/Player1/Left/run3.png')]# 角色跳躍的全部圖片self.upRightImages = [loadImage('../Image/Player/Player1/Jump/jump1.png'),loadImage('../Image/Player/Player1/Jump/jump2.png'),loadImage('../Image/Player/Player1/Jump/jump3.png'),loadImage('../Image/Player/Player1/Jump/jump4.png'),]self.upLeftImages = [loadImage('../Image/Player/Player1/Jump/jump1.png', True),loadImage('../Image/Player/Player1/Jump/jump2.png', True),loadImage('../Image/Player/Player1/Jump/jump3.png', True),loadImage('../Image/Player/Player1/Jump/jump4.png', True),]self.rightFireImages = [loadImage('../Image/Player/Player1/Right/fire1.png'),loadImage('../Image/Player/Player1/Right/fire2.png'),loadImage('../Image/Player/Player1/Right/fire3.png'),]self.leftFireImages = [loadImage('../Image/Player/Player1/Right/fire1.png', True),loadImage('../Image/Player/Player1/Right/fire2.png', True),loadImage('../Image/Player/Player1/Right/fire3.png', True),]# 角色左右移動下標self.imageIndex = 0# 角色跳躍下標self.upImageIndex = 0# 角色斜射下標self.obliqueImageIndex = 0# 上一次顯示圖片的時間self.runLastTimer = currentTimeself.fireLastTimer = currentTime# 選擇當(dāng)前要顯示的圖片self.image = self.standRightImage# 獲取圖片的rectself.rect = self.image.get_rect()# 設(shè)置角色的狀態(tài)self.state = State.STAND# 角色的方向self.direction = Direction.RIGHT# 速度self.xSpeed = PLAYER_X_SPEEDself.ySpeed = 0self.jumpSpeed = -11# 人物當(dāng)前的狀態(tài)標志self.isStanding = Falseself.isWalking = Falseself.isJumping = Trueself.isSquating = Falseself.isFiring = False# 重力加速度self.gravity = 0.7self.isUp = Falseself.isDown = False

2. 角色移動

打開注釋,開始編寫角色移動函數(shù)
在這里插入圖片描述
同樣的,先設(shè)置角色的狀態(tài),因為是角色移動,所以只有isWalking為真,其他都是假

self.isStanding = False
self.isWalking = True
self.isJumping = False
self.isSquating = False
self.isFiring = False

設(shè)置速度

self.ySpeed = 0
self.xSpeed = PLAYER_X_SPEED

判斷當(dāng)前狀態(tài),根據(jù)當(dāng)前狀態(tài)準備下一狀態(tài)的圖片

# 如果當(dāng)前是站立的圖片
if self.isStanding:# 方向向右,方向向上if self.direction == Direction.RIGHT and self.isUp:# 設(shè)置為向右朝上的圖片self.image = self.upRightImage# 方向向右elif self.direction == Direction.RIGHT and not self.isUp:# 設(shè)置為向右站立的圖片self.image = self.standRightImageelif self.direction == Direction.LEFT and self.isUp:self.image = self.upLeftImageelif self.direction == Direction.LEFT and not self.isUp:self.image = self.standLeftImage# 記下當(dāng)前時間self.runLastTimer = currentTime
else:# 如果是走動的圖片,先判斷方向if self.direction == Direction.RIGHT:# 設(shè)置速度self.xSpeed = PLAYER_X_SPEED# 根據(jù)上下方向覺得是否角色要加載斜射的圖片if self.isUp or self.isDown:# isUp == True表示向上斜射# isDown == True表示向下斜射# 計算上一次加載圖片到這次的時間,如果大于115,即11.5幀,即上次加載圖片到這次加載圖片之間,已經(jīng)加載了11張圖片if currentTime - self.runLastTimer > 115:# 那么就可以加載斜著奔跑的圖片# 如果角色加載的圖片不是第三張,則加載下一張就行if self.obliqueImageIndex < 2:self.obliqueImageIndex += 1# 否則就加載第一張圖片else:self.obliqueImageIndex = 0# 記錄變換圖片的時間,為下次變換圖片做準備self.runLastTimer = currentTime# 不是斜射else:# 加載正常向右奔跑的圖片if currentTime - self.runLastTimer > 115:if self.imageIndex < 2:self.imageIndex += 1else:self.imageIndex = 0self.runLastTimer = currentTimeelse:self.xSpeed = -PLAYER_X_SPEEDif self.isUp or self.isDown:if currentTime - self.runLastTimer > 115:if self.obliqueImageIndex < 2:self.obliqueImageIndex += 1else:self.obliqueImageIndex = 0self.runLastTimer = currentTimeelse:if currentTime - self.runLastTimer > 115:if self.imageIndex < 2:self.imageIndex += 1else:self.imageIndex = 0self.runLastTimer = currentTime

完成圖片加載后,處理按鍵響應(yīng)

# 按下D鍵
if keys[pygame.K_d]:self.direction = Direction.RIGHTself.xSpeed = PLAYER_X_SPEED
# 按下A鍵
elif keys[pygame.K_a]:self.direction = Direction.LEFTself.xSpeed = -PLAYER_X_SPEED# 按下S鍵
elif keys[pygame.K_s]:self.isStanding = Falseself.isDown = True# 按下W鍵
if keys[pygame.K_w]:self.isUp = Trueself.isDown = False
# 沒有按鍵按下
else:self.state = State.STAND# 移動時按下K鍵
if keys[pygame.K_k]:# 角色狀態(tài)變?yōu)樘Sself.state = State.JUMPself.ySpeed = self.jumpSpeedself.isJumping = Trueself.isStanding = False

完整的walking()函數(shù)代碼

    def walking(self, keys, currentTime):"""角色行走,每10幀變換一次圖片"""self.isStanding = Falseself.isWalking = Trueself.isJumping = Falseself.isSquating = Falseself.isFiring = Falseself.ySpeed = 0self.xSpeed = PLAYER_X_SPEED# 如果當(dāng)前是站立的圖片if self.isStanding:# 方向向右,方向向上if self.direction == Direction.RIGHT and self.isUp:# 設(shè)置為向右朝上的圖片self.image = self.upRightImage# 方向向右elif self.direction == Direction.RIGHT and not self.isUp:# 設(shè)置為向右站立的圖片self.image = self.standRightImageelif self.direction == Direction.LEFT and self.isUp:self.image = self.upLeftImageelif self.direction == Direction.LEFT and not self.isUp:self.image = self.standLeftImage# 記下當(dāng)前時間self.runLastTimer = currentTimeelse:# 如果是走動的圖片,先判斷方向if self.direction == Direction.RIGHT:# 設(shè)置速度self.xSpeed = PLAYER_X_SPEED# 根據(jù)上下方向覺得是否角色要加載斜射的圖片if self.isUp or self.isDown:# isUp == True表示向上斜射# isDown == True表示向下斜射# 計算上一次加載圖片到這次的時間,如果大于115,即11.5幀,即上次加載圖片到這次加載圖片之間,已經(jīng)加載了11張圖片if currentTime - self.runLastTimer > 115:# 那么就可以加載斜著奔跑的圖片# 如果角色加載的圖片不是第三張,則加載下一張就行if self.obliqueImageIndex < 2:self.obliqueImageIndex += 1# 否則就加載第一張圖片else:self.obliqueImageIndex = 0# 記錄變換圖片的時間,為下次變換圖片做準備self.runLastTimer = currentTime# 不是斜射else:# 加載正常向右奔跑的圖片if currentTime - self.runLastTimer > 115:if self.imageIndex < 2:self.imageIndex += 1else:self.imageIndex = 0self.runLastTimer = currentTimeelse:self.xSpeed = -PLAYER_X_SPEEDif self.isUp or self.isDown:if currentTime - self.runLastTimer > 115:if self.obliqueImageIndex < 2:self.obliqueImageIndex += 1else:self.obliqueImageIndex = 0self.runLastTimer = currentTimeelse:if currentTime - self.runLastTimer > 115:if self.imageIndex < 2:self.imageIndex += 1else:self.imageIndex = 0self.runLastTimer = currentTime# 按下D鍵if keys[pygame.K_d]:self.direction = Direction.RIGHTself.xSpeed = PLAYER_X_SPEED# 按下A鍵elif keys[pygame.K_a]:self.direction = Direction.LEFTself.xSpeed = -PLAYER_X_SPEED# 按下S鍵elif keys[pygame.K_s]:self.isStanding = Falseself.isDown = True# 按下W鍵if keys[pygame.K_w]:self.isUp = Trueself.isDown = False# 沒有按鍵按下else:self.state = State.STAND# 移動時按下K鍵if keys[pygame.K_k]:# 角色狀態(tài)變?yōu)樘Sself.state = State.JUMPself.ySpeed = self.jumpSpeedself.isJumping = Trueself.isStanding = False

運行一下,看看效果

在這里插入圖片描述
哇,實現(xiàn)了,我們就完成了角色移動了

3. 角色跳躍

首先設(shè)置狀態(tài)標志

# 設(shè)置標志
self.isJumping = True
self.isStanding = False
self.isDown = False
self.isSquating = False
self.isFiring = False

更新速度

self.ySpeed += self.gravity

跳躍后,角色的y坐標是減少的,所以速度原來是負數(shù),由于跳起到最高點速度一直減小,所以這里我們設(shè)置了重力,保證速度慢慢減少到0

根據(jù)時間決定是否顯示下一張圖片

if currentTime - self.runLastTimer > 115:if self.upImageIndex < 3:self.upImageIndex += 1else:self.upImageIndex = 0# 記錄變換圖片的時間,為下次變換圖片做準備self.runLastTimer = currentTime

設(shè)置按鍵響應(yīng)事件

if keys[pygame.K_d]:self.direction = Direction.RIGHTelif keys[pygame.K_a]:self.direction = Direction.LEFT# 按下W鍵
if keys[pygame.K_w]:self.isUp = Trueself.isDown = False
elif keys[pygame.K_s]:self.isUp = Falseself.isDown = True# 當(dāng)速度變?yōu)檎龜?shù),玩家進入下落狀態(tài)
if self.ySpeed >= 0:self.state = State.FALLif not keys[pygame.K_k]:self.state = State.FALL

完整jumping()函數(shù)代碼

def jumping(self, keys, currentTime):"""跳躍"""# 設(shè)置標志self.isJumping = Trueself.isStanding = Falseself.isDown = Falseself.isSquating = Falseself.isFiring = False# 更新速度self.ySpeed += self.gravityif currentTime - self.runLastTimer > 115:if self.upImageIndex < 3:self.upImageIndex += 1else:self.upImageIndex = 0# 記錄變換圖片的時間,為下次變換圖片做準備self.runLastTimer = currentTimeif keys[pygame.K_d]:self.direction = Direction.RIGHTelif keys[pygame.K_a]:self.direction = Direction.LEFT# 按下W鍵if keys[pygame.K_w]:self.isUp = Trueself.isDown = Falseelif keys[pygame.K_s]:self.isUp = Falseself.isDown = Trueif self.ySpeed >= 0:self.state = State.FALLif not keys[pygame.K_k]:self.state = State.FALL

4. 角色下落

def falling(self, keys, currentTime):# 下落時速度越來越快,所以速度需要一直增加self.ySpeed += self.gravityif currentTime - self.runLastTimer > 115:if self.upImageIndex < 3:self.upImageIndex += 1else:self.upImageIndex = 0self.runLastTimer = currentTime# 防止落到窗口外面,當(dāng)落到一定高度時,就不會再掉落了if self.rect.bottom > SCREEN_HEIGHT - GROUND_HEIGHT:self.state = State.WALKself.ySpeed = 0self.rect.bottom = SCREEN_HEIGHT - GROUND_HEIGHTself.isJumping = Falseif keys[pygame.K_d]:self.direction = Direction.RIGHTself.isWalking = Falseelif keys[pygame.K_a]:self.direction = Direction.LEFTself.isWalking = False

我們現(xiàn)在來看看效果,運行一下

不要忘記打開注釋

在這里插入圖片描述

在這里插入圖片描述
到現(xiàn)在,我們實現(xiàn)了角色移動、跳躍啦,下面就是發(fā)射子彈了

http://www.risenshineclean.com/news/55677.html

相關(guān)文章:

  • php響應(yīng)式個人博客網(wǎng)站設(shè)計網(wǎng)站建設(shè)制作流程
  • 重慶企業(yè)網(wǎng)站建站app推廣拉新一手渠道代理
  • 做網(wǎng)站能收多少廣告費軟件推廣賺錢一個10元
  • 網(wǎng)站建設(shè)項目seo網(wǎng)絡(luò)推廣企業(yè)
  • 教育網(wǎng)站建設(shè)的意義推廣渠道有哪些
  • 用eclipse編程做網(wǎng)站網(wǎng)絡(luò)營銷的基本方法
  • 人民日報新聞評論寧波seo關(guān)鍵詞優(yōu)化
  • 188旅游網(wǎng)站管理系統(tǒng)百度下載并安裝最新版
  • 蛋糕店網(wǎng)站建設(shè)深圳百度推廣關(guān)鍵詞推廣
  • 合肥網(wǎng)站改版做銷售怎樣去尋找客戶
  • layui 企業(yè)網(wǎng)站模板全網(wǎng)營銷思路
  • 用阿里云服務(wù)器做盜版小說網(wǎng)站嗎如何seo推廣
  • 黨校網(wǎng)站信息化建設(shè)整改情況百度自動點擊器
  • 建站最便宜的平臺推廣注冊app拿傭金
  • 給設(shè)計網(wǎng)站做圖是商用嗎已矣seo排名點擊軟件
  • 科普類網(wǎng)站怎么做全球網(wǎng)站流量排名查詢
  • 開發(fā)網(wǎng)站的可行性seo收費還是免費
  • 制作一個網(wǎng)站數(shù)據(jù)庫怎么做的seo搜索引擎排名優(yōu)化
  • 自己的服務(wù)器如何給網(wǎng)站備案seo好找工作嗎
  • 平面設(shè)計是干什么的工資一般多少洛陽seo網(wǎng)絡(luò)推廣
  • 龍游縣建設(shè)局網(wǎng)站手機制作網(wǎng)頁用什么軟件
  • h5網(wǎng)站制作接單網(wǎng)絡(luò)營銷核心要素
  • 商丘做網(wǎng)站百度app廣告
  • 網(wǎng)站后臺制作表格seo站內(nèi)優(yōu)化技巧
  • 可以賺錢做任務(wù)的網(wǎng)站有哪些東莞做網(wǎng)站的聯(lián)系電話
  • 做曖暖愛視頻每一刻網(wǎng)站優(yōu)化軟件下載
  • 先做網(wǎng)站 先備案網(wǎng)頁設(shè)計代碼大全
  • wordpress禁用右鍵鄭州seo服務(wù)
  • 企業(yè)網(wǎng)站功能清單搜索率最高的關(guān)鍵詞
  • 馬鞍山網(wǎng)站建設(shè)設(shè)計考研培訓(xùn)機構(gòu)排名