当前位置:   article > 正文

用python实现Minecraft_minecraft python编程

minecraft python编程

你有没有想过用python制作一个Minecraft?在github上就有这样一个python程序(链接:https://github.com/zifan-wang/Minecraft.git),玩起来还像模像样的:

一.游戏截图

二.代码分析

在游戏的文件夹中共有这些文件:

1.main.py

代码:(如有需要可复制,不需要可直接跳过)

  1. import sys
  2. import random
  3. import time
  4. import numba as nb
  5. import threading
  6. from collections import deque
  7. from pyglet import image
  8. from pyglet.gl import *
  9. from pyglet.graphics import TextureGroup
  10. from pyglet.window import key, mouse
  11. from settings import *
  12. SEED = random.randint(10, 1000000)#656795(种子"akioi") # 世界种子
  13. print('seed:', SEED)
  14. def cube_vertices(x, y, z, n):
  15. # 返回立方体的顶点,大小为2n。
  16. return [
  17. x-n,y+n,z-n, x-n,y+n,z+n, x+n,y+n,z+n, x+n,y+n,z-n, # top
  18. x-n,y-n,z-n, x+n,y-n,z-n, x+n,y-n,z+n, x-n,y-n,z+n, # bottom
  19. x-n,y-n,z-n, x-n,y-n,z+n, x-n,y+n,z+n, x-n,y+n,z-n, # left
  20. x+n,y-n,z+n, x+n,y-n,z-n, x+n,y+n,z-n, x+n,y+n,z+n, # right
  21. x-n,y-n,z+n, x+n,y-n,z+n, x+n,y+n,z+n, x-n,y+n,z+n, # front
  22. x+n,y-n,z-n, x-n,y-n,z-n, x-n,y+n,z-n, x+n,y+n,z-n, # back
  23. ]
  24. def tex_coord(x, y, n=8):
  25. # 返回纹理的边界顶点。
  26. m = 1.0 / n
  27. dx = x * m
  28. dy = y * m
  29. return dx, dy, dx + m, dy, dx + m, dy + m, dx, dy + m
  30. def tex_coords(top, bottom, side):
  31. # 返回顶部、底部和侧面的纹理列表。
  32. top = tex_coord(*top)
  33. bottom = tex_coord(*bottom)
  34. side = tex_coord(*side)
  35. result = []
  36. result.extend(top)
  37. result.extend(bottom)
  38. result.extend(side * 4)
  39. return result
  40. GRASS = tex_coords((1, 0), (0, 1), (0, 0))
  41. SNOW = tex_coords((4, 0), (0, 1), (1, 3))
  42. SAND = tex_coords((1, 1), (1, 1), (1, 1))
  43. DIRT = tex_coords((0, 1), (0, 1), (0, 1))
  44. STONE = tex_coords((2, 0), (2, 0), (2, 0))
  45. ENDSTONE = tex_coords((2, 1), (2, 1), (2, 1))
  46. WATER = tex_coords((0, 4), (0, 4), (0, 4))
  47. ICE = tex_coords((3, 1), (3, 1), (3, 1))
  48. WOOD = tex_coords((0, 2), (0, 2), (3, 0))
  49. LEAF = tex_coords((0, 3), (0, 3), (0, 3))
  50. BRICK = tex_coords((1, 2), (1, 2), (1, 2))
  51. PUMKEY = tex_coords((2, 2), (3, 3), (2, 3))
  52. MELON = tex_coords((2, 4), (2, 4), (1, 4))
  53. CLOUD = tex_coords((3, 2), (3, 2), (3, 2))
  54. TNT = tex_coords((4, 2), (4, 3), (4, 1))
  55. DIMO = tex_coords((3, 4), (3, 4), (3, 4))
  56. IRNO = tex_coords((4, 4), (4, 4), (4, 4))
  57. COAL = tex_coords((5, 0), (5, 0), (5, 0))
  58. GOLDO = tex_coords((5, 1), (5, 1), (5, 1))
  59. # 立方体的6个面
  60. FACES = [
  61. ( 0, 1, 0),
  62. ( 0,-1, 0),
  63. (-1, 0, 0),
  64. ( 1, 0, 0),
  65. ( 0, 0, 1),
  66. ( 0, 0,-1),
  67. ]
  68. random.seed(SEED)
  69. def normalize(position):
  70. # 将三维坐标'position'的x、y、z取近似值
  71. x, y, z = position
  72. x, y, z = (round(x), round(y), round(z))
  73. return (x, y, z)
  74. def sectorize(position):
  75. x, y, z = normalize(position)
  76. x, y, z = x // SECTOR_SIZE, y // SECTOR_SIZE, z // SECTOR_SIZE
  77. return (x, 0, z)
  78. persistence = round(random.uniform(0.25, 0.45), 6)
  79. Number_Of_Octaves = random.randint(3, 5)
  80. PMAGN = persistence * 16
  81. HAMPL = 8
  82. threads = deque() # 多线程队列
  83. @nb.jit(nopython=True, fastmath=True)
  84. def Noise(x, y):
  85. n = x + y * 57
  86. n = (n * 8192) ^ n
  87. return ( 1.0 - ( (n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0)
  88. @nb.jit(nopython=True, fastmath=True)
  89. def SmoothedNoise(x, y):
  90. corners = ( Noise(x-1, y-1)+Noise(x+1, y-1)+Noise(x-1, y+1)+Noise(x+1, y+1) ) / 16
  91. sides = ( Noise(x-1, y) +Noise(x+1, y) +Noise(x, y-1) +Noise(x, y+1) ) / 8
  92. center = Noise(x, y) / 4
  93. return corners + sides + center
  94. @nb.jit(nopython=True, fastmath=True)
  95. def Cosine_Interpolate(a, b, x):
  96. ft = x * 3.1415927
  97. f = (1 - math.cos(ft)) * 0.5
  98. return a*(1-f) + b*f
  99. @nb.jit(nopython=True, fastmath=True)
  100. def Linear_Interpolate(a, b, x):
  101. return a*(1-x) + b*x
  102. def InterpolatedNoise(x, y):
  103. integer_X = int(x)
  104. fractional_X = x - integer_X
  105. integer_Y = int(y)
  106. fractional_Y = y - integer_Y
  107. v1 = SmoothedNoise(integer_X, integer_Y)
  108. v2 = SmoothedNoise(integer_X + 1, integer_Y)
  109. v3 = SmoothedNoise(integer_X, integer_Y + 1)
  110. v4 = SmoothedNoise(integer_X + 1, integer_Y + 1)
  111. i1 = Cosine_Interpolate(v1, v2, fractional_X)
  112. i2 = Cosine_Interpolate(v3, v4, fractional_X)
  113. return Cosine_Interpolate(i1, i2, fractional_Y)
  114. def PerlinNoise(x, y):
  115. x = abs(x)
  116. y = abs(y)
  117. noise = 0
  118. p = persistence
  119. n = Number_Of_Octaves
  120. for i in range(n):
  121. frequency = pow(2,i)
  122. amplitude = pow(p,i)
  123. noise = noise + InterpolatedNoise(x * frequency, y * frequency) * amplitude
  124. return noise
  125. class mbatch:
  126. def __init__(self):
  127. self.batch = {}
  128. def add(self, x, z, *args):
  129. x = int(x / 64) * 64
  130. z = int(z / 64) * 64
  131. if
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/796984
推荐阅读
相关标签
  

闽ICP备14008679号