概述
游戏介绍:数字会以和俄罗斯方块类似的形式落下,玩家需在数字落在屏幕中的时候按下对应数字键,让数字消失。随着关数的推移,数字下落速度会越来越快,一旦数字掉落到底部则游戏失败。左上角显示关数,游戏结束也会显示
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()
游戏截图
最后
以上就是粗心棒球为你收集整理的c语言消消乐字母游戏代码,基于pygame的小游戏———数字消消乐的全部内容,希望文章能够帮你解决c语言消消乐字母游戏代码,基于pygame的小游戏———数字消消乐所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复