1. 程式人生 > 程式設計 >pygame實現打字遊戲

pygame實現打字遊戲

本文例項為大家分享了pygame實現打字遊戲的具體程式碼,供大家參考,具體內容如下

1.基本程式碼

下面的程式碼完成了每一秒在介面的頂部隨機生成一個新的字母

# -*- coding=utf-8 -*-
import pygame
from pygame.locals import KEYDOWN
import random

w,h = 800,600
pygame.init()
screen = pygame.display.set_mode((w,h))

white=255,255,255
black=0,0
myfont = pygame.font.Font(None,80)

word_diff_ticks = 1000
word_ticks = pygame.time.get_ticks() + word_diff_ticks

def get_random_word():
 color = (random.randint(0,255),random.randint(0,255)) # 顏色隨機
 x = random.randint(100,w-100) # x座標從左右邊距各100之間隨機
 y = 0
 word = random.randint(65,90)
 return x,y,word,color

arr=[]
arr.append(get_random_word())

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

 screen.fill((255,255))

 for i in range(len(arr)): # 繪製這些字母
  x,c = arr[i]
  textImage = myfont.render(chr(word),True,c)
  screen.blit(textImage,(x,y))

 if pygame.time.get_ticks()>=word_ticks: # 計時增加新字母
  word_ticks +=word_diff_ticks
  arr.append(get_random_word())

 pygame.display.update()

2.移動字母

先增加一個定時器,設定字母20毫秒移動一格

diff_ticks = 20
ticks = pygame.time.get_ticks() + diff_ticks

在主迴圈中加入移動的程式碼

if pygame.time.get_ticks() >= ticks:
  ticks += diff_ticks
  for i in range(len(arr)):
   x,c = arr[i]
   arr[i] = (x,y+1,c)

3.消除字母

在事件的處理程式碼中加入對鍵盤字母的判斷

for event in pygame.event.get():
  ……
 
  if len(arr)>0 and event.type == KEYDOWN:
   if event.key == arr[0][2]+32: # 大小寫字母差32
    arr.pop(0)

規定每次消除都必須是第一個,所以如果正確按下了第一個字母,就將第一個字母移除

4.增加遊戲難度級別

增加一個變數clear_word用於記錄消除的字母數量,增加一個變數level用於記錄目前的級別,把介面的標題設定顯示當前level

clear_word=0
level = 1
pygame.display.set_caption('typing level:%d'%level)

在正確按下字母后增加技術和判斷是否增加難度,因為diff_ticks和word_diff_ticks分別是字母移動的時間間隔和增加一個新字母的時間間隔,所以對這兩個變數進行*0.9的處理,就縮短了時間間隔,增加了難度

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

  if len(arr)>0 and event.type == KEYDOWN:
   if event.key == arr[0][2]+32: # 大小寫字母差32
    arr.pop(0)
    clear_word += 1
    if clear_word >= level*10: #每10個字母增加一次難度
     level+=1
     pygame.display.set_caption('typing level:%d' % level)
     diff_ticks=diff_ticks*0.9
     word_diff_ticks=word_diff_ticks*0.9

5.遊戲狀態

增加一個變數game_state用於記錄遊戲狀態

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

遊戲失敗時的顯示

if game_state == 2:
  textImage = myfont.render("Level%d fail"%level,(255,0))
  sw,sh = textImage.get_size()
  screen.blit(textImage,((w-sw)/2,(h-sh)/2)) # 居中顯示

移動字母的程式碼中做下修改,將程式碼放入game_state為1的判斷中,並且用arr[0][1] > h檢測最下面一個字母是否已經超過螢幕下線

if game_state == 1:
  if pygame.time.get_ticks()>=word_ticks: # 計時增加新字母
   word_ticks +=word_diff_ticks
   arr.append(get_random_word())

  if pygame.time.get_ticks() >= ticks:
   ticks += diff_ticks
   for i in range(len(arr)):
    x,c = arr[i]
    arr[i] = (x,c)
   if len(arr) > 0 and arr[0][1] > h: game_state=2

對字母的按鍵判斷處理處也增加下游戲狀態的判斷,避免遊戲結束後還能消除字母

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

  if game_state==1 and len(arr)>0 and event.type == KEYDOWN:
   ……

6.給第一個字母增加閃爍效果

為增加顯示效果,給第一個字母增加下閃爍效果,便於提醒要敲下對應字母。
實現思路是每移動一格讓這個字母隨機變個顏色,以實現閃爍。
定義一個sign變數,用於切換顏色

sign=1

在移動字母的地方增加下sign變數的切換1-sign實現0、1兩個值的切換

if game_state == 1:
  ……

  if pygame.time.get_ticks() >= ticks:
   ticks += diff_ticks
   sign=1-sign
   ……

在繪製字母的地方增加下sign的判斷,如果是第一個字母,並且sign不為0,則對字母做隨機顏色

for i in range(len(arr)):
  x,c = arr[i]
  if i==0 and sign:
   c = (random.randint(0,255))

  textImage = myfont.render(chr(word),y))

7.最終效果圖

8.完整的程式碼

# -*- coding=utf-8 -*-
import pygame
from pygame.locals import KEYDOWN
import random

w,80)

diff_ticks = 20
ticks = pygame.time.get_ticks() + diff_ticks
word_diff_ticks = 1000
word_ticks = pygame.time.get_ticks() + word_diff_ticks

def get_random_word():
 color = (random.randint(0,color

arr=[]
arr.append(get_random_word())

clear_word=0
level = 1
pygame.display.set_caption('typing level:%d'%level)
game_state=1 # 1.進行中 2.遊戲失敗
sign=1
while True:
 for event in pygame.event.get():
  if event.type == pygame.QUIT:
   pygame.quit()
   exit()

  if game_state==1 and len(arr)>0 and event.type == KEYDOWN:
   if event.key == arr[0][2]+32: # 大小寫字母差32
    arr.pop(0)
    clear_word += 1
    if 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))
  textImage = myfont.render(chr(word),y))

 if game_state == 2:
  textImage = myfont.render("Level%d fail"%level,(h-sh)/2)) # 居中顯示

 if game_state == 1:
  if pygame.time.get_ticks()>=word_ticks: # 計時增加新字母
   word_ticks +=word_diff_ticks
   arr.append(get_random_word())

  if pygame.time.get_ticks() >= ticks:
   ticks += diff_ticks
   sign=1-sign
   for i in range(len(arr)):
    x,c)
   if len(arr) > 0 and arr[0][1] > h: game_state=2

 pygame.display.update()

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。