Python+Pygame实战之英文版猜字游戏的实现

作者:顾木子吖 时间:2021-01-18 01:40:32 

导语

当下的孩子们多少会被电子产品“侵袭”,那么既然都要玩游戏,为什么不选既能玩又能收获知识的呢?

Python+Pygame实战之英文版猜字游戏的实现

兴趣是最大的学习推动力,当学习英语变成一款小游戏时,不仅能够玩游戏,激发调动孩子的积极性,还能够让孩子们在娱乐中潜移默化地掌握英语的学习技巧哦~

今天木子为大家敲了一款简单的英语版《神奇的猜字小游戏》,分享给大家,希望能帮到大家。如果您是一位热心于辅导孩子学习英语的家长,上面这款游戏也不妨用一用,试一试哦~

Python+Pygame实战之英文版猜字游戏的实现

(文中代码仅供娱乐,真要辅导孩子边玩儿游戏边学习英语,请大家找找这方面的app啦随便一款都很可的。噗又是自我调节的一天,真实的自我认知,哈哈哈.jpg)

Python+Pygame实战之英文版猜字游戏的实现

一、运行环境

小编使用的环境:Python3、Pycharm社区版、Pygame模块部分自带就不一一 展示啦。

模块安装:pip install -i https://pypi.douban.com/simple/ +pygame

二、素材(图片等)

图片的话可以自己准备,但是要注意的大小尺寸问题还有图片是.png模式的哈。

Python+Pygame实战之英文版猜字游戏的实现

单词的话这个要自己准备,我这里就准备了几个单词的,大家可以从最简单的开始哈。

Python+Pygame实战之英文版猜字游戏的实现

三、代码展示

主程序:

# File Name:神奇的猜数字游戏.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!

四、效果展示

(其实学习编程也要敲代码的啦,有点儿英语基础的话更好学习的啦~)

1)游戏界面

Python+Pygame实战之英文版猜字游戏的实现

2)游戏开始

Python+Pygame实战之英文版猜字游戏的实现

3)游戏失败次数

失败每一次会出现一部分,然后拼出来一个人,五次全部拼错,就挂了。正常的通关是不会出现这个小人的。

Python+Pygame实战之英文版猜字游戏的实现

来源:https://blog.csdn.net/weixin_55822277/article/details/126276450

标签:Python,Pygame,猜字,游戏
0
投稿

猜你喜欢

  • XML 在使用中产生的二十个热点问题

    2008-05-29 11:07:00
  • favicon.ico以及动态图片的实现

    2008-07-03 12:34:00
  • 瀑布流布局浅析

    2011-09-16 20:18:09
  • Python日期的加减等操作的示例

    2021-10-06 14:28:14
  • 按钮的反馈

    2009-01-01 20:06:00
  • pandas提升计算效率的一些方法汇总

    2023-12-01 00:08:04
  • 用Python删除本地目录下某一时间点之前创建的所有文件的实例

    2021-07-29 05:34:59
  • 各种鼠标经过图片边框加粗效果整理

    2007-09-29 21:43:00
  • Python实现的NN神经网络算法完整示例

    2023-10-06 04:30:41
  • django做form表单的数据验证过程详解

    2023-11-10 12:26:13
  • python2爬取百度贴吧指定关键字和图片代码实例

    2022-10-12 09:03:44
  • Django 使用easy_thumbnails压缩上传的图片方法

    2023-11-10 07:47:38
  • 详解Python爬虫爬取博客园问题列表所有的问题

    2021-02-15 02:01:46
  • python configparser中默认值的设定方式

    2023-09-08 22:01:33
  • django缓存配置的几种方法详解

    2022-08-29 02:37:16
  • 如何让利用Python+AI使静态图片动起来

    2022-06-06 08:15:31
  • Flask项目的部署的实现步骤

    2023-08-11 17:59:58
  • 分面搜索(Faceted Search)

    2009-07-31 12:44:00
  • pytorch中retain_graph==True的作用说明

    2021-08-03 09:15:26
  • ASP防止图片木马上传的代码

    2011-02-05 11:08:00
  • asp之家 网络编程 m.aspxhome.com