• 【制杖品质,无宇非凡】来自男友的灵魂拷问


      这是一个能给带给你无限“快乐”的Python游戏源码……

      1 import sys
      2 import pygame
      3 from pygame.locals import *
      4 
      5 class Trivia(object) : #Trivia类,用于处理游戏逻辑
      6     def __init__(self,filename) : #初始化方法
      7         self.data = []
      8         self.total = 0 #总行数
      9         self.current = 0 #当前行
     10         self.score = 0 #获得分数
     11         self.scored = False #正确标志
     12         self.failed = False #错误标志
     13         self.correct = 0 #正确答案序号
     14         self.wronganswer = 0 #错误序号
     15         self.num = 1;
     16         self.colors = [white,white,white,white]
     17         f = open(filename,"r")
     18         trivia_data = f.readlines()
     19         f.close()
     20         for text_line in trivia_data :
     21             self.data.append(text_line.strip())
     22             self.total += 1
     23 
     24     def show_question(self):  # 显示问题方法
     25         print_text(font1, 210, 5, "答题游戏")
     26         print_text(font2, 160, 500-40, "请选择1-4答案中正确的一个!", purple)
     27         print_text(font2, 530, 5, "分数", purple)  # 显示分数标题
     28         print_text(font2, 550, 40, str(self.score), purple)  # 显示分数值
     29         self.correct = int(self.data[self.current+5])
     30 
     31         print_text(font1,5,80,"问题"+str(self.num))
     32         print_text(font2,20,120,self.data[self.current],yellow)
     33 
     34         if self.scored :
     35             self.colors = [white,white,white,white]
     36             self.colors[self.correct-1] = green
     37             print_text(font1,230,380,"回答正确!",green)
     38             print_text(font2,170,420,"按下回车进行下一道题目",green)
     39         elif self.failed :
     40             self.colors = [white,white,white,white]
     41             self.colors[self.correct-1] = green
     42             print_text(font1, 220, 380, "回答错误!", red)
     43             print_text(font2,170,420,"按下回车进行下一道题目",red)
     44 
     45         print_text(font1, 5, 170, "有以下选项:")
     46         print_text(font2, 20, 210, "1." + self.data[self.current + 1],self.colors[0])
     47         print_text(font2, 20, 240, "2." + self.data[self.current + 2], self.colors[1])
     48         print_text(font2, 20, 270, "3." + self.data[self.current + 3], self.colors[2])
     49         print_text(font2, 20, 300, "4." + self.data[self.current + 4], self.colors[3])
     50 
     51     def handle_input(self,number) :
     52         if not self.scored and not self.failed :
     53             if number == self.correct :
     54                 self.score += 1
     55                 self.scored = True
     56             else :
     57                 self.failed = True
     58                 self.wronganswer = number
     59 
     60     def next_question(self) :
     61         if self.scored or self.failed :
     62             self.scored = False
     63             self.failed = False
     64             self.current += 6
     65             self.num += 1
     66             self.colors = [white,white,white,white]
     67             self.correct = 0
     68             if self.current >= self.total and self.num > self.total/6:
     69                 START = False
     70                 self.num = 1
     71                 self.current = 0
     72 
     73 pygame.init() #初始化pygame库
     74 screen = pygame.display.set_mode((650,500)) #设置游戏窗口大小
     75 pygame.display.set_caption("寻找你的灵魂男友(See Your Soul Boy Friend)") #设置游戏标题
     76 font1 = pygame.font.SysFont('SimHei',30); #定义大号字体形式
     77 font2 = pygame.font.SysFont('SimHei',25); #定义小号字体形式
     78 
     79 white = 255,255,255 #颜色标识定义
     80 cyan = 0,255,255
     81 yellow = 255,255,0
     82 purple = 255,0,255
     83 green = 0,255,0
     84 red = 255,0,0
     85 
     86 def print_text(font, x, y, text, color=(255, 255, 255), shadow=True):  # 自定义文字显示方法
     87     if shadow:
     88         imgText = font.render(text, True, (0, 0, 0))
     89         screen.blit(imgText, (x - 2, y - 2))
     90     imgText = font.render(text, True, color)
     91     screen.blit(imgText, (x, y))
     92 
     93 trivia = Trivia("C:/Users/Gulob/Desktop/Q.txt")
     94 
     95 while True:
     96     for event in pygame.event.get():
     97         if event.type == QUIT:
     98             sys.exit()
     99         elif event.type == KEYUP:
    100             if event.type == pygame.K_ESCAPE:
    101                 sys.exit()
    102             elif event.key == pygame.K_1:
    103                 trivia.handle_input(1)
    104             elif event.key == pygame.K_2:
    105                 trivia.handle_input(2)
    106             elif event.key == pygame.K_3:
    107                 trivia.handle_input(3)
    108             elif event.key == pygame.K_4:
    109                 trivia.handle_input(4)
    110             elif event.key == pygame.K_RETURN:
    111                 trivia.next_question()
    112     screen.fill((0,0,0))
    113     trivia.show_question()
    114     pygame.display.update()

    演示效果:

    此时,我的感受:

    【慢慢做出像样的游戏来……】

  • 相关阅读:
    php iconv函数转换出错问题
    linux 上配置swoole
    Linux中查看某 个软件的安装路径
    mysql 5.0存储过程学习总结
    maven--私服的搭建(Nexus的使用)
    Linux的chattr与lsattr命令详解
    [转]Delphi 中 image 控件加载bmp、JPG、GIF、PNG等图片的办法
    [转]Delphi 中动态链接库(dll)的建立和使用
    Delphi PChar与String互转
    [转]Delphi 快捷键 让你更像高手!!
  • 原文地址:https://www.cnblogs.com/moegarn/p/12926996.html
Copyright © 2020-2023  润新知