赞
踩
你有没有想过用python制作一个Minecraft?在github上就有这样一个python程序(链接:https://github.com/zifan-wang/Minecraft.git),玩起来还像模像样的:
在游戏的文件夹中共有这些文件:
代码:(如有需要可复制,不需要可直接跳过)
- import sys
- import random
- import time
- import numba as nb
- import threading
-
- from collections import deque
- from pyglet import image
- from pyglet.gl import *
- from pyglet.graphics import TextureGroup
- from pyglet.window import key, mouse
- from settings import *
-
- SEED = random.randint(10, 1000000)#656795(种子"akioi") # 世界种子
- print('seed:', SEED)
-
-
- def cube_vertices(x, y, z, n):
- # 返回立方体的顶点,大小为2n。
- return [
- 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
- 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
- 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
- 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
- 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
- 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
- ]
-
- def tex_coord(x, y, n=8):
- # 返回纹理的边界顶点。
- m = 1.0 / n
- dx = x * m
- dy = y * m
- return dx, dy, dx + m, dy, dx + m, dy + m, dx, dy + m
-
-
- def tex_coords(top, bottom, side):
- # 返回顶部、底部和侧面的纹理列表。
- top = tex_coord(*top)
- bottom = tex_coord(*bottom)
- side = tex_coord(*side)
- result = []
- result.extend(top)
- result.extend(bottom)
- result.extend(side * 4)
- return result
-
- GRASS = tex_coords((1, 0), (0, 1), (0, 0))
- SNOW = tex_coords((4, 0), (0, 1), (1, 3))
- SAND = tex_coords((1, 1), (1, 1), (1, 1))
- DIRT = tex_coords((0, 1), (0, 1), (0, 1))
- STONE = tex_coords((2, 0), (2, 0), (2, 0))
- ENDSTONE = tex_coords((2, 1), (2, 1), (2, 1))
- WATER = tex_coords((0, 4), (0, 4), (0, 4))
- ICE = tex_coords((3, 1), (3, 1), (3, 1))
- WOOD = tex_coords((0, 2), (0, 2), (3, 0))
- LEAF = tex_coords((0, 3), (0, 3), (0, 3))
- BRICK = tex_coords((1, 2), (1, 2), (1, 2))
- PUMKEY = tex_coords((2, 2), (3, 3), (2, 3))
- MELON = tex_coords((2, 4), (2, 4), (1, 4))
- CLOUD = tex_coords((3, 2), (3, 2), (3, 2))
- TNT = tex_coords((4, 2), (4, 3), (4, 1))
- DIMO = tex_coords((3, 4), (3, 4), (3, 4))
- IRNO = tex_coords((4, 4), (4, 4), (4, 4))
- COAL = tex_coords((5, 0), (5, 0), (5, 0))
- GOLDO = tex_coords((5, 1), (5, 1), (5, 1))
-
- # 立方体的6个面
- FACES = [
- ( 0, 1, 0),
- ( 0,-1, 0),
- (-1, 0, 0),
- ( 1, 0, 0),
- ( 0, 0, 1),
- ( 0, 0,-1),
- ]
-
- random.seed(SEED)
-
-
- def normalize(position):
- # 将三维坐标'position'的x、y、z取近似值
- x, y, z = position
- x, y, z = (round(x), round(y), round(z))
- return (x, y, z)
-
-
- def sectorize(position):
- x, y, z = normalize(position)
- x, y, z = x // SECTOR_SIZE, y // SECTOR_SIZE, z // SECTOR_SIZE
- return (x, 0, z)
-
-
- persistence = round(random.uniform(0.25, 0.45), 6)
- Number_Of_Octaves = random.randint(3, 5)
- PMAGN = persistence * 16
- HAMPL = 8
- threads = deque() # 多线程队列
-
- @nb.jit(nopython=True, fastmath=True)
- def Noise(x, y):
- n = x + y * 57
- n = (n * 8192) ^ n
- return ( 1.0 - ( (n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0)
-
- @nb.jit(nopython=True, fastmath=True)
- def SmoothedNoise(x, y):
- corners = ( Noise(x-1, y-1)+Noise(x+1, y-1)+Noise(x-1, y+1)+Noise(x+1, y+1) ) / 16
- sides = ( Noise(x-1, y) +Noise(x+1, y) +Noise(x, y-1) +Noise(x, y+1) ) / 8
- center = Noise(x, y) / 4
- return corners + sides + center
-
- @nb.jit(nopython=True, fastmath=True)
- def Cosine_Interpolate(a, b, x):
- ft = x * 3.1415927
- f = (1 - math.cos(ft)) * 0.5
- return a*(1-f) + b*f
-
- @nb.jit(nopython=True, fastmath=True)
- def Linear_Interpolate(a, b, x):
- return a*(1-x) + b*x
-
- def InterpolatedNoise(x, y):
- integer_X = int(x)
- fractional_X = x - integer_X
- integer_Y = int(y)
- fractional_Y = y - integer_Y
- v1 = SmoothedNoise(integer_X, integer_Y)
- v2 = SmoothedNoise(integer_X + 1, integer_Y)
- v3 = SmoothedNoise(integer_X, integer_Y + 1)
- v4 = SmoothedNoise(integer_X + 1, integer_Y + 1)
- i1 = Cosine_Interpolate(v1, v2, fractional_X)
- i2 = Cosine_Interpolate(v3, v4, fractional_X)
- return Cosine_Interpolate(i1, i2, fractional_Y)
-
- def PerlinNoise(x, y):
- x = abs(x)
- y = abs(y)
- noise = 0
- p = persistence
- n = Number_Of_Octaves
- for i in range(n):
- frequency = pow(2,i)
- amplitude = pow(p,i)
- noise = noise + InterpolatedNoise(x * frequency, y * frequency) * amplitude
- return noise
-
- class mbatch:
- def __init__(self):
- self.batch = {}
-
- def add(self, x, z, *args):
- x = int(x / 64) * 64
- z = int(z / 64) * 64
- if
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。