本人刚入坑Python,有C#的经验看着Python的语法书很是想要睡觉,索性就直接上手,照着书上的《星球大战》写写,以下是我对于Python的初识,纯给自己做个笔记,也希望给予初学者一些帮助。
一.安装Pygame
首先就是Pygame的安装,我现在用的Microsoft Windows,对于Linux/OS X下的安装只能用pip,但Windows下的安装可以灵活一点,我们可以直接去Bitbucket下载,如果在Bitbucket上找不到合适的安装程序,可以去这里看看,其中不单单有关于pygame的,还有很多关于Python和其他一些功能库的结合的应用。由于我是64位的系统而且我电脑上安装的是Python3.7,所以我选择了以下版本的Pygame。
我这个是.WHL各式的文件,还需要通过pip来安装,首先cmd,cd到要安装文件的目录下,然后用pip install指令来安装如下所示:
这样我们就成功的安装了Pygame,在程序中加入import pygame查看是否报错,如果没有,恭喜你安装成功了!
二.关于Pygame
稍微的扯一点点关于Pygame的,看它的名字你可能就知道他是由Python以及game组成的,更深的说就是Pygame是Python和SDL的结合,是Python抓取SDL库的Python的延升库。SLD是一款直接媒体孵化器,完全是基于C语言来写的,不利于应用,于是Pygame的创始人Pete Shinners将轻便直接快捷的Python语言与SDL相结合,希望做一些复杂的事情能够尽量的简单快捷,2000年的10月Pygame项目开始了,6个月后Pygame1.0版本问世。
三.Pygame的一些简单应用
首先是Pygame的基本结构:
import sys
import pygame//导入sys以及pygame模块
def run_game()://定义一个运行游戏的函数
pygame.init()//pygame模块的初始化
screen=pygame.display.set_mode((100,80))//设置所打开屏幕窗口大小
pygame.display.set_caption("Alien Invasion")//设置窗口标题
while True: //循环检测键盘鼠标的输入事件以及刷新整个屏幕
for event in pygame.event.get()://检测外部输入
if event.type==pygame.QUIT:
sys.exit() //点窗口的X关闭系统
pygame.display.flip()//显示窗口
run_game()//运行这个函数
以上是pygame的基本结构。接下来我们去pygame的官网看看pygame库的函数,为接下的实践做准备(pygame官网文档)
3.1 color
pygame.Color.r | — | Gets or sets the red value of the Color. |
pygame.Color.g | — | Gets or sets the green value of the Color. |
pygame.Color.b | — | Gets or sets the blue value of the Color. |
pygame.Color.a | — | Gets or sets the alpha value of the Color. |
pygame.Color.cmy | — | Gets or sets the CMY representation of the Color. |
pygame.Color.hsva | — | Gets or sets the HSVA representation of the Color. |
pygame.Color.hsla | — | Gets or sets the HSLA representation of the Color. |
pygame.Color.i1i2i3 | — | Gets or sets the I1I2I3 representation of the Color. |
pygame.Color.normalize | — | Returns the normalized RGBA values of the Color. |
pygame.Color.correct_gamma | — | Applies a certain gamma value to the Color. |
pygame.Color.set_length | — | Set the number of elements in the Color to 1,2,3, or 4. |
Pygame主要的是以RGB的值来代表颜色的,就是由红色,绿色,蓝色来组成颜色,然后每个颜色可能的值范围为0-255,当然还有cmy,hsva,hsla,i1i2i3这样的代表颜色的方式,其中pygame.Color.a是字母的颜色的意思,pygame.Color.correct_gamma这个方法可以用来做颜色的伽马校正。
3.2 display
pygame.display.init | — | Initialize the display module |
pygame.display.quit | — | Uninitialize the display module |
pygame.display.get_init | — | Returns True if the display module has been initialized |
pygame.display.set_mode | — | Initialize a window or screen for display |
pygame.display.get_surface | — | Get a reference to the currently set display surface |
pygame.display.flip | — | Update the full display Surface to the screen |
pygame.display.update | — | Update portions of the screen for software displays |
pygame.display.get_driver | — | Get the name of the pygame display backend |
pygame.display.Info | — | Create a video display information object |
pygame.display.get_wm_info | — | Get information about the current windowing system |
pygame.display.list_modes | — | Get list of available fullscreen modes |
pygame.display.mode_ok | — | Pick the best color depth for a display mode |
pygame.display.gl_get_attribute | — | Get the value for an OpenGL flag for the current display |
pygame.display.gl_set_attribute | — | Request an OpenGL display attribute for the display mode |
pygame.display.get_active | — | Returns True when the display is active on the display |
pygame.display.iconify | — | Iconify the display surface |
pygame.display.toggle_fullscreen | — | Switch between fullscreen and windowed displays |
pygame.display.set_gamma | — | Change the hardware gamma ramps |
pygame.display.set_gamma_ramp | — | Change the hardware gamma ramps with a custom lookup |
pygame.display.set_icon | — | Change the system image for the display window |
pygame.display.set_caption | — | Set the current window caption |
pygame.display.get_caption | — | Get the current window caption |
pygame.display.set_palette | — | Set the display color palette for indexed displays |
3.3 image
pygame.image.load | — | load new image from a file |
pygame.image.save | — | save an image to disk |
pygame.image.get_extended | — | test if extended image formats can be loaded |
pygame.image.tostring | — | transfer image to string buffer |
pygame.image.fromstring | — | create new Surface from a string buffer |
pygame.image.frombuffer | — | create a new Surface that shares data inside a string buffer |
还有很多有用的类可以使用,可以按照自己需求去官网查询,至此对于Pygame我们就有了一个较为完备的影响,在我们的大脑中已经有了大体的轮廓,那么我们与pygame的初次见面也就结束了,接下来便是我们实践动手的时候了。