【pygame】Python 制作 XP 经典扫雷游戏

游戏 游戏 1346 人阅读 | 0 人回复

<
本扫雷也没必要多道,先看文件格局:
main.py
Saolei _pycache_
mineblock.py
mineblock.cpython-39.pyc
resources文章结尾有素材

再看截图:
 
212612h20aae69popptk9z.jpg

 
以后便是上代码了:
main.py
[code]"""事情室:凌天做者:M一个劲日期:2021年8月30日工夫:20:52:35"""import sysimport timeimport pygamefrom pygame.locals import *from SaoLei.mineblock import  *n = input("输进 1 开端游戏\n输进 其他键 协助:")if n != "1":    input("""左键单击:发掘左键单击:插旗左键单击:没有肯定是否是雷左键单击后念回到本初形态:左键单击左键单击后念回到本初形态:左键单击左键单击后念变成没有肯定是否是雷的形态:左键单击看完回车""")# 游戏屏幕的宽SCREEN_WIDTH = BLOCK_WIDTH * SIZE# 游戏屏幕的下SCREEN_HEIGHT = (BLOCK_HEIGHT + 2) * SIZEclass GameStatus(Enum):    readied = 1,    started = 2,    over = 3,    win = 4def print_text(screen, font, x, y, text, fcolor=(255, 255, 255)):    imgText = font.render(text, True, fcolor)    screen.blit(imgText, (x, y))def main():    pygame.init()    screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))    pygame.display.set_caption(&#39;扫雷&#39;)    font1 = pygame.font.Font("freesansbold.ttf", SIZE * 2)  # 得分的字体    fwidth, fheight = font1.size(&#39;999&#39;)    red = (200, 40, 40)    # 减载资本图片,由于资本文件巨细纷歧,以是做了同一的缩放处置    img0 = pygame.image.load(&#39;resources/0.gif&#39;).convert()    img0 = pygame.transform.smoothscale(img0, (SIZE, SIZE))    img1 = pygame.image.load(&#39;resources/1.gif&#39;).convert()    img1 = pygame.transform.smoothscale(img1, (SIZE, SIZE))    img2 = pygame.image.load(&#39;resources/2.gif&#39;).convert()    img2 = pygame.transform.smoothscale(img2, (SIZE, SIZE))    img3 = pygame.image.load(&#39;resources/3.gif&#39;).convert()    img3 = pygame.transform.smoothscale(img3, (SIZE, SIZE))    img4 = pygame.image.load(&#39;resources/4.gif&#39;).convert()    img4 = pygame.transform.smoothscale(img4, (SIZE, SIZE))    img5 = pygame.image.load(&#39;resources/5.gif&#39;).convert()    img5 = pygame.transform.smoothscale(img5, (SIZE, SIZE))    img6 = pygame.image.load(&#39;resources/6.gif&#39;).convert()    img6 = pygame.transform.smoothscale(img6, (SIZE, SIZE))    img7 = pygame.image.load(&#39;resources/7.gif&#39;).convert()    img7 = pygame.transform.smoothscale(img7, (SIZE, SIZE))    img8 = pygame.image.load(&#39;resources/8.gif&#39;).convert()    img8 = pygame.transform.smoothscale(img8, (SIZE, SIZE))    img_blank = pygame.image.load(&#39;resources/blank1.gif&#39;).convert()    img_blank = pygame.transform.smoothscale(img_blank, (SIZE, SIZE))    img_flag = pygame.image.load(&#39;resources/flag.gif&#39;).convert()    img_flag = pygame.transform.smoothscale(img_flag, (SIZE, SIZE))    img_ask = pygame.image.load(&#39;resources/ask.gif&#39;).convert()    img_ask = pygame.transform.smoothscale(img_ask, (SIZE, SIZE))    img_mine = pygame.image.load(&#39;resources/mine.gif&#39;).convert()    img_mine = pygame.transform.smoothscale(img_mine, (SIZE, SIZE))    img_blood = pygame.image.load(&#39;resources/blood.gif&#39;).convert()    img_blood = pygame.transform.smoothscale(img_blood, (SIZE, SIZE))    img_error = pygame.image.load(&#39;resources/error.gif&#39;).convert()    img_error = pygame.transform.smoothscale(img_error, (SIZE, SIZE))    face_size = int(SIZE * 1.25)    img_face_fail = pygame.image.load(&#39;resources/Ooo.gif&#39;).convert()    img_face_fail = pygame.transform.smoothscale(img_face_fail, (face_size, face_size))    img_face_normal = pygame.image.load(&#39;resources/Ooo.gif&#39;).convert()    img_face_normal = pygame.transform.smoothscale(img_face_normal, (face_size, face_size))    img_face_success = pygame.image.load(&#39;resources/smile.gif&#39;).convert()    img_face_success = pygame.transform.smoothscale(img_face_success, (face_size, face_size))    face_pos_x = (SCREEN_WIDTH - face_size) // 2    face_pos_y = (SIZE * 2 - face_size) // 2    img_dict = {        0: img0,        1: img1,        2: img2,        3: img3,        4: img4,        5: img5,        6: img6,        7: img7,        8: img8    }    bgcolor = (225, 225, 225)   # 布景致    block = MineBlock()    game_status = GameStatus.readied    start_time = None   # 开端工夫    elapsed_time = 0    # 耗时    while True:        # 添补布景致        screen.fill(bgcolor)        for event in pygame.event.get():            if event.type == QUIT:                sys.exit()            elif event.type == MOUSEBUTTONDOWN:                mouse_x, mouse_y = event.pos                x = mouse_x // SIZE                y = mouse_y // SIZE - 2                b1, b2, b3 = pygame.mouse.get_pressed()                if game_status == GameStatus.started:                    # 鼠标阁下键同时按下,假如曾经标识表记标帜了一切雷,则翻开四周一圈                    # 假如借已标识表记标帜完一切雷,则有一个四周一圈被同时按下的结果                    if b1 and b3:                        mine = block.getmine(x, y)                        if mine.status == BlockStatus.opened:                            if not block.double_mouse_button_down(x, y):                                game_status = GameStatus.over            elif event.type == MOUSEBUTTONUP:                if y < 0:                    if face_pos_x
1、本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,按照目前互联网开放的原则,我们将在不通知作者的情况下,转载文章;如果原文明确注明“禁止转载”,我们一定不会转载。如果我们转载的文章不符合作者的版权声明或者作者不想让我们转载您的文章的话,请您发送邮箱:Cdnjson@163.com提供相关证明,我们将积极配合您!
2、本网站转载文章仅为传播更多信息之目的,凡在本网站出现的信息,均仅供参考。本网站将尽力确保所提供信息的准确性及可靠性,但不保证信息的正确性和完整性,且不对因信息的不正确或遗漏导致的任何损失或损害承担责任。
3、任何透过本网站网页而链接及得到的资讯、产品及服务,本网站概不负责,亦不负任何法律责任。
4、本网站所刊发、转载的文章,其版权均归原作者所有,如其他媒体、网站或个人从本网下载使用,请在转载有关文章时务必尊重该文章的著作权,保留本网注明的“稿件来源”,并自负版权等法律责任。
回复 关闭延时

使用道具 举报

 
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则