c语言消消乐字母游戏代码,基于pygame的小游戏———数字消消乐

游戏介绍:数字会以和俄罗斯方块类似的形式落下,玩家需在数字落在屏幕中的时候按下对应数字键,让数字消失。随着关数的推移,数字下落速度会越来越快,一旦数字掉落到底部则游戏失败。左上角显示关数,游戏结束也会显示

1.设置游戏界面

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,0

myfont = pygame.font.Font(None,80)

2.随机掉落数字

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 = (0,0,0)

x = random.randint(100, w-100) # x坐标从左右边距各100之间随机

y = 0

word = random.randint(48, 57)

return x,y,word,color

arr=[]

arr.append(get_random_word())

3.消除数字判定,随时间增长掉落速度加快

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]:

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

4.游戏的开始和结束

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)) # 居中显示

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, y, word, c = arr[i]

arr[i] = (x, y+1, word, c)

if len(arr) > 0 and arr[0][1] > h: game_state=2

5.总代码参考

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,0

myfont = pygame.font.Font(None,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 = (0,0,0)

x = random.randint(100, w-100) # x坐标从左右边距各100之间随机

y = 0

word = random.randint(48, 57)

return x,y,word,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]:

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, 255))

for i in range(len(arr)): # 绘制这些数字

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

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)) # 居中显示

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, y, word, c = arr[i]

arr[i] = (x, y+1, word, c)

if len(arr) > 0 and arr[0][1] > h: game_state=2

pygame.display.update()

游戏截图

9af25a024395c33e522635aa24cc362e.png

b4d9d04903ae9de373acfe9cc1f3386e.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值