用pygame開發(fā)自己的游戲-8.增加生命值功能

一、引言

前面的游戲中紅色方塊一旦出界一次,我們就認為游戲結束。為使游戲更好玩,我們增加生命值功能,每個玩家每句游戲有三個生命值,每次紅色方塊出界一次,就扣一個生命值,當生命值為零時,游戲失敗。

二、實現(xiàn)思路

這個功能相對比較簡單。當游戲中有某項數(shù)值會變化,我們就要想到用變量來存儲,這是編程的基本思路。很明顯生命值就是會變化的數(shù)字,我們用life_times來保存,初始值為3,每次紅色方塊出界減1,當life_times為0時,游戲結束。
實現(xiàn)代碼為:

import pygame, sys
import random
# 初始化
pygame.init()
SCREEN = pygame.display.set_mode((400, 300))
pygame.display.set_caption('Hello World!')

# 綠色方塊固定在最下方,左右移動,y值不變
green_x = 110
# 紅色方塊從上往下移動,x值不變
red_y = 0
# 游戲主循環(huán)
score = 0
pygame.font.init()
myfont = pygame.font.Font(None,60)
red_x = 35
life_times, is_over = 3, False
while True: 
    for event in pygame.event.get():
        # 處理退出事件
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        # 鍵盤按下事件
        elif event.type == pygame.KEYDOWN:
            # 'a'鍵被按下
            if event.key == pygame.K_a:
                green_x -= 5
            elif event.key == pygame.K_d:
                green_x += 5
    red_y += 5
    green_rect = pygame.Rect(green_x, 250, 100, 50)
    if green_rect.colliderect(red_x, red_y, 20, 50):
        print('紅色方塊與綠色方塊碰撞到了')
        # 為了方便看到碰撞結果,直接break返回
        score += 1
        red_y = 0
        red_x = random.randint(50, 350)
    if red_y >= 300:
        life_times -= 1
        if life_times <= 0:
            is_over = True
        red_y = 0
        red_x = random.randint(50, 350)
        
    SCREEN.fill((255, 255, 255))
    # 調(diào)用 pygame.display.update() 方法更新整個屏幕的顯示
    pygame.draw.rect(SCREEN, (255, 0, 0), (red_x, red_y, 20, 50))
    pygame.draw.rect(SCREEN, (0, 255, 0), (green_x, 250, 100, 50))
    textImage = myfont.render("score: " + str(score), True, (0, 0, 255))
    SCREEN.blit(textImage, (10,10))
    if is_over:
        gameOverTextImage = myfont.render('GAME OVER!', True, (255, 0, 0))
        SCREEN.blit(gameOverTextImage, (80,150))
    pygame.display.update()
    pygame.time.delay(50)

上述代碼實現(xiàn)生命值功能,為了方便用戶看到生命值的變化,我們在右上角顯示生命值功能,這部分的功能可以參考
用pygame開發(fā)自己的游戲-5.增加分數(shù)功能來實現(xiàn)。
實現(xiàn)代碼為:

import pygame, sys
import random
# 初始化
pygame.init()
SCREEN = pygame.display.set_mode((400, 300))
pygame.display.set_caption('Hello World!')

# 綠色方塊固定在最下方,左右移動,y值不變
green_x = 110
# 紅色方塊從上往下移動,x值不變
red_y = 0
# 游戲主循環(huán)
score = 0
pygame.font.init()
myfont = pygame.font.Font(None,60)
red_x = 35
life_times, is_over = 3, False
while True: 
    for event in pygame.event.get():
        # 處理退出事件
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        # 鍵盤按下事件
        elif event.type == pygame.KEYDOWN:
            # 'a'鍵被按下
            if event.key == pygame.K_a:
                green_x -= 5
            elif event.key == pygame.K_d:
                green_x += 5
    red_y += 5
    green_rect = pygame.Rect(green_x, 250, 100, 50)
    if green_rect.colliderect(red_x, red_y, 20, 50):
        print('紅色方塊與綠色方塊碰撞到了')
        # 為了方便看到碰撞結果,直接break返回
        score += 1
        red_y = 0
        red_x = random.randint(50, 350)
    if red_y >= 300:
        life_times -= 1
        if life_times <= 0:
            is_over = True
        red_y = 0
        red_x = random.randint(50, 350)
        
    SCREEN.fill((255, 255, 255))
    # 調(diào)用 pygame.display.update() 方法更新整個屏幕的顯示
    pygame.draw.rect(SCREEN, (255, 0, 0), (red_x, red_y, 20, 50))
    pygame.draw.rect(SCREEN, (0, 255, 0), (green_x, 250, 100, 50))
    textImage = myfont.render("score: " + str(score), True, (0, 0, 255))
    SCREEN.blit(textImage, (10,10))
    lifeTextImage = myfont.render("life: " + str(life_times), True, (0, 255, 0))
    SCREEN.blit(lifeTextImage, (250,10))
    if is_over:
        gameOverTextImage = myfont.render('GAME OVER!', True, (255, 0, 0))
        SCREEN.blit(gameOverTextImage, (80,150))
    pygame.display.update()
    pygame.time.delay(50)

三、思考題

目前游戲的結束時,紅色方塊會繼續(xù)生成下降,life_times甚至會成為負數(shù),這明顯有問題,請思考下如何解決這個問題。

提示:哪里是游戲結束的相關代碼,哪里是重新生成紅色方塊的代碼。

?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容