评论

【蓝因子教育】用Python 和 Pyglet 编写一个我的世界小游戏

开发环境

Python版本:3.7.8

相关模块:

requests模块;

tqdm模块;

pyfreeproxy模块;

pyecharts模块;

以及一些python自带的模块。

环境搭建

安装Python并添加到环境变量,pip安装需要的相关模块即可。

效果展示

游戏玩法

移动

  • W:前锋S:后退A:向左扫射D:向右扫射鼠标:环顾四周空格:跳跃标签:切换飞行模式

建筑

  • 选择要创建的块类型:
  1. 砖草沙子
  • 鼠标左键:移除方块鼠标右键:创建块

退出

ESC:释放鼠标,然后关闭窗口

代码实现

导入模块

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

人物控制

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))

# me = tex_coords((1, 0), (0, 1), (3, 0))

FACES = [

( 0, 1, 0),

( 0,-1, 0),

(-1, 0, 0),

( 1, 0, 0),

( 0, 0, 1),

( 0, 0,-1),

]

窗口类

class Window(pyglet.window.Window):

def __init__(self, *args, **kwargs):

super(Window, self).__init__(*args, **kwargs)

# Whether or not the window exclusively captures the mouse.

self.exclusive = False

# When flying gravity has no effect and speed is increased.

self.flying = False

# Strafing is moving lateral to the direction you are facing,

# e.g. moving to the left or right while continuing to face forward.

#

# First element is -1 when moving forward, 1 when moving back, and 0

# otherwise. The second element is -1 when moving left, 1 when moving

# right, and 0 otherwise.

self.strafe = [0, 0]

# Current (x, y, z) position in the world, specified with floats. Note

# that, perhaps unlike in math class, the y-axis is the vertical axis.

self.position = (0, 0, 0)

# First element is rotation of the player in the x-z plane (ground

# plane) measured from the z-axis down. The second is the rotation

# angle from the ground plane up. Rotation is in degrees.

#

# The vertical plane rotation ranges from -90 (looking straight down) to

# 90 (looking straight up). The horizontal rotation range is unbounded.

self.rotation = (0, 0)

# Which sector the player is currently in.

self.sector = None

# The crosshairs at the center of the screen.

self.reticle = None

# Velocity in the y (upward) direction.

self.dy = 0

# A list of blocks the player can place. Hit num keys to cycle.

self.inventory = [BRICK, GRASS, SAND]

# The current block the user can place. Hit num keys to cycle.

self.block = self.inventory[0]

# Convenience list of num keys.

self.num_keys = [

key._1, key._2, key._3, key._4, key._5,

key._6, key._7, key._8, key._9, key._0]

# Instance of the model that handles the world.

self.model = Model()

# The label that is displayed in the top left of the canvas.

self.label = pyglet.text.Label('', font_name='Arial', font_size=18,

x=10, y=self.height - 10, anchor_x='left', anchor_y='top',

color=(0, 0, 0, 255))

# This call schedules the `update()` method to be called

# TICKS_PER_SEC. This is the main game event loop.

pyglet.clock.schedule_interval(self.update, 1.0 / TICKS_PER_SEC)返回搜狐,查看更多

责任编辑:

平台声明:该文观点仅代表作者本人,搜狐号系信息发布平台,搜狐仅提供信息存储空间服务。
阅读 ()