• python版的羊了个羊,你见过没?


     最近不是出了个超火的游戏嘛,周围小伙伴都说好难玩,玩不过

    看了下,发现就是个变种的连连看嘛,就尝试下写一个出来玩玩,当作是练手了

    刚好找到一位大佬用C语言写的羊羊,结果发现是windows版的,编译起来贼麻烦

    就想着用python写一个,方便大家学习其中的原理,不然光看代码不运行,技术是提高不了的

    运行环境为python2,需要安装pygame,安装好之后,就可以运行了

    pip2 install pygame
    

    运行效果图就不放了,保持一点神秘感^^

    废话不多说,直接上源码

    1. # -*- coding: utf-8 -*-
    2. import pygame
    3. from pygame.locals import *
    4. from sys import exit
    5. from random import randint
    6. import random
    7. windows_width = 400
    8. windows_height = 760
    9. icoSize = 48
    10. whiteColor=pygame.Color(255,255,255)
    11. redColor=pygame.Color(255,0,0)
    12. blackColor=pygame.Color(0,0,0)
    13. greyColor=pygame.Color(150,150,150)
    14. blueColor=pygame.Color(0,0,255)
    15. greenColor=pygame.Color(0,255,0)
    16. aaa=pygame.Color(128,255,0)
    17. bbb=pygame.Color(255,255,0)
    18. ccc=pygame.Color(255,255,0)
    19. sheepList=[redColor,blueColor,greenColor,greyColor,blackColor,aaa,bbb,ccc]
    20. class Point():
    21. def __init__(self,x=0,y=0):
    22. self.x=x
    23. self.y=y
    24. def main():
    25. totalScore=0
    26. score=0
    27. itemCount=5
    28. pygame.init()
    29. fpsClock=pygame.time.Clock()
    30. playSurface=pygame.display.set_mode((windows_width,windows_height))
    31. #pygame.display.set_caption("羊了个羊Python版")
    32. defaultFont=pygame.font.get_default_font()
    33. font=pygame.font.SysFont(defaultFont,24)
    34. data=[[i+1 for i in range(3)] for j in range(3)]
    35. for r in range(3):
    36. for c in range(3):
    37. r1 = random.randint(1,100) % 3;
    38. c1 = random.randint(1,100) % 3;
    39. t = data[r1][c1];
    40. data[r1][c1] = data[r][c];
    41. data[r][c] = t;
    42. store=[0,0,0,0,0,0,0]
    43. offsetX = (windows_width - (2 * (48 + icoSize) + 48)) / 2
    44. offsetY = (windows_height - (2 * (48 + icoSize) + 48)) / 2
    45. while True:
    46. pygame.display.update()
    47. #pygame.draw.rect(playSurface,whiteColor,Rect(0,0,windows_width,windows_height))
    48. playSurface.fill(whiteColor)
    49. color = (255, 0, 0)
    50. s="mission "+str(itemCount-4)
    51. text=font.render(s,True,color)
    52. playSurface.blit(text,(5,45))
    53. color = (0, 255, 0)
    54. text=font.render("score: "+str(totalScore),True,color)
    55. playSurface.blit(text,(5,65))
    56. for r in range(3):
    57. for c in range(3):
    58. if (data[r][c]):
    59. pygame.draw.rect(playSurface,sheepList[data[r][c]-1],Rect(offsetX + c * (48 + icoSize), offsetY + r * (48 + icoSize),48,48))
    60. for i in range(7):
    61. if store[i]:
    62. pygame.draw.rect(playSurface,sheepList[store[i]-1],Rect((i*50)+26,620,48,48))
    63. for event in pygame.event.get():
    64. msg=Point()
    65. if event.type==MOUSEBUTTONUP:
    66. (x,y)=event.pos
    67. print(x,y)
    68. msg=Point(x,y)
    69. else:
    70. continue
    71. for r in range(3):
    72. for c in range(3):
    73. x = offsetX + c * (48 + icoSize)
    74. y = offsetY + r * (48 + icoSize)
    75. if (msg.x > x and msg.x < x + 48 and msg.y>y and msg.y < y + 48):
    76. col = int((msg.x - offsetX) / (48 + icoSize))
    77. row = int((msg.y - offsetY) / (48 + icoSize))
    78. print("row:", row, col);
    79. for i in range(7):
    80. if (store[i] == 0):
    81. store[i] = data[row][col];
    82. break;
    83. cnt = 0;
    84. for i in range(7):
    85. if (store[i] == data[row][col]):
    86. cnt+=1
    87. if (cnt == 3):
    88. for i in range(7):
    89. if (store[i] == data[row][col]):
    90. store[i] = 0
    91. score+=1
    92. totalScore+=1
    93. if score>10:
    94. itemCount+=1
    95. score=0
    96. data[row][col] = random.randint(1,100) % itemCount+1;
    97. while True:
    98. sleep(1)
    99. return 0;
    100. if __name__=="__main__":
    101. main()

    游戏只设计了几关,已经能玩,如果想更好玩,那就自己改吧~~以下是一些改造建议

    1.将方块换成各式各样的图标

    2.当前方块是点击一个就随机生成一个,满10个消除进入下一关,后面可以改成一次性生成100个作为一关

    3.添加通关时间限制

    4.增加更多格子和图标

    5.添加排行榜

    6.添加地区排行榜

    7添加广告

    8.添加特色道具

  • 相关阅读:
    Linux系统使用AndroidStudio创建桌面快捷键
    Ansible 自动化运维工具 --- playbook 剧本
    【Web书城】书城前端开发
    报错处理:Nginx端口被占用
    设计模式 --- 工厂模式
    systemverilog:interface中的modport用法
    Linux查看文件大小的几种方法
    人工神经网络算法实战教程
    麒麟操作系统安装人大金仓数据库
    Linux下c++串口编程
  • 原文地址:https://blog.csdn.net/kof98765/article/details/127246554