python我的世界代码
import sys
import random
import time
import numba as nb
from collections import deque
from pyglet import image
from pyglet.gl import *
aphics import TextureGroup
from pyglet.window import key, mouse
import math
TICKS_PER_SEC = 60
SECTOR_SIZE = 16
GAMETYPES = False # 是否开启冰雪世界
SEED = random.randint(10, 1000000)#656795(种⼦"akioi") # 世界种⼦
print('seed:', SEED)
GTIME = 0 # 当前世界时间
GDAY = 0.0005
GNIGHT = 0.0015
WALKING_SPEED = 5 # ⾛路速度
RUNNING_SPEED = 8 # 跑步速度
FLYING_SPEED = 15 # 飞⾏速度
GRAVITY = 35.0 # 重⼒
MAX_JUMP_HEIGHT = 1.25 # 最⼤跳跃速度
JUMP_SPEED = math.sqrt(2 * GRAVITY * MAX_JUMP_HEIGHT)
TERMINAL_VELOCITY = 35 # 终端速度
PLAYER_HEIGHT = 2 # 玩家⾼度
WORLDLEN = 128 # 世界长度
TEXTURE_PATH = 'texture.png' # 纹理⽂件
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)
top = tex_coord(*top)
bottom = tex_coord(*bottom)
side = tex_coord(*side)
result = []
return result
if GAMETYPES:
GRASS = tex_coords((4, 0), (0, 1), (1, 3))
else:
GRASS = tex_coords((1, 0), (0, 1), (0, 0))
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))
if GAMETYPES:
WATER = tex_coords((3, 1), (3, 1), (3, 1))
else:
WATER = tex_coords((0, 4), (0, 4), (0, 4))
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))
# ⽴⽅体的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 = random.uniform(0.01, 0.15)
Number_Of_Octaves = random.randint(3, 5)
@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):
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):
random python
ft = x * 3.1415927
f = (1 - s(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):
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 Model(object):
def __init__(self):
self.batch = aphics.Batch()
self.shown = {} # 显⽰的⽅块
self._shown = {} # 显⽰的纹理
self.sectors = {}
self.queue = deque()
self.dfy = self._initialize()
def tree(self, y, x, z):
# ⽣成树
th = random.randint(4, 6)
ts = random.randint(th // 2, 4)
for i in range(y, y + th):
self.add_block((x, i, z), WOOD, immediate=False)
for dy in range(y + th, y + th + 2):
for dx in range(x - ts, x + ts + 1):
for dz in range(z - ts, z + ts + 1):
self.add_block((dx, dy, dz), LEAF, immediate=False)
for dy in range(y + th + 2, y + th + ts + 2):
ts -= 1
for dx in range(x - ts, x + ts + 1):
for dz in range(z - ts, z + ts + 1):
for dz in range(z - ts, z + ts + 1):
self.add_block((dx, dy, dz), LEAF, immediate=False)
def _initialize(self):
# 初始化世界
hl = WORLDLEN // 2
mn = 0
quality = 4
gmap = [[0 for x in range(0, WORLDLEN)]for z in range(0, WORLDLEN)]
for x in range(0, WORLDLEN):
for z in range(0, WORLDLEN):
gmap[x - hl][z - hl] += round(PerlinNoise(x / quality, z / quality) * quality)
mn = min(mn, gmap[x - hl][z - hl])
for x in range(-hl, hl):
for z in range(-hl, hl):
gmap[x][z] += abs(mn)
if gmap[x][z] < 2:
self.add_block((x, -1, z), random.choice([SAND, STONE]))
self.add_block((x, 0, z), WATER)
if GAMETYPES:
self.add_block((x, 1, z), WATER)
else:
self._show_block((x, 1, z), WATER)
else:
for y in range(-1, gmap[x][z]):
self.add_block((x, y, z), DIRT)
self.add_block((x, gmap[x][z], z), GRASS)
self.add_block((x, -2, z), ENDSTONE)
for x in range(-hl, hl, 4):
for z in range(-hl, hl, 4):
if x == 0 and z == 0:
continue
if random.randint(0, 3) == 1 and gmap[x][z] > 1:
<(gmap[x][z] + 1, x, z)
for i in range(x, x + 4):
for j in range(z, z + 4):
self._show_block((i, 30, j), CLOUD)
elif random.randint(0, 4) == 2 and gmap[x][z] > 2:
self.add_block((x, gmap[x][z] + 1, z), random.choice([PUMKEY, MELON]))        return gmap[0][0] + abs(mn) + 2
def hit_test(self, position, vector, max_distance=8):
m = 8
x, y, z = position
dx, dy, dz = vector
previous = None
for _ in range(max_distance * m):
key = normalize((x, y, z))
if key != previous and key in self.world:
return key, previous
previous = key
x, y, z = x + dx / m, y + dy / m, z + dz / m
return None, None
def exposed(self, position):
x, y, z = position
for dx, dy, dz in FACES:
if (x + dx, y + dy, z + dz) not in self.world:
return True
return False
def add_block(self, position, texture, immediate=True):
if position in self.world:
self.world[position] = texture
self.sectors.setdefault(sectorize(position), []).append(position)
if immediate:
posed(position):
self.show_block(position)
self.check_neighbors(position)
def remove_block(self, position, immediate=True):
del self.world[position]
self.sectors[sectorize(position)].remove(position)
if immediate:
if position in self.shown:
self.hide_block(position)
self.check_neighbors(position)
def check_neighbors(self, position):
x, y, z = position
for dx, dy, dz in FACES:
key = (x + dx, y + dy, z + dz)
if key not in self.world:
continue
posed(key):
if key not in self.shown:
self.show_block(key)
else:
if key in self.shown:
self.hide_block(key)
def show_block(self, position, immediate=True):
texture = self.world[position]
self.shown[position] = texture
if immediate:
self._show_block(position, texture)
else:
self._enqueue(self._show_block, position, texture)
def _show_block(self, position, texture):
x, y, z = position
vertex_data = cube_vertices(x, y, z, 0.5)
texture_data = list(texture)
self._shown[position] = self.batch.add(24, GL_QUADS, up,            ('v3f/static', vertex_data),
('t2f/static', texture_data))
def hide_block(self, position, immediate=True):
self.shown.pop(position)
if immediate:
self._hide_block(position)
else:
self._enqueue(self._hide_block, position)
def _hide_block(self, position):
self._shown.pop(position).delete()
def show_sector(self, sector):
for position in (sector, []):
if position not in self.shown posed(position):
self.show_block(position, False)
def hide_sector(self, sector):
for position in (sector, []):
if position in self.shown:
self.hide_block(position, False)
def change_sectors(self, before, after):
before_set = set()
after_set = set()

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。