十六 pygame打字遊戲二 預設的字母序列

2021-10-02 17:39:03 字數 3684 閱讀 8776

將打字遊戲改版為使用預設字母列表的的方式

# -*- coding=utf-8 -*-

import pygame

from pygame.

locals

import keydown

import random

w,h =

800,

600pygame.init(

)screen = pygame.display.set_mode(

(w, h)

)white=

255,

255,

255black=0,

0,0myfont = pygame.font.font(

none,80

)diff_ticks =

20ticks = pygame.time.get_ticks(

)+ diff_ticks

word_diff_ticks =

1000

word_ticks = pygame.time.get_ticks(

)+ word_diff_ticks

my_str =

'hello world'

# 修改你要的字母列表

word_arr =

for c in my_str:

iford

(c)!=32:

# 跳過空格

ord(c)

)def

get_random_word()

:iflen(word_arr)==0

:return

none

color =

(random.randint(0,

255)

,random.randint(0,

255)

,random.randint(0,

255)

)# 顏色隨機

x = random.randint(

100, w-

100)

# x座標從左右邊距各100之間隨機

y =0 word = word_arr.pop(0)

#random.randint(65, 90)

return x,y,word,color

arr=

))clear_word=

0level =

1pygame.display.set_caption(

'typing level:%d'

%level)

game_state=

1# 1.進行中 2.遊戲失敗

sign=

1while

true

:for event in pygame.event.get():

if event.

type

== pygame.quit:

pygame.quit(

) exit(

)if game_state==

1and

len(arr)

>

0and event.

type

== keydown:

if event.key == arr[0]

[2]+

32:# 大小寫字母差32

arr.pop(0)

iflen

(arr)==0

andlen

(word_arr)==0

: game_state =

3 clear_word +=

1if clear_word >= level*10:

level+=

1 pygame.display.set_caption(

'typing level:%d'

% level)

diff_ticks=diff_ticks*

0.9 word_diff_ticks=word_diff_ticks*

0.95

screen.fill(

(255

,255

,255))

for i in

range

(len

(arr)):

# 繪製這些字母

x, y, word, c = arr[i]

if i==

0and sign:

c =(random.randint(0,

255)

, random.randint(0,

255)

, random.randint(0,

255)

) textimage = myfont.render(

chr(word)

,true

, c)

screen.blit(textimage,

(x, y)

)if game_state ==2:

textimage = myfont.render(

"level%d fail"

%level,

true,(

255,0,

0)) sw,sh = textimage.get_size(

) screen.blit(textimage,

((w-sw)/2

,(h-sh)/2

))# 居中顯示

elif game_state ==3:

textimage = myfont.render(

"you win"

,true,(

0,255,

255)

) sw,sh = textimage.get_size(

) screen.blit(textimage,

((w-sw)/2

,(h-sh)/2

))# 居中顯示

if game_state ==1:

if pygame.time.get_ticks(

)>=word_ticks:

# 計時增加新字母

c = get_random_word(

)if c is

notnone

: word_ticks +=word_diff_ticks

if pygame.time.get_ticks(

)>= ticks:

ticks += diff_ticks

sign=

1-sign

for i in

range

(len

(arr)):

x, y, word, c = arr[i]

arr[i]

=(x, y+

1, word, c)

iflen

(arr)

>

0and arr[0]

[1]> h: game_state=

2 pygame.display.update(

)

效果圖

pygame遊戲 Pygame遊戲製作教程

pygame遊戲設計第一彈 pygame程式設計框架 www.bilibili.com pygame繪製圖形前,首先要建立乙個視窗 import pygame 使用pygame的第一步是將pygame庫匯入到python程式中 from pygame.locals import 然後需要引入pyga...

打字遊戲 摘

include stdio.h include stdlib.h include time.h void main for i 0 i90 p i j p i 0 printf s n p getchar start time clock for i 0 iend time clock a floa...

c 打字遊戲

include include include include include using namespace std 隨機生成數字 大寫字母 小寫字母 char randomchar if type 1 return rand 26 a 將游標移到控制台 x,y 處。void gotoxy int...