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
    评论
以下是一个简单的C语言消消乐小游戏代码示例: ``` #include <stdio.h> #include <stdlib.h> #include <time.h> #define ROW 8 // 定义行数 #define COL 8 // 定义列数 // 颜色定义 #define COLOR_RED "\033[1;31m" #define COLOR_GREEN "\033[1;32m" #define COLOR_YELLOW "\033[1;33m" #define COLOR_BLUE "\033[1;34m" #define COLOR_PURPLE "\033[1;35m" #define COLOR_CYAN "\033[1;36m" #define COLOR_RESET "\033[0m" // 游戏地图 int map[ROW][COL]; // 随机生成地图 void generateMap() { int i, j; srand(time(NULL)); for (i = 0; i < ROW; i++) { for (j = 0; j < COL; j++) { map[i][j] = rand() % 5 + 1; } } } // 打印地图 void printMap() { int i, j; for (i = 0; i < ROW; i++) { for (j = 0; j < COL; j++) { switch (map[i][j]) { case 1: printf(COLOR_RED "● " COLOR_RESET); break; case 2: printf(COLOR_GREEN "● " COLOR_RESET); break; case 3: printf(COLOR_YELLOW "● " COLOR_RESET); break; case 4: printf(COLOR_BLUE "● " COLOR_RESET); break; case 5: printf(COLOR_PURPLE "● " COLOR_RESET); break; default: printf(" "); break; } } printf("\n"); } } // 消除相同颜色的方块 int eliminate(int x, int y, int color) { if (x < 0 || x >= ROW || y < 0 || y >= COL || map[x][y] != color) { return 0; } map[x][y] = 0; return 1 + eliminate(x - 1, y, color) + eliminate(x + 1, y, color) + eliminate(x, y - 1, color) + eliminate(x, y + 1, color); } // 下落方块 void drop() { int i, j, k; for (j = 0; j < COL; j++) { k = ROW - 1; for (i = ROW - 1; i >= 0; i--) { if (map[i][j] != 0) { map[k--][j] = map[i][j]; } } for (; k >= 0; k--) { map[k][j] = 0; } } } // 判断是否游戏结束 int isGameOver() { int i, j; for (i = 0; i < ROW; i++) { for (j = 0; j < COL; j++) { if (map[i][j] != 0 && eliminate(i, j, map[i][j]) >= 3) { return 0; } } } return 1; } int main() { int x, y; generateMap(); while (1) { system("clear"); // 清屏 printMap(); // 打印地图 if (isGameOver()) { printf("Game Over!\n"); break; } printf("Please enter the location you want to eliminate (row, col): "); scanf("%d %d", &x, &y); if (eliminate(x, y, map[x][y]) < 3) { // 如果无法消除 printf("Cannot eliminate the block!\n"); } else { // 如果可以消除 drop(); // 下落方块 } } return 0; } ``` 这个代码示例中,使用了递归函数来消除相同颜色的方块,通过下落方块来填补空缺。在主函数中,使用了循环来不断接收玩家输入,直到游戏结束。同时,也使用了 ANSI 转义序列来实现彩色输出。 希望这个代码示例能够帮助您理解消消乐小游戏的实现方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值