Python我的世界小游戏源代码 您所在的位置:网站首页 pythonmod代表什么 Python我的世界小游戏源代码

Python我的世界小游戏源代码

2024-06-02 10:45| 来源: 网络整理| 查看: 265

我的世界小游戏使用方法:

移动

前进:W,后退:S,向左:A,向右:D,环顾四周:鼠标,跳起:空格键,切换飞行模式:Tab;

选择建筑材料

砖:1,草:2,沙子:3,删除建筑:鼠标左键单击,创建建筑块:鼠标右键单击

ESC退出程序。

完整程序包请通过文末地址下载,程序运行截图如下: 在这里插入图片描述

''' 公众号:Python代码大全 ''' from __future__ import division import sys import math import random import time from collections import deque from pyglet import image from pyglet.gl import * from pyglet.graphics import TextureGroup from pyglet.window import key, mouse TICKS_PER_SEC = 60 # Size of sectors used to ease block loading. SECTOR_SIZE = 16 WALKING_SPEED = 5 FLYING_SPEED = 15 GRAVITY = 20.0 MAX_JUMP_HEIGHT = 1.0 # About the height of a block. # To derive the formula for calculating jump speed, first solve # v_t = v_0 + a * t # for the time at which you achieve maximum height, where a is the acceleration # due to gravity and v_t = 0. This gives: # t = - v_0 / a # Use t and the desired MAX_JUMP_HEIGHT to solve for v_0 (jump speed) in # s = s_0 + v_0 * t + (a * t^2) / 2 JUMP_SPEED = math.sqrt(2 * GRAVITY * MAX_JUMP_HEIGHT) TERMINAL_VELOCITY = 50 PLAYER_HEIGHT = 2 if sys.version_info[0] >= 3: xrange = range def cube_vertices(x, y, z, n): """ Return the vertices of the cube at position x, y, z with size 2*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, # 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=4): """ Return the bounding vertices of the texture square. """ 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): """ Return a list of the texture squares for the top, bottom and 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)) FACES = [ ( 0, 1, 0), ( 0,-1, 0), (-1, 0, 0), ( 1, 0, 0), ( 0, 0, 1), ( 0, 0,-1), ] def normalize(position): """ Accepts `position` of arbitrary precision and returns the block containing that position. Parameters ---------- position : tuple of len 3 Returns ------- block_position : tuple of ints of len 3 """ x, y, z = position x, y, z = (int(round(x)), int(round(y)), int(round(z))) return (x, y, z) def sectorize(position): """ Returns a tuple representing the sector for the given `position`. Parameters ---------- position : tuple of len 3 Returns ------- sector : tuple of len 3 """ x, y, z = normalize(position) x, y, z = x // SECTOR_SIZE, y // SECTOR_SIZE, z // SECTOR_SIZE return (x, 0, z) class Model(object): def __init__(self): # A Batch is a collection of vertex lists for batched rendering. self.batch = pyglet.graphics.Batch() # A TextureGroup manages an OpenGL texture. self.group = TextureGroup(image.load(TEXTURE_PATH).get_texture()) # A mapping from position to the texture of the block at that position. # This defines all the blocks that are currently in the world. self.world = {} # Same mapping as `world` but only contains blocks that are shown. self.shown = {} # Mapping from position to a pyglet `VertextList` for all shown blocks. self._shown = {} # Mapping from sector to a list of positions inside that sector. self.sectors = {} # Simple function queue implementation. The queue is populated with # _show_block() and _hide_block() calls self.queue = deque() self._initialize() def _initialize(self): """ Initialize the world by placing all the blocks. """ n = 80 # 1/2 width and height of world s = 1 # step size y = 0 # initial y height for x in xrange(-n, n + 1, s): for z in xrange(-n, n + 1, s): # create a layer stone an grass everywhere. 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): # create outer walls. for dy in xrange(-2, 3): self.add_block((x, y + dy, z), STONE, immediate=False) # generate the hills randomly o = n - 10 for _ in xrange(120): a = random.randint(-o, o) # x position of the hill b = random.randint(-o, o) # z position of the hill c = -1 # base of the hill h = random.randint(1, 6) # height of the hill s = random.randint(4, 8) # 2 * s is the side length of the hill d = 1 # how quickly to taper off the hills t = random.choice([GRASS, SAND, BRICK]) for y in xrange(c, c + h): for x in xrange(a - s, a + s + 1): for z in xrange(b - s, b + s + 1): if (x - a) ** 2 + (z - b) ** 2 > (s + 1) ** 2: continue if (x - 0) ** 2 + (z - 0) ** 2 (pad + 1) ** 2: continue if before: x, y, z = before before_set.add((x + dx, y + dy, z + dz)) if after: x, y, z = after after_set.add((x + dx, y + dy, z + dz)) show = after_set - before_set hide = before_set - after_set for sector in show: self.show_sector(sector) for sector in hide: self.hide_sector(sector) def _enqueue(self, func, *args): """ Add `func` to the internal queue. """ self.queue.append((func, args)) def _dequeue(self): """ Pop the top function from the internal queue and call it. """ func, args = self.queue.popleft() func(*args) def process_queue(self): """ Process the entire queue while taking periodic breaks. This allows the game loop to run smoothly. The queue contains calls to _show_block() and _hide_block() so this method should be called if add_block() or remove_block() was called with immediate=False """ start = time.perf_counter() while self.queue and time.time()- start 0: # Moving backwards. dy *= -1 # When you are flying up or down, you have less left and right # motion. dx = math.cos(x_angle) * m dz = math.sin(x_angle) * m else: dy = 0.0 dx = math.cos(x_angle) dz = math.sin(x_angle) else: dy = 0.0 dx = 0.0 dz = 0.0 return (dx, dy, dz) def update(self, dt): """ This method is scheduled to be called repeatedly by the pyglet clock. Parameters ---------- dt : float The change in time since the last call. """ self.model.process_queue() sector = sectorize(self.position) if sector != self.sector: self.model.change_sectors(self.sector, sector) if self.sector is None: self.model.process_entire_queue() self.sector = sector m = 8 dt = min(dt, 0.2) for _ in xrange(m): self._update(dt / m) def _update(self, dt): """ Private implementation of the `update()` method. This is where most of the motion logic lives, along with gravity and collision detection. Parameters ---------- dt : float The change in time since the last call. """ # walking speed = FLYING_SPEED if self.flying else WALKING_SPEED d = dt * speed # distance covered this tick. dx, dy, dz = self.get_motion_vector() # New position in space, before accounting for gravity. dx, dy, dz = dx * d, dy * d, dz * d # gravity if not self.flying: # Update your vertical speed: if you are falling, speed up until you # hit terminal velocity; if you are jumping, slow down until you # start falling. self.dy -= dt * GRAVITY self.dy = max(self.dy, -TERMINAL_VELOCITY) dy += self.dy * dt # collisions x, y, z = self.position x, y, z = self.collide((x + dx, y + dy, z + dz), PLAYER_HEIGHT) self.position = (x, y, z) def collide(self, position, height): """ Checks to see if the player at the given `position` and `height` is colliding with any blocks in the world. Parameters ---------- position : tuple of len 3 The (x, y, z) position to check for collisions at. height : int or float The height of the player. Returns ------- position : tuple of len 3 The new position of the player taking into account collisions. """ # How much overlap with a dimension of a surrounding block you need to # have to count as a collision. If 0, touching terrain at all counts as # a collision. If .49, you sink into the ground, as if walking through # tall grass. If >= .5, you'll fall through the ground. pad = 0.25 p = list(position) np = normalize(position) for face in FACES: # check all surrounding blocks for i in xrange(3): # check each dimension independently if not face[i]: continue # How much overlap you have with this dimension. d = (p[i] - np[i]) * face[i] if d


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有