某一天,我突然喜欢上了3D,在一些scratch教程中见过一些3D引擎,找了一个简单的,结果z轴太大了,于是网上一搜,就发现了pyglet

还是先讲如何启动一个窗口

先看看官网:

Creating a window
If the Window constructor is called with no arguments, defaults will be assumed for all parameters: window = pyglet.window.Window()
The default parameters used are: The window will have a size of 640x480, and not be resizable.
A default context will be created using template config described in OpenGL configuration options.
The window caption will be the name of the executing Python script (i.e., sys.argv[0]).
Windows are visible as soon as they are created, unless you give the visible=False argument to the constructor. The following example shows how to create and display a window in two steps: window = pyglet.window.Window(visible=False)
# ... perform some additional initialisation
window.set_visible()
Context configuration
The context of a window cannot be changed once created. There are several ways to control the context that is created: Supply an already-created Context using the context argument: context = config.create_context(share)
window = pyglet.window.Window(context=context)
Supply a complete Config obtained from a Screen using the config argument. The context will be created from this config and will share object space with the most recently created existing context: config = screen.get_best_config(template)
window = pyglet.window.Window(config=config)
Supply a template Config using the config argument. The context will use the best config obtained from the default screen of the default display: config = gl.Config(double_buffer=True)
window = pyglet.window.Window(config=config)
Specify a Screen using the screen argument. The context will use a config created from default template configuration and this screen: screen = display.get_screens()[screen_number]
window = pyglet.window.Window(screen=screen)
Specify a Display using the display argument. The default screen on this display will be used to obtain a context using the default template configuration: display = platform.get_display(display_name)
window = pyglet.window.Window(display=display)
If a template Config is given, a Screen or Display may also be specified; however any other combination of parameters overconstrains the configuration and some parameters will be ignored.

总结了就是

import pyglet as p
p.window.Window(600,600)
p.app.run()

效果:

然后画个正方形

import pyglet as p
from pyglet.gl import *
win=p.window.Window(600,600)
@win.event
def on_draw():
#(
win.clear()
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(90, 1, 0.1, 100)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
#)
pos=[0,0,-20]
glTranslatef(*pos)
glBegin(GL_POLYGON)
glVertex3f(-5,-5,0)
glVertex3f(5,-5,0)
glVertex3f(5,5,0)
glVertex3f(-5,5,0)
glEnd() glFlush()
p.app.run()

#(  和  #)中间的是固定的,不能改

glTranslatef(*pos):摄像头位置
glBegin(GL_POLYGON):开始绘制多边形

    glVertex3f(-5,-5,0)
glVertex3f(5,-5,0)
glVertex3f(5,5,0)
glVertex3f(-5,5,0):给出所有坐标点,绘制图形
glEnd():结束绘制当前图形
glFlush():刷新界面

都包含在pyglet.gl里,可使用
from pyglet.gl import *

 效果:

这就是本篇文章的内容

最后留个彩蛋

import pyglet
from pyglet.gl import * pos = [0, 0, -20]
rot_y = 0
rot_x=0
rot_z=0
config = Config(sample_buffers=1, samples=8)
tela = pyglet.window.Window(height=500, width=500, #config=config)
)
mode=True
dotlst=[[[-5,-5,0],[5,-5,0],[5,5,0],[-5,5,0]]]
input_=""
@tela.event
def on_draw(): global pos_z, rot_y,rot_z,rot_X tela.clear() glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(90, 1, 0.1, 100)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity() glTranslatef(*pos)
glRotatef(rot_y, 0, 1, 0)
glRotatef(rot_z,0,0,1)
glRotatef(rot_x,1,0,0)
glBegin(GL_POLYGON)
glVertex3f(-5,-5,0)
glVertex3f(5,-5,0)
glVertex3f(5,5,0)
glVertex3f(-5,5,0)
glEnd() glFlush() @tela.event
def on_key_press(s,m): global pos_z, rot_y,rot_z,rot_x
if s == pyglet.window.key.W:
pos[2] -= 1
if s == pyglet.window.key.S:
pos[2] += 1
if s == pyglet.window.key.A:
pos[0]-=1
if s == pyglet.window.key.D:
pos[0] += 1
if s == pyglet.window.key.R:
pos[1]-=1
if s == pyglet.window.key.F:
pos[1] += 1
if s == pyglet.window.key.I:
rot_z -= 5
if s == pyglet.window.key.K:
rot_z += 5
if s == pyglet.window.key.J:
rot_x-=5
if s == pyglet.window.key.L:
rot_x+= 5
if s == pyglet.window.key.O:
rot_y-=5
if s == pyglet.window.key.P:
rot_y += 5 pyglet.app.run()

  

WSADRFJKLIOP操作

原理下次讲

后面的出了会写在最下面的。

下一讲

Python:pyglet学习(1):想弄点3D,还发现了pyglet的更多相关文章

  1. 开始写下自己的python的cocos2d, pyglet学习

    开始写下自己的python的cocos2d, pyglet学习 2014年01月18日 13:52:36 我要做程序达人 阅读数 9051更多 分类专栏: python的cocos2d和pyglet ...

  2. caffe的python接口学习(4):mnist实例---手写数字识别

    深度学习的第一个实例一般都是mnist,只要这个例子完全弄懂了,其它的就是举一反三的事了.由于篇幅原因,本文不具体介绍配置文件里面每个参数的具体函义,如果想弄明白的,请参看我以前的博文: 数据层及参数 ...

  3. Python Tutorial 学习(八)--Errors and Exceptions

    Python Tutorial 学习(八)--Errors and Exceptions恢复 Errors and Exceptions 错误与异常 此前,我们还没有开始着眼于错误信息.不过如果你是一 ...

  4. Day1 Python基础学习

    一.编程语言分类 1.简介 机器语言:站在计算机的角度,说计算机能听懂的语言,那就是直接用二进制编程,直接操作硬件 汇编语言:站在计算机的角度,简写的英文标识符取代二进制去编写程序,本质仍然是直接操作 ...

  5. Day1 Python基础学习——概述、基本数据类型、流程控制

    一.Python基础学习 一.编程语言分类 1.简介 机器语言:站在计算机的角度,说计算机能听懂的语言,那就是直接用二进制编程,直接操作硬件 汇编语言:站在计算机的角度,简写的英文标识符取代二进制去编 ...

  6. python爬虫学习(1) —— 从urllib说起

    0. 前言 如果你从来没有接触过爬虫,刚开始的时候可能会有些许吃力 因为我不会从头到尾把所有知识点都说一遍,很多文章主要是记录我自己写的一些爬虫 所以建议先学习一下cuiqingcai大神的 Pyth ...

  7. Python正则表达式学习摘要及资料

    摘要 在正则表达式中,如果直接给出字符,就是精确匹配. {m,n}? 对于前一个字符重复 m 到 n 次,并且取尽可能少的情况 在字符串'aaaaaa'中,a{2,4} 会匹配 4 个 a,但 a{2 ...

  8. webdriver(python)学习笔记一

    最近有python开发的项目,也正打算要学习自动化与python语言.因此想通过学习python版本的webdriver来一同学习. 学习过程中参考资料有乙醇的博客:https://github.co ...

  9. !!对python列表学习整理列表及数组详细介绍

    1.Python的数组分三种类型:(详细见 http://blog.sina.com.cn/s/blog_6b783cbd0100q2ba.html) (1) list 普通的链表,初始化后可以通过特 ...

随机推荐

  1. CSS Modules 的六种用法

    一.局部作用域 二.全局作用域 三.定制哈希类名 四. Class 的组合 五.输入其他模块 六.输入变量

  2. Java 中使用正则表达式校检IP是否输入正确

    感谢大佬案例:https://www.jb51.net/article/114671.htm 正则表达式学习:(待办)近期总结

  3. java中的成员变量和局部变量的区别

    成员变量: 在类体里面定义的变量叫做成员变量: 如果在变量有static关键字修饰,就叫作静态变量或类变量: 如果该变量没有static关键字修饰,就叫作非静态变量或实例变量: 局部变量: 方法内定义 ...

  4. iOS中JavaScript和OC交互 --by 胡 xu

    在iOS开发中很多时候我们会和UIWebView打交道,目前国内的很多应用都采用了UIWebView的混合编程技术,最常见的是微信公众号的内容页面.前段时间在做微信公众平台相关的开发,发现很多应用场景 ...

  5. 无脑安装——Python 及 安装python集成开发环境pycharm

    无脑安装--Python 及安装python集成开发环境pycharm 1.真机安装python 2.安装python集成开发环境pycharm Python 是一种解释型语言 Python 是面向对 ...

  6. 面试题之java缓存总结,从单机缓存到分布式缓存架构

    1.缓存定义 高速数据存储层,提高程序性能 2.为什么要用缓存(读多写少,高并发) 1.提高读取吞吐量 2.提升应用程序性能 3.降低数据库成本 4.减少后端负载 5.消除数据库热点 6.可预测的性能 ...

  7. MySQL保留2位小数

    1.round(x,d),四舍五入.round(x) ,其实就是round(x,0),也就是默认d为0 select round(110.35,1); # 110.4 2.TRUNCATE(x,d), ...

  8. 基于Xilinx XCKU115的半高PCIe x8 硬件加速卡

    一.板卡概述 本板卡系我公司自主研发,采用Xilinx公司的XCKU115-3-FLVF1924-E芯片作为主处理器,主要用于FPGA硬件加速.板卡设计满足工业级要求.如下图所示: 二.功能和技术指标 ...

  9. 磁盘分区 & Linux 三剑客之 awk

    今日内容 磁盘分区 Linux 三剑客之 awk 内容详细 一.磁盘分区 磁盘分区 --> 挂载 步骤 1.关机 2.添加硬盘 3.创建分区 fdisk /dev/sdb or gdisk /d ...

  10. Spring Boot数据访问之整合Mybatis

    在Mybatis整合Spring - 池塘里洗澡的鸭子 - 博客园 (cnblogs.com)中谈到了Spring和Mybatis整合需要整合的点在哪些方面,需要将Mybatis中数据库连接池等相关对 ...