鱼C论坛

 找回密码
 立即注册
查看: 308|回复: 4

[已解决]求助

[复制链接]
发表于 2024-12-5 21:42:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. """
  2. from zifan.site
  3. version 1.1.0
  4. python 3.x
  5. """
  6. import sys
  7. import random
  8. import time
  9. import numba as nb

  10. from collections import deque
  11. from pyglet import image
  12. from pyglet.gl import *
  13. from pyglet.graphics import TextureGroup
  14. from pyglet.window import key, mouse
  15. import math

  16. TICKS_PER_SEC = 60

  17. SECTOR_SIZE = 16

  18. GAMETYPES = False # 是否开启冰雪世界

  19. SEED = random.randint(10, 1000000)#656795(种子"akioi") # 世界种子
  20. print('seed:', SEED)

  21. GTIME = 0 # 当前世界时间
  22. GDAY = 0.0005
  23. GNIGHT = 0.0015

  24. WALKING_SPEED = 5 # 走路速度
  25. RUNNING_SPEED = 8 # 跑步速度
  26. FLYING_SPEED = 15 # 飞行速度

  27. GRAVITY = 35.0 # 重力
  28. MAX_JUMP_HEIGHT = 1.25 # 最大跳跃速度
  29. JUMP_SPEED = math.sqrt(2 * GRAVITY * MAX_JUMP_HEIGHT)
  30. TERMINAL_VELOCITY = 35 # 终端速度

  31. PLAYER_HEIGHT = 2 # 玩家高度

  32. WORLDLEN = 128 # 世界长度

  33. TEXTURE_PATH = 'texture.png' # 纹理文件

  34. def cube_vertices(x, y, z, n):
  35.     # 返回立方体的顶点,大小为2n。
  36.     return [
  37.         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
  38.         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
  39.         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
  40.         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
  41.         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
  42.         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
  43.     ]

  44. def tex_coord(x, y, n=8):
  45.     # 返回纹理的边界顶点。
  46.     m = 1.0 / n
  47.     dx = x * m
  48.     dy = y * m
  49.     return dx, dy, dx + m, dy, dx + m, dy + m, dx, dy + m


  50. def tex_coords(top, bottom, side):
  51.     # 返回顶部、底部和侧面的纹理列表。
  52.     top = tex_coord(*top)
  53.     bottom = tex_coord(*bottom)
  54.     side = tex_coord(*side)
  55.     result = []
  56.     result.extend(top)
  57.     result.extend(bottom)
  58.     result.extend(side * 4)
  59.     return result

  60. if GAMETYPES:
  61.     GRASS = tex_coords((4, 0), (0, 1), (1, 3))
  62. else:
  63.     GRASS = tex_coords((1, 0), (0, 1), (0, 0))
  64. SAND = tex_coords((1, 1), (1, 1), (1, 1))
  65. DIRT = tex_coords((0, 1), (0, 1), (0, 1))
  66. STONE = tex_coords((2, 0), (2, 0), (2, 0))
  67. ENDSTONE = tex_coords((2, 1), (2, 1), (2, 1))
  68. if GAMETYPES:
  69.     WATER = tex_coords((3, 1), (3, 1), (3, 1))
  70. else:
  71.     WATER = tex_coords((0, 4), (0, 4), (0, 4))
  72. WOOD = tex_coords((0, 2), (0, 2), (3, 0))
  73. LEAF = tex_coords((0, 3), (0, 3), (0, 3))
  74. BRICK = tex_coords((1, 2), (1, 2), (1, 2))
  75. PUMKEY = tex_coords((2, 2), (3, 3), (2, 3))
  76. MELON = tex_coords((2, 4), (2, 4), (1, 4))
  77. CLOUD = tex_coords((3, 2), (3, 2), (3, 2))
  78. TNT = tex_coords((4, 2), (4, 3), (4, 1))

  79. # 立方体的6个面
  80. FACES = [
  81.     ( 0, 1, 0),
  82.     ( 0,-1, 0),
  83.     (-1, 0, 0),
  84.     ( 1, 0, 0),
  85.     ( 0, 0, 1),
  86.     ( 0, 0,-1),
  87. ]

  88. random.seed(SEED)


  89. def normalize(position):
  90.     # 将三维坐标'position'的x、y、z取近似值
  91.     x, y, z = position
  92.     x, y, z = (round(x), round(y), round(z))
  93.     return (x, y, z)


  94. def sectorize(position):
  95.     x, y, z = normalize(position)
  96.     x, y, z = x // SECTOR_SIZE, y // SECTOR_SIZE, z // SECTOR_SIZE
  97.     return (x, 0, z)


  98. persistence = random.uniform(0.01, 0.15)
  99. Number_Of_Octaves = random.randint(3, 5)

  100. @nb.jit(nopython=True, fastmath=True)
  101. def Noise(x, y):
  102.     n = x + y * 57
  103.     n = (n * 8192) ^ n
  104.     return ( 1.0 - ( (n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0)

  105. @nb.jit(nopython=True, fastmath=True)
  106. def SmoothedNoise(x, y):
  107.     corners = ( Noise(x-1, y-1)+Noise(x+1, y-1)+Noise(x-1, y+1)+Noise(x+1, y+1) ) / 16
  108.     sides = ( Noise(x-1, y) +Noise(x+1, y) +Noise(x, y-1) +Noise(x, y+1) ) / 8
  109.     center = Noise(x, y) / 4
  110.     return corners + sides + center

  111. @nb.jit(nopython=True, fastmath=True)
  112. def Cosine_Interpolate(a, b, x):
  113.     ft = x * 3.1415927
  114.     f = (1 - math.cos(ft)) * 0.5
  115.     return a*(1-f) + b*f

  116. @nb.jit(nopython=True, fastmath=True)
  117. def Linear_Interpolate(a, b, x):
  118.     return a*(1-x) + b*x

  119. def InterpolatedNoise(x, y):
  120.     integer_X = int(x)
  121.     fractional_X = x - integer_X
  122.     integer_Y = int(y)
  123.     fractional_Y = y - integer_Y
  124.     v1 = SmoothedNoise(integer_X, integer_Y)
  125.     v2 = SmoothedNoise(integer_X + 1, integer_Y)
  126.     v3 = SmoothedNoise(integer_X, integer_Y + 1)
  127.     v4 = SmoothedNoise(integer_X + 1, integer_Y + 1)
  128.     i1 = Cosine_Interpolate(v1, v2, fractional_X)
  129.     i2 = Cosine_Interpolate(v3, v4, fractional_X)
  130.     return Cosine_Interpolate(i1, i2, fractional_Y)

  131. def PerlinNoise(x, y):
  132.     noise = 0
  133.     p = persistence
  134.     n = Number_Of_Octaves
  135.     for i in range(n):
  136.         frequency = pow(2,i)
  137.         amplitude = pow(p,i)
  138.         noise = noise + InterpolatedNoise(x * frequency, y * frequency) * amplitude
  139.     return noise

  140. class Model(object):

  141.     def __init__(self):

  142.         self.batch = pyglet.graphics.Batch()
  143.         self.group = TextureGroup(image.load(TEXTURE_PATH).get_texture()) # 纹理列表
  144.         self.world = {} # 地图
  145.         self.shown = {} # 显示的方块
  146.         self._shown = {} # 显示的纹理
  147.         self.sectors = {}
  148.         self.queue = deque()
  149.         self.dfy = self._initialize()

  150.     def tree(self, y, x, z):
  151.         # 生成树
  152.         th = random.randint(4, 6)
  153.         ts = random.randint(th // 2, 4)
  154.         for i in range(y, y + th):
  155.             self.add_block((x, i, z), WOOD, immediate=False)
  156.         for dy in range(y + th, y + th + 2):
  157.              for dx in range(x - ts, x + ts + 1):
  158.                 for dz in range(z - ts, z + ts + 1):
  159.                     self.add_block((dx, dy, dz), LEAF, immediate=False)
  160.         for dy in range(y + th + 2, y + th + ts + 2):
  161.             ts -= 1
  162.             for dx in range(x - ts, x + ts + 1):
  163.                 for dz in range(z - ts, z + ts + 1):
  164.                     self.add_block((dx, dy, dz), LEAF, immediate=False)

  165.     def _initialize(self):
  166.         # 初始化世界
  167.         hl = WORLDLEN // 2
  168.         mn = 0
  169.         quality = 4
  170.         gmap = [[0 for x in range(0, WORLDLEN)]for z in range(0, WORLDLEN)]
  171.         for x in range(0, WORLDLEN):
  172.             for z in range(0, WORLDLEN):
  173.                 gmap[x - hl][z - hl] += round(PerlinNoise(x / quality, z / quality) * quality)
  174.                 mn = min(mn, gmap[x - hl][z - hl])
  175.         for x in range(-hl, hl):
  176.             for z in range(-hl, hl):
  177.                 gmap[x][z] += abs(mn)
  178.                 if gmap[x][z] < 2:
  179.                     self.add_block((x, -1, z), random.choice([SAND, STONE]))
  180.                     self.add_block((x, 0, z), WATER)
  181.                     if GAMETYPES:
  182.                         self.add_block((x, 1, z), WATER)
  183.                     else:
  184.                         self._show_block((x, 1, z), WATER)
  185.                 else:
  186.                     for y in range(-1, gmap[x][z]):
  187.                         self.add_block((x, y, z), DIRT)
  188.                     self.add_block((x, gmap[x][z], z), GRASS)
  189.                 self.add_block((x, -2, z), ENDSTONE)
  190.         for x in range(-hl, hl, 4):
  191.             for z in range(-hl, hl, 4):
  192.                 if x == 0 and z == 0:
  193.                     continue
  194.                 if random.randint(0, 3) == 1 and gmap[x][z] > 1:
  195.                     self.tree(gmap[x][z] + 1, x, z)
  196.                     for i in range(x, x + 4):
  197.                         for j in range(z, z + 4):
  198.                             self._show_block((i, 30, j), CLOUD)
  199.                 elif random.randint(0, 4) == 2 and gmap[x][z] > 2:
  200.                     self.add_block((x, gmap[x][z] + 1, z), random.choice([PUMKEY, MELON]))
  201.         return gmap[0][0] + abs(mn) + 2

  202.     def hit_test(self, position, vector, max_distance=8):
  203.         m = 8
  204.         x, y, z = position
  205.         dx, dy, dz = vector
  206.         previous = None
  207.         for _ in range(max_distance * m):
  208.             key = normalize((x, y, z))
  209.             if key != previous and key in self.world:
  210.                 return key, previous
  211.             previous = key
  212.             x, y, z = x + dx / m, y + dy / m, z + dz / m
  213.         return None, None

  214.     def exposed(self, position):
  215.         x, y, z = position
  216.         for dx, dy, dz in FACES:
  217.             if (x + dx, y + dy, z + dz) not in self.world:
  218.                 return True
  219.         return False

  220.     def add_block(self, position, texture, immediate=True):
  221.         if position in self.world:
  222.             self.remove_block(position, immediate)
  223.         self.world[position] = texture
  224.         self.sectors.setdefault(sectorize(position), []).append(position)
  225.         if immediate:
  226.             if self.exposed(position):
  227.                 self.show_block(position)
  228.             self.check_neighbors(position)

  229.     def remove_block(self, position, immediate=True):
  230.         del self.world[position]
  231.         self.sectors[sectorize(position)].remove(position)
  232.         if immediate:
  233.             if position in self.shown:
  234.                 self.hide_block(position)
  235.             self.check_neighbors(position)

  236.     def check_neighbors(self, position):
  237.         x, y, z = position
  238.         for dx, dy, dz in FACES:
  239.             key = (x + dx, y + dy, z + dz)
  240.             if key not in self.world:
  241.                 continue
  242.             if self.exposed(key):
  243.                 if key not in self.shown:
  244.                     self.show_block(key)
  245.             else:
  246.                 if key in self.shown:
  247.                     self.hide_block(key)

  248.     def show_block(self, position, immediate=True):
  249.         texture = self.world[position]
  250.         self.shown[position] = texture
  251.         if immediate:
  252.             self._show_block(position, texture)
  253.         else:
  254.             self._enqueue(self._show_block, position, texture)

  255.     def _show_block(self, position, texture):
  256.         x, y, z = position
  257.         vertex_data = cube_vertices(x, y, z, 0.5)
  258.         texture_data = list(texture)
  259.         self._shown[position] = self.batch.add(24, GL_QUADS, self.group,
  260.             ('v3f/static', vertex_data),
  261.             ('t2f/static', texture_data))

  262.     def hide_block(self, position, immediate=True):
  263.         self.shown.pop(position)
  264.         if immediate:
  265.             self._hide_block(position)
  266.         else:
  267.             self._enqueue(self._hide_block, position)

  268.     def _hide_block(self, position):
  269.         self._shown.pop(position).delete()

  270.     def show_sector(self, sector):
  271.         for position in self.sectors.get(sector, []):
  272.             if position not in self.shown and self.exposed(position):
  273.                 self.show_block(position, False)

  274.     def hide_sector(self, sector):
  275.         for position in self.sectors.get(sector, []):
  276.             if position in self.shown:
  277.                 self.hide_block(position, False)

  278.     def change_sectors(self, before, after):
  279.         before_set = set()
  280.         after_set = set()
  281.         pad = 4
  282.         for dx in range(-pad, pad + 1):
  283.             for dy in [0]:
  284.                 for dz in range(-pad, pad + 1):
  285.                     if dx ** 2 + dy ** 2 + dz ** 2 > (pad + 1) ** 2:
  286.                         continue
  287.                     if before:
  288.                         x, y, z = before
  289.                         before_set.add((x + dx, y + dy, z + dz))
  290.                     if after:
  291.                         x, y, z = after
  292.                         after_set.add((x + dx, y + dy, z + dz))
  293.         show = after_set - before_set
  294.         hide = before_set - after_set
  295.         for sector in show:
  296.             self.show_sector(sector)
  297.         for sector in hide:
  298.             self.hide_sector(sector)

  299.     def _enqueue(self, func, *args):
  300.         self.queue.append((func, args))

  301.     def _dequeue(self):
  302.         func, args = self.queue.popleft()
  303.         func(*args)

  304.     def process_queue(self):
  305.         start = time.perf_counter()
  306.         while self.queue and time.perf_counter() - start < 1.0 / TICKS_PER_SEC:
  307.             self._dequeue()

  308.     def process_entire_queue(self):
  309.         while self.queue:
  310.             self._dequeue()


  311. class Window(pyglet.window.Window):

  312.     def __init__(self, *args, **kwargs):
  313.         super(Window, self).__init__(*args, **kwargs)
  314.         self.exclusive = False
  315.         self.flying = False # 是否在飞行
  316.         self.walking = True # 是否在走路
  317.         self.jumping = False # 是否在跳
  318.         self.model = Model()
  319.         self.strafe = [0, 0]
  320.         self.position = (0, self.model.dfy, 0)
  321.         self.rotation = (0, 0)
  322.         self.sector = None
  323.         self.reticle = None
  324.         self.dy = 0
  325.         self.inventory = [GRASS, DIRT, STONE, SAND, WOOD, BRICK, PUMKEY, MELON, TNT]
  326.         self.block = self.inventory[0]
  327.         self.num_keys = [
  328.             key._1, key._2, key._3, key._4, key._5,
  329.             key._6, key._7, key._8, key._9, key._0]
  330.         self.label = pyglet.text.Label('', font_name='Arial', font_size=18,
  331.             x=10, y=self.height - 10, anchor_x='left', anchor_y='top',
  332.             color=(0, 0, 0, 255))
  333.         pyglet.clock.schedule_interval(self.update, 1.0 / TICKS_PER_SEC)

  334.     def set_exclusive_mouse(self, exclusive):
  335.         super(Window, self).set_exclusive_mouse(exclusive)
  336.         self.exclusive = exclusive

  337.     def get_sight_vector(self):
  338.         x, y = self.rotation
  339.         m = math.cos(math.radians(y))
  340.         dy = math.sin(math.radians(y))
  341.         dx = math.cos(math.radians(x - 90)) * m
  342.         dz = math.sin(math.radians(x - 90)) * m
  343.         return (dx, dy, dz)

  344.     def get_motion_vector(self):
  345.         if any(self.strafe):
  346.             x, y = self.rotation
  347.             strafe = math.degrees(math.atan2(*self.strafe))
  348.             y_angle = math.radians(y)
  349.             x_angle = math.radians(x + strafe)
  350.             if self.flying:
  351.                 m = math.cos(y_angle)
  352.                 dy = math.sin(y_angle)
  353.                 if self.strafe[1]:
  354.                     dy = 0.0
  355.                     m = 1
  356.                 if self.strafe[0] > 0:
  357.                     dy *= -1
  358.                 dx = math.cos(x_angle) * m
  359.                 dz = math.sin(x_angle) * m
  360.             else:
  361.                 dy = 0.0
  362.                 dx = math.cos(x_angle)
  363.                 dz = math.sin(x_angle)
  364.         else:
  365.             dy = 0.0
  366.             dx = 0.0
  367.             dz = 0.0
  368.         return (dx, dy, dz)

  369.     def update(self, dt):
  370.         # 刷新
  371.         global GTIME
  372.         global GNIGHT
  373.         global GDAY
  374.         glClearColor(0.5 - GTIME * 0.01, 0.69 - GTIME * 0.01, 1.0 - GTIME * 0.01, 1)
  375.         setup_fog()
  376.         GTIME += GDAY if GTIME < 23 else GNIGHT
  377.         if GTIME > 50:
  378.             GTIME = 50
  379.             GNIGHT = -GNIGHT
  380.             GDAY = -GDAY
  381.         elif GTIME < 0:
  382.             GTIME = 0
  383.             GNIGHT = -GNIGHT
  384.             GDAY = -GDAY
  385.         self.model.process_queue()
  386.         sector = sectorize(self.position)
  387.         if sector != self.sector:
  388.             self.model.change_sectors(self.sector, sector)
  389.             if self.sector is None:
  390.                 self.model.process_entire_queue()
  391.             self.sector = sector
  392.         m = 8
  393.         dt = min(dt, 0.2)
  394.         if self.jumping:
  395.             if self.dy == 0:
  396.                 self.dy = JUMP_SPEED
  397.         for _ in range(m):
  398.             self._update(dt / m)

  399.     def _update(self, dt):
  400.         speed = FLYING_SPEED if self.flying else WALKING_SPEED if self.walking else RUNNING_SPEED
  401.         d = dt * speed
  402.         dx, dy, dz = self.get_motion_vector()
  403.         dx, dy, dz = dx * d, dy * d, dz * d
  404.         if not self.flying:
  405.             self.dy -= dt * GRAVITY
  406.             self.dy = max(self.dy, -TERMINAL_VELOCITY)
  407.             dy += self.dy * dt
  408.         x, y, z = self.position
  409.         x, y, z = self.collide((x + dx, y + dy, z + dz), PLAYER_HEIGHT)
  410.         self.position = (x, y, z)

  411.     def collide(self, position, height):
  412.         pad = 0.25
  413.         p = list(position)
  414.         np = normalize(position)
  415.         for face in FACES:
  416.             for i in range(3):
  417.                 if not face[i]:
  418.                     continue
  419.                 d = (p[i] - np[i]) * face[i]
  420.                 if d < pad:
  421.                     continue
  422.                 for dy in range(height):
  423.                     op = list(np)
  424.                     op[1] -= dy
  425.                     op[i] += face[i]
  426.                     if tuple(op) not in self.model.world:
  427.                         continue
  428.                     p[i] -= (d - pad) * face[i]
  429.                     if face == (0, -1, 0) or face == (0, 1, 0):
  430.                         self.dy = 0
  431.                     break
  432.         return tuple(p)

  433.     def TNTboom(self, x, y, z):
  434.         # TNT爆炸
  435.         self.model.remove_block((x, y, z))
  436.         bf = 4
  437.         s = 0
  438.         for dy in range(y - bf, y):
  439.             for i in range(x - s, x + s):
  440.                 for j in range(z - s, z + s):
  441.                     if (i, dy, j) in self.model.world:
  442.                         if j == z-s or j == z+s-1 or i == x-s or i == x+s-1:
  443.                             if random.randint(0, 1):
  444.                                 if self.model.world[(i, dy, j)] == TNT:
  445.                                     self.TNTboom(i, dy, j)
  446.                                     continue
  447.                                 if self.model.world[(i, dy, j)] != ENDSTONE:
  448.                                     self.model.remove_block((i, dy, j))
  449.                         else:
  450.                             if self.model.world[(i, dy, j)] == TNT:
  451.                                 self.TNTboom(i, dy, j)
  452.                                 continue
  453.                             if self.model.world[(i, dy, j)] != ENDSTONE:
  454.                                 self.model.remove_block((i, dy, j))
  455.             s += 1
  456.         s = bf
  457.         for i in range(x - s, x + s):
  458.             for j in range(z - s, z + s):
  459.                 if (i, y, j) in self.model.world:
  460.                     if j == z-s or j == z+s-1 or i == x-s or i == x+s-1:
  461.                         if random.randint(0, 1):
  462.                             if self.model.world[(i, y, j)] == TNT:
  463.                                 self.TNTboom(i, y, j)
  464.                                 continue
  465.                             self.model.remove_block((i, y, j))
  466.                     else:
  467.                         if self.model.world[(i, y, j)] == TNT:
  468.                             self.TNTboom(i, y, j)
  469.                             continue
  470.                         self.model.remove_block((i, y, j))
  471.         for dy in range(y + 1, y + s + 1):
  472.             for i in range(x - s, x + s):
  473.                 for j in range(z - s, z + s):
  474.                     if (i, dy, j) in self.model.world:
  475.                         if j == z-s or j == z+s-1 or i == x-s or i == x+s-1:
  476.                             if random.randint(0, 1):
  477.                                 if self.model.world[(i, dy, j)] == TNT:
  478.                                     self.TNTboom(i, dy, j)
  479.                                     continue
  480.                                 if self.model.world[(i, dy, j)] != ENDSTONE:
  481.                                     self.model.remove_block((i, dy, j))
  482.                         else:
  483.                             if self.model.world[(i, dy, j)] == TNT:
  484.                                 self.TNTboom(i, dy, j)
  485.                                 continue
  486.                             if self.model.world[(i, dy, j)] != ENDSTONE:
  487.                                 self.model.remove_block((i, dy, j))
  488.             s -= 1

  489.     def on_mouse_press(self, x, y, button, modifiers):
  490.         if self.exclusive:
  491.             vector = self.get_sight_vector()
  492.             block, previous = self.model.hit_test(self.position, vector)
  493.             if (button == mouse.RIGHT) or \
  494.                     ((button == mouse.LEFT) and (modifiers & key.MOD_CTRL)):
  495.                 if previous:
  496.                     # 鼠标右击
  497.                     x, y, z = self.position
  498.                     flag = True
  499.                     for i in range(0, PLAYER_HEIGHT):
  500.                         if previous == normalize((x, y - i, z)):
  501.                             flag = False
  502.                             break
  503.                     if flag:
  504.                         self.model.add_block(previous, self.block)
  505.             elif button == pyglet.window.mouse.LEFT and block:
  506.                 # 鼠标左击
  507.                 texture = self.model.world[block]
  508.                 if texture == TNT:
  509.                     self.TNTboom(block[0], block[1], block[2])
  510.                 elif texture != ENDSTONE:
  511.                     self.model.remove_block(block)
  512.         else:
  513.             self.set_exclusive_mouse(True)

  514.     def on_mouse_motion(self, x, y, dx, dy):
  515.         if self.exclusive:
  516.             m = 0.15
  517.             x, y = self.rotation
  518.             x, y = x + dx * m, y + dy * m
  519.             y = max(-90, min(90, y))
  520.             self.rotation = (x, y)

  521.     def on_key_press(self, symbol, modifiers):
  522.         # 键盘按键
  523.         if symbol == key.W:
  524.             self.strafe[0] -= 1
  525.         elif symbol == key.S:
  526.             self.strafe[0] += 1
  527.         elif symbol == key.A:
  528.             self.strafe[1] -= 1
  529.         elif symbol == key.D:
  530.             self.strafe[1] += 1
  531.         elif symbol == key.SPACE:
  532.             self.jumping = True
  533.         elif symbol == key.R:
  534.             self.walking = not self.walking
  535.         elif symbol == key.ESCAPE:
  536.             self.set_exclusive_mouse(False)
  537.         elif symbol == key.E:
  538.             self.set_exclusive_mouse(False)
  539.         elif symbol == key.TAB:
  540.             self.flying = not self.flying
  541.         elif symbol in self.num_keys:
  542.             index = (symbol - self.num_keys[0]) % len(self.inventory)
  543.             self.block = self.inventory[index]

  544.     def on_key_release(self, symbol, modifiers):
  545.         # 键盘松键
  546.         if symbol == key.W:
  547.             self.strafe[0] += 1
  548.         elif symbol == key.S:
  549.             self.strafe[0] -= 1
  550.         elif symbol == key.A:
  551.             self.strafe[1] += 1
  552.         elif symbol == key.D:
  553.             self.strafe[1] -= 1
  554.         elif symbol == key.SPACE:
  555.             self.jumping = False

  556.     def on_resize(self, width, height):
  557.         # label
  558.         self.label.y = height - 10
  559.         # reticle
  560.         if self.reticle:
  561.             self.reticle.delete()
  562.         x, y = self.width // 2, self.height // 2
  563.         n = 10
  564.         self.reticle = pyglet.graphics.vertex_list(4,
  565.             ('v2i', (x - n, y, x + n, y, x, y - n, x, y + n))
  566.         )

  567.     def set_2d(self):
  568.         # 3d模式
  569.         width, height = self.get_size()
  570.         glDisable(GL_DEPTH_TEST)
  571.         viewport = self.get_viewport_size()
  572.         glViewport(0, 0, max(1, viewport[0]), max(1, viewport[1]))
  573.         glMatrixMode(GL_PROJECTION)
  574.         glLoadIdentity()
  575.         glOrtho(0, max(1, width), 0, max(1, height), -1, 1)
  576.         glMatrixMode(GL_MODELVIEW)
  577.         glLoadIdentity()

  578.     def set_3d(self):
  579.         # 3d模式
  580.         width, height = self.get_size()
  581.         glEnable(GL_DEPTH_TEST)
  582.         viewport = self.get_viewport_size()
  583.         glViewport(0, 0, max(1, viewport[0]), max(1, viewport[1]))
  584.         glMatrixMode(GL_PROJECTION)
  585.         glLoadIdentity()
  586.         gluPerspective(65.0, width / float(height), 0.1, 60.0)
  587.         glMatrixMode(GL_MODELVIEW)
  588.         glLoadIdentity()
  589.         x, y = self.rotation
  590.         glRotatef(x, 0, 1, 0)
  591.         glRotatef(-y, math.cos(math.radians(x)), 0, math.sin(math.radians(x)))
  592.         x, y, z = self.position
  593.         glTranslatef(-x, -y, -z)

  594.     def on_draw(self):
  595.         # 绘制
  596.         self.clear()
  597.         self.set_3d()
  598.         glColor3d(1, 1, 1)
  599.         self.model.batch.draw()
  600.         self.draw_focused_block()
  601.         self.set_2d()
  602.         self.draw_label()
  603.         self.draw_reticle()

  604.     def draw_focused_block(self):
  605.         vector = self.get_sight_vector()
  606.         block = self.model.hit_test(self.position, vector)[0]
  607.         if block:
  608.             x, y, z = block
  609.             vertex_data = cube_vertices(x, y, z, 0.51)
  610.             glColor3d(0, 0, 0)
  611.             glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
  612.             pyglet.graphics.draw(24, GL_QUADS, ('v3f/static', vertex_data))
  613.             glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)

  614.     def draw_label(self):
  615.         x, y, z = self.position
  616.         self.label.text = '%02d (%.2f, %.2f, %.2f) %d / %d' % (
  617.             pyglet.clock.get_fps(), x, y, z,
  618.             len(self.model._shown), len(self.model.world))
  619.         self.label.draw()

  620.     def draw_reticle(self):
  621.         glColor3d(0, 0, 0)
  622.         self.reticle.draw(GL_LINES)


  623. def setup_fog():
  624.     # 初始化迷雾和光照
  625.     glEnable(GL_FOG)
  626.     glFogfv(GL_FOG_COLOR, (GLfloat * 4)(0.5 - GTIME * 0.01, 0.69 - GTIME * 0.01, 1.0 - GTIME * 0.01, 1))
  627.     glHint(GL_FOG_HINT, GL_DONT_CARE)
  628.     glFogi(GL_FOG_MODE, GL_LINEAR)
  629.     glFogf(GL_FOG_START, 30.0)
  630.     glFogf(GL_FOG_END, 60.0)
  631.     glLightfv(GL_LIGHT0, GL_POSITION, (GLfloat * 4)(0.0, 0.0, 0.0, 0.0))
  632.     setup_light()

  633. def setup_light():
  634.     # 初始化光照
  635.     gamelight = 5.0 - GTIME / 10
  636.     glLightfv(GL_LIGHT0, GL_AMBIENT, (GLfloat * 4)(gamelight, gamelight, gamelight, 1.0))
  637.     glLightfv(GL_LIGHT0, GL_DIFFUSE, (GLfloat * 4)(gamelight, gamelight, gamelight, 1.0))
  638.     glLightfv(GL_LIGHT0, GL_SPECULAR, (GLfloat * 4)(1.0, 1.0, 1.0, 1.0))
  639.     glEnable(GL_LIGHTING)
  640.     glEnable(GL_LIGHT0)

  641. def setup():
  642.     # 初始化
  643.     glClearColor(0.5 - GTIME * 0.01, 0.69 - GTIME * 0.01, 1.0 - GTIME * 0.01, 1)
  644.     glEnable(GL_CULL_FACE)
  645.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
  646.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
  647.     setup_fog()

  648. def main():
  649.     window = Window(width=800, height=600, caption='Python Minecraft', resizable=True)
  650.     window.set_exclusive_mouse(True)
  651.     setup()
  652.     pyglet.app.run()

  653. if __name__ == '__main__':
  654.     main()

复制代码


我输入以上代码,发生以下报错
  1. Traceback (most recent call last):
  2.   File "D:\覃棋丰python文件\我的世界20.py", line 9, in <module>
  3.     import numba as nb
  4. ModuleNotFoundError: No module named 'numba'
复制代码
最佳答案
2024-12-5 21:42:43
你遇到的错误是因为你的系统中没有安装 `numba` 模块。要解决这个问题,你需要安装 `numba`。可以通过以下命令在命令行中安装:

  1. pip install numba
复制代码


确保你在安装时使用的是与你运行代码相同的 Python 环境。如果你使用的是虚拟环境,请先激活它。

安装完成后,再次运行你的代码,应该就不会再出现这个错误了。


以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-12-5 21:42:43 | 显示全部楼层    本楼为最佳答案   
你遇到的错误是因为你的系统中没有安装 `numba` 模块。要解决这个问题,你需要安装 `numba`。可以通过以下命令在命令行中安装:

  1. pip install numba
复制代码


确保你在安装时使用的是与你运行代码相同的 Python 环境。如果你使用的是虚拟环境,请先激活它。

安装完成后,再次运行你的代码,应该就不会再出现这个错误了。


以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-12-5 21:48:50 | 显示全部楼层
好精简的回复!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-12-8 17:59:59 | 显示全部楼层
FishC 发表于 2024-12-5 21:42
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。

我完成了如上操作,但是又发生以下报错:
  1. seed: 74583
  2. Traceback (most recent call last):
  3.   File "D:\覃棋丰python文件\我的世界20.py", line 732, in <module>
  4.     main()
  5.   File "D:\覃棋丰python文件\我的世界20.py", line 726, in main
  6.     window = Window(width=800, height=600, caption='Python Minecraft', resizable=True)
  7.   File "D:\覃棋丰python文件\我的世界20.py", line 372, in __init__
  8.     self.model = Model()
  9.   File "D:\覃棋丰python文件\我的世界20.py", line 176, in __init__
  10.     self.group = TextureGroup(image.load(TEXTURE_PATH).get_texture()) # 纹理列表
  11.   File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\pyglet\image\__init__.py", line 170, in load
  12.     return _codec_registry.decode(filename, file)
  13.   File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\pyglet\util.py", line 166, in decode
  14.     return decoder.decode(filename, file, **kwargs)
  15.   File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\pyglet\image\codecs\wic.py", line 505, in decode
  16.     file = open(filename, 'rb')
  17. FileNotFoundError: [Errno 2] No such file or directory: 'texture.png'
复制代码


代码没有改变
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-12-8 18:00:09 | 显示全部楼层
小甲鱼303 发表于 2024-12-08 17:59
我完成了如上操作,但是又发生以下报错:
seed: 7458 ...


看起来您的代码在尝试加载名为 `texture.png` 的纹理文件时出现了问题。错误信息显示该文件没有找到。以下是解决此问题的方法:

1. **检查文件路径**:确保 `texture.png` 文件确实存在于您的项目目录中。如果没有,请确认文件名和扩展名是否正确。

2. **路径问题**:如果 `texture.png` 文件不在脚本的同一目录下,请确保提供正确的文件路径。例如,将 `TEXTURE_PATH` 修改为文件的完整路径,如 `C:\\path\\to\\your\\texture.png`。

3. **文件存在性**:确认 `texture.png` 文件存在,并且可以被应用程序访问。

通过以上检查应该能解决您的问题。如果还有其他问题,请及时反馈!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-7 17:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表