pygame实现成语填空游戏

本文介绍如何利用pygame框架创建一个成语填空游戏。程序将所有代码集成在一个文件中,涉及图片背景、文字格子及成语列表。通过调整格子大小和数量,可以自定义界面布局。提供的资源包括背景图片、文字格子样式、成语列表文件和字体库。
摘要由CSDN通过智能技术生成

最近看到很多人玩成语填字游戏,那么先用pygame来做一个吧,花了大半天终于完成了,附下效果图。

偷了下懒程序没有拆分,所有程序写在一个文件里,主要代码如下:


    # -*- coding=utf-8 -*-
    import sys
    import random
    import pygame
    from pygame.locals import *
    reload(sys)
    sys.setdefaultencoding('utf-8')
    
    f = open('words.txt')
    all_idiom = f.readlines()
    f.close()
    
    word_dic = {
   }
    for idiom in all_idiom:
     idiom = idiom.strip().decode('utf-8')
     for word in idiom:
     if word not in word_dic: 
     word_dic[word] = [idiom]
     else:
      word_dic[word].append(idiom)
    
    word_arr = list(word_dic.keys())
    
    header_height = 30
    main_space = 20
    
    block_size = 36
    block_num=12
    bspace = 2
    space = 20
    width = block_size * block_num + main_space * 2
    height = header_height + block_size * block_num + main_space * 2 + (block_size+space) * 3
    
    pygame.init()
    screen = pygame.display.set_mode((width,height))
    screencaption = pygame.display.set_caption(u'成语填空')
    
    font = pygame.font.Font(u'syht.otf', int(block_size*0.8))
    
    dray_gray = 50,50,50
    white = 255,255,255
    #textImage = font.render(u'你好', True, white)
    
    class IdiomInfo(object):
     def __init__(self,idiom):
     self.idiom = idiom
     self.dire = 0
     self.word_arr = []
    
    class WordInfo(object):
     def __init__(self, word, i, j):
     self.i = i
     self.j = j
     self.word = word
     self.is_lock = True
     self.state = -1
     self.hide_index = -1
     self.op_hide_index = -1
    
    class Matrix(object):
     rows = 0
     cols = 0
     data = []
    
     def __init__(self, rows, cols, data=None):
      self.rows = rows
      self.cols = cols
      if data is None: data = [None for i in range(rows * cols)]
      self.data = data
    
     def set_val(self, x, y, val):
      self.data[y * self.cols + x] = val
    
     def get_val(self, x, y):
      return self.data[y * self.cols + x]
    
     def exist_val_four_around(self, x, y, ignore_set):
      move_arr = [(-1,0),(1,0),(0,-1),(0,1)]
    
      for dx,dy in move_arr:
      tx = x + dx
      ty = y + dy
      if (tx,ty) in ignore_set: continue
      if tx < 0 or tx >= self.cols or ty <0 or ty >= self.rows: continue
      if self.data[ty * self.cols + tx]: return True
      return False
    
    
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值