马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
好久没有发长帖了
废话不多说,直接奔入主题。
以下是用Python制作《我的世界》的源代码:
from pyglet import image
from pyglet.gl import *
from pyglet.window import key, mouse
import math
import random
TICKS_PER_SEC = 60
SECTOR_SIZE = 16
WALKING_SPEED = 5
FLYING_SPEED = 15
GRAVITY = 20.0
MAX_JUMP_HEIGHT = 1.0
JUMP_SPEED = math.sqrt(2 * GRAVITY * MAX_JUMP_HEIGHT)
TERMINAL_VELOCITY = 50
PLAYER_HEIGHT = 2
def cube_vertices(x, y, z, n):
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,
x-n,y-n,z-n, x+n,y-n,z-n, x+n,y-n,z+n, x-n,y-n,z+n,
x-n,y-n,z-n, x-n,y-n,z+n, x-n,y+n,z+n, x-n,y+n,z-n,
x+n,y-n,z+n, x+n,y-n,z-n, x+n,y+n,z-n, x+n,y+n,z+n,
x-n,y-n,z+n, x+n,y-n,z+n, x+n,y+n,z+n, x-n,y+n,z+n,
x+n,y-n,z-n, x-n,y-n,z-n, x-n,y+n,z-n, x+n,y+n,z-n]
def tex_coord(x, y, n=4):
m = 1.0 / n
dx = x * m
dy = y * m
return dx, dy, dx + m, 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
TEXTURE_PATH = 'texture.png'
GRASS = tex_coords((1, 0), (0, 1), (0, 0))
SAND = tex_coords((1, 1), (1, 1), (1, 1))
BRICK = tex_coords((2, 0), (2, 0), (2, 0))
STONE = tex_coords((2, 1), (2, 1), (2, 1))
class Model(object):
def __init__(self):
self.batch = pyglet.graphics.Batch()
self.group = TextureGroup(image.load(TEXTURE_PATH).get_texture())
self.world = {}
self.shown = {}
self._shown = {}
self.sectors = {}
self.queue = deque()
self._initialize()
def _initialize(self):
n = 80
s = 1
y = 0
for x in range(-n, n + 1, s):
for z in range(-n, n + 1, s):
self.add_block((x, y - 2, z), GRASS, immediate=False)
self.add_block((x, y - 3, z), STONE, immediate=False)
if x in (-n, n) or z in (-n, n):
for dy in range(-2, 3):
self.add_block((x, y + dy, z), STONE, immediate=False)
o = n - 10
for _ in range(120):
a = random.randint(-o, o)
b = random.randint(-o, o)
c = -1
h = random.randint(1, 6)
s = random.randint(4, 8)
d = 1
t = random.choice([GRASS, SAND, BRICK])
for y in range(c, c + h):
for x in range(a - s, a + s + 1):
for z in range(b - s, b + s + 1):
if (x - a) ** 2 + (z - b) ** 2 > (s + 1) ** 2:
continue
if (x - 0) ** 2 + (z - 0) ** 2 < 5 ** 2:
continue
self.add_block((x, y, z), t, immediate=False)
def add_block(self, position, texture, immediate=True):
if position in self.world:
self.remove_block(position)
self.world[position] = texture
if immediate:
if self.exposed(position):
self.show_block(position)
def remove_block(self, position):
del self.world[position]
if position in self.shown:
self.hide_block(position)
def show_block(self, position):
texture = self.world[position]
vertex_data = cube_vertices(*position, 0.5)
texture_data = list(texture)
self._shown[position] = self.batch.add(24, GL_QUADS,
self.group,
('v3f/static', vertex_data),
('t2f/static', texture_data))
self.shown[position] = texture
def hide_block(self, position):
self._shown.pop(position).delete()
del self.shown[position]
class Window(pyglet.window.Window):
def __init__(self, *args, **kwargs):
super(Window, self).__init__(*args, **kwargs)
self.model = Model()
pyglet.clock.schedule_interval(self.update, 1.0 / TICKS_PER_SEC)
def update(self, dt):
pass
def on_draw(self):
self[/code][/code]
高手在民间
喜欢的可以试试跑一下,不过要安装Pyglet库。
pip install pyglet |