一凑阶、引言
前面的游戲中紅色方塊一旦出界一次猿规,我們就認(rèn)為游戲結(jié)束。為使游戲更好玩宙橱,我們?cè)黾由倒δ芤塘總€(gè)玩家每句游戲有三個(gè)生命值,每次紅色方塊出界一次师郑,就扣一個(gè)生命值环葵,當(dāng)生命值為零時(shí),游戲失敗宝冕。
二张遭、實(shí)現(xiàn)思路
這個(gè)功能相對(duì)比較簡(jiǎn)單。當(dāng)游戲中有某項(xiàng)數(shù)值會(huì)變化地梨,我們就要想到用變量來(lái)存儲(chǔ)菊卷,這是編程的基本思路缔恳。很明顯生命值就是會(huì)變化的數(shù)字,我們用life_times來(lái)保存洁闰,初始值為3歉甚,每次紅色方塊出界減1,當(dāng)life_times為0時(shí)扑眉,游戲結(jié)束纸泄。
實(shí)現(xiàn)代碼為:
import pygame, sys
import random
# 初始化
pygame.init()
SCREEN = pygame.display.set_mode((400, 300))
pygame.display.set_caption('Hello World!')
# 綠色方塊固定在最下方,左右移動(dòng)襟雷,y值不變
green_x = 110
# 紅色方塊從上往下移動(dòng)刃滓,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()
# 鍵盤(pán)按下事件
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('紅色方塊與綠色方塊碰撞到了')
# 為了方便看到碰撞結(jié)果仁烹,直接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() 方法更新整個(gè)屏幕的顯示
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)
上述代碼實(shí)現(xiàn)生命值功能耸弄,為了方便用戶看到生命值的變化,我們?cè)谟疑辖秋@示生命值功能卓缰,這部分的功能可以參考
用pygame開(kāi)發(fā)自己的游戲-5.增加分?jǐn)?shù)功能來(lái)實(shí)現(xiàn)计呈。
實(shí)現(xiàn)代碼為:
import pygame, sys
import random
# 初始化
pygame.init()
SCREEN = pygame.display.set_mode((400, 300))
pygame.display.set_caption('Hello World!')
# 綠色方塊固定在最下方,左右移動(dòng)征唬,y值不變
green_x = 110
# 紅色方塊從上往下移動(dòng)捌显,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()
# 鍵盤(pán)按下事件
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('紅色方塊與綠色方塊碰撞到了')
# 為了方便看到碰撞結(jié)果,直接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() 方法更新整個(gè)屏幕的顯示
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)
三总寒、思考題
目前游戲的結(jié)束時(shí)扶歪,紅色方塊會(huì)繼續(xù)生成下降,life_times甚至?xí)蔀樨?fù)數(shù)摄闸,這明顯有問(wèn)題善镰,請(qǐng)思考下如何解決這個(gè)問(wèn)題。
提示:哪里是游戲結(jié)束的相關(guān)代碼年枕,哪里是重新生成紅色方塊的代碼炫欺。