欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Pygame實(shí)戰(zhàn)之檢測(cè)按鍵正確的小游戲

 更新時(shí)間:2021年12月15日 08:55:11   作者:我的天才女友  
這篇文章主要為大家介紹了利用Pygame模塊實(shí)現(xiàn)的檢測(cè)按鍵正確的小游戲:每個(gè)字母有10秒的按鍵時(shí)間,如果按對(duì),則隨機(jī)產(chǎn)生新的字符,一共60s,如果時(shí)間到了,則游戲結(jié)束??靵?lái)跟隨小編一起學(xué)習(xí)一下吧

游戲功能

游戲開(kāi)始,屏幕隨機(jī)顯示一個(gè)字符,按 Enter 游戲開(kāi)始,每個(gè)字母有10秒的按鍵時(shí)間,如果按對(duì),則隨機(jī)產(chǎn)生新的字符,一共60s,如果時(shí)間到了,則游戲結(jié)束。

引入包,初始化配置信息

import sys, random, time, pygame
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption("打字速度")

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    screen.fill((255, 192, 128))

    pygame.display.update()

初始化游戲提示信息

首先設(shè)置兩種字體,然后封裝一個(gè)屏幕上寫(xiě)字的函數(shù),寫(xiě)出提示。

white = 255, 255, 255

font1 = pygame.font.SysFont("方正粗黑宋簡(jiǎn)體", 24)
font2 = pygame.font.SysFont("方正粗黑宋簡(jiǎn)體", 200)

def print_text(font, x, y, text, color=white):
    img_text = font.render(text, True, color)
    screen.blit(img_text, (x, y))
while True:
	---
	print_text(font1, 0, 0, "看看你的速度有多快")
	print_text(font1, 0, 30, "請(qǐng)?jiān)?0秒內(nèi)嘗試")
	---

顯示隨機(jī)的字母

使用 ASCII 字符表,鍵盤(pán)上默認(rèn)輸入的是小寫(xiě),97 - 122,然后我們使用chr() 函數(shù)減去32,就可以得到對(duì)應(yīng)的大寫(xiě)字母,將其寫(xiě)在窗口上

# 隨機(jī)的字母
correct_answer = random.randint(97, 122)
while True:
	---
	print_text(font2, 0, 240, chr(correct_answer - 32), (255, 255, 0))
	---

設(shè)置游戲的屬性

# 是否按鍵
key_flag = False
# 游戲是否開(kāi)始 默認(rèn)是結(jié)束的
game_over = True
# 隨機(jī)的字母
correct_answer = random.randint(97, 122)
# 分?jǐn)?shù)
score = 0
# 開(kāi)始時(shí)間
clock_start = 0
# 讀秒倒計(jì)時(shí)
seconds = 11

根據(jù)用戶的按鍵改變對(duì)應(yīng)的屬性,如果游戲重新開(kāi)始,重置對(duì)應(yīng)的屬性。

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == KEYDOWN:
            key_flag = True
        elif event.type == KEYUP:
            key_flag = False
    keys = pygame.key.get_pressed()
    if keys[K_ESCAPE]:
        sys.exit()
    if keys[K_RETURN]:
        if game_over:
            game_over = False
            score = 0
            clock_start = time.perf_counter()
            seconds = 11

使用 time.perf_counter() 獲取程序運(yùn)行到當(dāng)前的時(shí)間,計(jì)算差值,實(shí)現(xiàn)在屏幕上的倒計(jì)時(shí),并根據(jù)時(shí)間結(jié)束游戲或者重新開(kāi)始

	current = time.perf_counter() - clock_start

    if seconds - current < 0:
        game_over = True
    elif current <= 10:
        if keys[correct_answer]:
            correct_answer = random.randint(97, 122)
            score += 1
            clock_start = time.perf_counter()
    if not game_over:
        print_text(font1, 0, 80, "Time: " + str(int(seconds - current)))
        print_text(font1, 500, 40,  str(int(time.perf_counter())))

完整代碼?

import sys, random, time, pygame
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption("打字速度")
font1 = pygame.font.SysFont("方正粗黑宋簡(jiǎn)體", 24)
font2 = pygame.font.SysFont("方正粗黑宋簡(jiǎn)體", 200)
white = 255, 255, 255
yellow = 255, 255, 0
# 是否按鍵
key_flag = False
# 游戲是否開(kāi)始 默認(rèn)是結(jié)束的
game_over = True
# 隨機(jī)的字母
correct_answer = random.randint(97, 122)
# 分?jǐn)?shù)
score = 0
# 開(kāi)始時(shí)間
clock_start = 0
# 讀秒倒計(jì)時(shí)
seconds = 11


def print_text(font, x, y, text, color=white):
    img_text = font.render(text, True, color)
    screen.blit(img_text, (x, y))


while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == KEYDOWN:
            key_flag = True
        elif event.type == KEYUP:
            key_flag = False
    keys = pygame.key.get_pressed()
    if keys[K_ESCAPE]:
        sys.exit()
    if keys[K_RETURN]:
        if game_over:
            game_over = False
            score = 0
            clock_start = time.perf_counter()
            seconds = 11

    screen.fill((0, 100, 0))
    current = time.perf_counter() - clock_start
    print_text(font1, 0, 0, "看看你的速度有多快")
    print_text(font1, 0, 30, "請(qǐng)?jiān)?0秒內(nèi)嘗試")

    if seconds - current < 0:
        game_over = True
    elif current <= 10:
        if keys[correct_answer]:
            correct_answer = random.randint(97, 122)
            score += 1
            clock_start = time.perf_counter()
    # 如果按鍵了
    if key_flag:
        print_text(font1, 500, 0, "按鍵了")

    if not game_over:
        print_text(font1, 0, 80, "Time: " + str(int(seconds - current)))
        print_text(font1, 500, 40,  str(int(time.perf_counter())))

    print_text(font1, 0, 100, "分?jǐn)?shù): " + str(score))

    if game_over:
        print_text(font1, 0, 160, "請(qǐng)按enter開(kāi)始游戲...")

    print_text(font2, 0, 240, chr(correct_answer - 32), yellow)

    pygame.display.update() 

以上就是Pygame實(shí)戰(zhàn)之檢測(cè)按鍵正確的小游戲的詳細(xì)內(nèi)容,更多關(guān)于Pygame按鍵正確游戲的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python Excel vlookup函數(shù)實(shí)現(xiàn)過(guò)程解析

    Python Excel vlookup函數(shù)實(shí)現(xiàn)過(guò)程解析

    這篇文章主要介紹了Python Excel vlookup函數(shù)實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • pandas.DataFrame.iloc的具體使用詳解

    pandas.DataFrame.iloc的具體使用詳解

    本文主要介紹了pandas.DataFrame.iloc的具體使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • 解決安裝python庫(kù)時(shí)windows error5 報(bào)錯(cuò)的問(wèn)題

    解決安裝python庫(kù)時(shí)windows error5 報(bào)錯(cuò)的問(wèn)題

    今天小編就為大家分享一篇解決安裝python庫(kù)時(shí)windows error5 報(bào)錯(cuò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-10-10
  • Python破解極驗(yàn)滑動(dòng)驗(yàn)證碼詳細(xì)步驟

    Python破解極驗(yàn)滑動(dòng)驗(yàn)證碼詳細(xì)步驟

    學(xué)習(xí)python知識(shí)越來(lái)越多,大家都知道極驗(yàn)驗(yàn)證碼應(yīng)用非常廣泛,今天小編就給大家分享Python破解極驗(yàn)滑動(dòng)驗(yàn)證碼的詳細(xì)步驟,對(duì)Python極驗(yàn)滑動(dòng)驗(yàn)證碼相關(guān)知識(shí)感興趣的朋友一起看看吧
    2021-05-05
  • Python 字典的使用詳解及實(shí)例代碼

    Python 字典的使用詳解及實(shí)例代碼

    今天小編幫大家簡(jiǎn)單介紹下Python的一種數(shù)據(jù)結(jié)構(gòu): 字典,字典是 另一種可變?nèi)萜髂P?,且可存?chǔ)任意類型對(duì)象,它用于存放具有映射關(guān)系的數(shù)據(jù),通讀本篇對(duì)大家的學(xué)習(xí)或工作具有一定的價(jià)值,需要的朋友可以參考下
    2021-11-11
  • Java byte數(shù)組操縱方式代碼實(shí)例解析

    Java byte數(shù)組操縱方式代碼實(shí)例解析

    這篇文章主要介紹了Java byte數(shù)組操縱方式代碼實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • 用Python+OpenCV對(duì)比圖像質(zhì)量的幾種方法

    用Python+OpenCV對(duì)比圖像質(zhì)量的幾種方法

    這篇文章主要介紹了用Python+OpenCV對(duì)比圖像質(zhì)量過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • Python中列表和元組的使用方法和區(qū)別詳解

    Python中列表和元組的使用方法和區(qū)別詳解

    這篇文章主要介紹了Python中列表和元組的使用方法和區(qū)別詳解的相關(guān)資料,需要的朋友可以參考下
    2016-07-07
  • python中關(guān)于requests里的timeout()用法

    python中關(guān)于requests里的timeout()用法

    這篇文章主要介紹了python中關(guān)于requests里的timeout()用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • Django模板過(guò)濾器和繼承示例詳解

    Django模板過(guò)濾器和繼承示例詳解

    初入python和django做項(xiàng)目,遇到很多前端頁(yè)面代碼冗余的情況,特別是頭部和腳部,代碼都是一樣的,所以下面這篇文章主要給大家介紹了關(guān)于Django模板過(guò)濾器和繼承的相關(guān)資料,需要的朋友可以參考下
    2021-11-11

最新評(píng)論