Python+Pygame實(shí)戰(zhàn)之英文版猜字游戲的實(shí)現(xiàn)
導(dǎo)語
當(dāng)下的孩子們多少會被電子產(chǎn)品“侵襲”,那么既然都要玩游戲,為什么不選既能玩又能收獲知識的呢?

興趣是最大的學(xué)習(xí)推動力,當(dāng)學(xué)習(xí)英語變成一款小游戲時,不僅能夠玩游戲,激發(fā)調(diào)動孩子的積極性,還能夠讓孩子們在娛樂中潛移默化地掌握英語的學(xué)習(xí)技巧哦~
今天木子為大家敲了一款簡單的英語版《神奇的猜字小游戲》,分享給大家,希望能幫到大家。如果您是一位熱心于輔導(dǎo)孩子學(xué)習(xí)英語的家長,上面這款游戲也不妨用一用,試一試哦~

(文中代碼僅供娛樂,真要輔導(dǎo)孩子邊玩兒游戲邊學(xué)習(xí)英語,請大家找找這方面的app啦隨便一款都很可的。噗又是自我調(diào)節(jié)的一天,真實(shí)的自我認(rèn)知,哈哈哈.jpg)

一、運(yùn)行環(huán)境
小編使用的環(huán)境:Python3、Pycharm社區(qū)版、Pygame模塊部分自帶就不一一 展示啦。
模塊安裝:pip install -i https://pypi.douban.com/simple/ +pygame
二、素材(圖片等)
圖片的話可以自己準(zhǔn)備,但是要注意的大小尺寸問題還有圖片是.png模式的哈。

單詞的話這個要自己準(zhǔn)備,我這里就準(zhǔn)備了幾個單詞的,大家可以從最簡單的開始哈。

三、代碼展示
主程序:
# File Name:神奇的猜數(shù)字游戲.py
import pygame
import random
pygame.init()
winHeight = 480
winWidth = 700
win=pygame.display.set_mode((winWidth,winHeight))
BLACK = (0,0, 0)
WHITE = (255,255,255)
RED = (255,0, 0)
GREEN = (0,255,0)
BLUE = (0,0,255)
LIGHT_BLUE = (102,255,255)
btn_font = pygame.font.SysFont("arial", 20)
guess_font = pygame.font.SysFont("monospace", 24)
lost_font = pygame.font.SysFont('arial', 45)
word = ''
buttons = []
guessed = []
hangmanPics = [pygame.image.load('hangman0.png'), pygame.image.load('hangman1.png'), pygame.image.load('hangman2.png'), pygame.image.load('hangman3.png'), pygame.image.load('hangman4.png'), pygame.image.load('hangman5.png'), pygame.image.load('hangman6.png')]
limbs = 0
def redraw_game_window():
global guessed
global hangmanPics
global limbs
win.fill(GREEN)
# Buttons
for i in range(len(buttons)):
if buttons[i][4]:
pygame.draw.circle(win, BLACK, (buttons[i][1], buttons[i][2]), buttons[i][3])
pygame.draw.circle(win, buttons[i][0], (buttons[i][1], buttons[i][2]), buttons[i][3] - 2
)
label = btn_font.render(chr(buttons[i][5]), 1, BLACK)
win.blit(label, (buttons[i][1] - (label.get_width() / 2), buttons[i][2] - (label.get_height() / 2)))
spaced = spacedOut(word, guessed)
label1 = guess_font.render(spaced, 1, BLACK)
rect = label1.get_rect()
length = rect[2]
win.blit(label1,(winWidth/2 - length/2, 400))
pic = hangmanPics[limbs]
win.blit(pic, (winWidth/2 - pic.get_width()/2 + 20, 150))
pygame.display.update()
def randomWord():
file = open('words.txt')
f = file.readlines()
i = random.randrange(0, len(f) - 1)
return f[i][:-1]
def hang(guess):
global word
if guess.lower() not in word.lower():
return True
else:
return False
def spacedOut(word, guessed=[]):
spacedWord = ''
guessedLetters = guessed
for x in range(len(word)):
if word[x] != ' ':
spacedWord += '_ '
for i in range(len(guessedLetters)):
if word[x].upper() == guessedLetters[i]:
spacedWord = spacedWord[:-2]
spacedWord += word[x].upper() + ' '
elif word[x] == ' ':
spacedWord += ' '
return spacedWord
def buttonHit(x, y):
for i in range(len(buttons)):
if x < buttons[i][1] + 20 and x > buttons[i][1] - 20:
if y < buttons[i][2] + 20 and y > buttons[i][2] - 20:
return buttons[i][5]
return None
def end(winner=False):
global limbs
lostTxt = 'You Lost, press any key to play again...'
winTxt = 'WINNER!, press any key to play again...'
redraw_game_window()
pygame.time.delay(1000)
win.fill(GREEN)
if winner == True:
label = lost_font.render(winTxt, 1, BLACK)
else:
label = lost_font.render(lostTxt, 1, BLACK)
wordTxt = lost_font.render(word.upper(), 1, BLACK)
wordWas = lost_font.render('The phrase was: ', 1, BLACK)
win.blit(wordTxt, (winWidth/2 - wordTxt.get_width()/2, 295))
win.blit(wordWas, (winWidth/2 - wordWas.get_width()/2, 245))
win.blit(label, (winWidth / 2 - label.get_width() / 2, 140))
pygame.display.update()
again = True
while again:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.KEYDOWN:
again = False
reset()
def reset():
global limbs
global guessed
global buttons
global word
for i in range(len(buttons)):
buttons[i][4] = True
limbs = 0
guessed = []
word = randomWord()
#MAINLINE
# Setup buttons
increase = round(winWidth / 13)
for i in range(26):
if i < 13:
y = 40
x = 25 + (increase * i)
else:
x = 25 + (increase * (i - 13))
y = 85
buttons.append([LIGHT_BLUE, x, y, 20, True, 65 + i])
# buttons.append([color, x_pos, y_pos, radius, visible, char])
word = randomWord()
inPlay = True
while inPlay:
redraw_game_window()
pygame.time.delay(10)
for event in pygame.event.get():
if event.type == pygame.QUIT:
inPlay = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
inPlay = False
if event.type == pygame.MOUSEBUTTONDOWN:
clickPos = pygame.mouse.get_pos()
letter = buttonHit(clickPos[0], clickPos[1])
if letter != None:
guessed.append(chr(letter))
buttons[letter - 65][4] = False
if hang(chr(letter)):
if limbs != 5:
limbs += 1
else:
end()
else:
print(spacedOut(word, guessed))
if spacedOut(word, guessed).count('_') == 0:
end(True)
pygame.quit()
# always quit pygame when done!四、效果展示
(其實(shí)學(xué)習(xí)編程也要敲代碼的啦,有點(diǎn)兒英語基礎(chǔ)的話更好學(xué)習(xí)的啦~)
1)游戲界面

2)游戲開始

3)游戲失敗次數(shù)
失敗每一次會出現(xiàn)一部分,然后拼出來一個人,五次全部拼錯,就掛了。正常的通關(guān)是不會出現(xiàn)這個小人的。

以上就是Python+Pygame實(shí)戰(zhàn)之英文版猜字游戲的實(shí)現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于Python Pygame猜字游戲的資料請關(guān)注腳本之家其它相關(guān)文章!
- Pygame改編飛機(jī)大戰(zhàn)制作兔子接月餅游戲
- Python+Pygame實(shí)現(xiàn)海洋之神大冒險游戲
- Python pygame 動畫游戲循環(huán)游戲時鐘實(shí)現(xiàn)原理
- Python+Pygame實(shí)戰(zhàn)之實(shí)現(xiàn)小蜜蜂歷險記游戲
- Pygame游戲開發(fā)之太空射擊實(shí)戰(zhàn)盾牌篇
- Pygame游戲開發(fā)之太空射擊實(shí)戰(zhàn)碰撞改進(jìn)篇
- Pygame游戲開發(fā)之太空射擊實(shí)戰(zhàn)添加圖形篇
- Pygame游戲開發(fā)之太空射擊實(shí)戰(zhàn)子彈與碰撞處理篇
- pygame實(shí)現(xiàn)一個類似滿天星游戲流程詳解
相關(guān)文章
vscode 配置 python3開發(fā)環(huán)境的方法
這篇文章主要介紹了vscode 配置 python3開發(fā)環(huán)境的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
對TensorFlow中的variables_to_restore函數(shù)詳解
今天小編就為大家分享一篇對TensorFlow中的variables_to_restore函數(shù)詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07
使用pandas實(shí)現(xiàn)篩選出指定列值所對應(yīng)的行
這篇文章主要介紹了使用pandas實(shí)現(xiàn)篩選出指定列值所對應(yīng)的行,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
Django ORM 聚合查詢和分組查詢實(shí)現(xiàn)詳解
這篇文章主要介紹了Django ORM 聚合查詢和分組查詢實(shí)現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-08-08
python 實(shí)現(xiàn)倒計時功能(gui界面)
這篇文章主要介紹了python 實(shí)現(xiàn)倒計時功能(gui界面),幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-11-11
Python matplotlib實(shí)現(xiàn)散點(diǎn)圖的繪制
Matplotlib作為Python的2D繪圖庫,它以各種硬拷貝格式和跨平臺的交互式環(huán)境生成出版質(zhì)量級別的圖形。本文將利用Matplotlib庫繪制散點(diǎn)圖,感興趣的可以了解一下2022-03-03
python列表推導(dǎo)式實(shí)現(xiàn)找出列表中長度大于5的名字
這篇文章主要介紹了python列表推導(dǎo)式實(shí)現(xiàn)找出列表中長度大于5的名字,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02

