(1)Python学习之Pygame初识

   本人刚入坑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.rGets or sets the red value of the Color.
pygame.Color.gGets or sets the green value of the Color.
pygame.Color.bGets or sets the blue value of the Color.
pygame.Color.aGets or sets the alpha value of the Color.
pygame.Color.cmyGets or sets the CMY representation of the Color.
pygame.Color.hsvaGets or sets the HSVA representation of the Color.
pygame.Color.hslaGets or sets the HSLA representation of the Color.
pygame.Color.i1i2i3Gets or sets the I1I2I3 representation of the Color.
pygame.Color.normalizeReturns the normalized RGBA values of the Color.
pygame.Color.correct_gammaApplies a certain gamma value to the Color.
pygame.Color.set_lengthSet 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.initInitialize the display module
pygame.display.quitUninitialize the display module
pygame.display.get_initReturns True if the display module has been initialized
pygame.display.set_modeInitialize a window or screen for display
pygame.display.get_surfaceGet a reference to the currently set display surface
pygame.display.flipUpdate the full display Surface to the screen
pygame.display.updateUpdate portions of the screen for software displays
pygame.display.get_driverGet the name of the pygame display backend
pygame.display.InfoCreate a video display information object
pygame.display.get_wm_infoGet information about the current windowing system
pygame.display.list_modesGet list of available fullscreen modes
pygame.display.mode_okPick the best color depth for a display mode
pygame.display.gl_get_attributeGet the value for an OpenGL flag for the current display
pygame.display.gl_set_attributeRequest an OpenGL display attribute for the display mode
pygame.display.get_activeReturns True when the display is active on the display
pygame.display.iconifyIconify the display surface
pygame.display.toggle_fullscreenSwitch between fullscreen and windowed displays
pygame.display.set_gammaChange the hardware gamma ramps
pygame.display.set_gamma_rampChange the hardware gamma ramps with a custom lookup
pygame.display.set_iconChange the system image for the display window
pygame.display.set_captionSet the current window caption
pygame.display.get_captionGet the current window caption
pygame.display.set_paletteSet the display color palette for indexed displays

 

3.3 image

pygame.image.loadload new image from a file
pygame.image.savesave an image to disk
pygame.image.get_extendedtest if extended image formats can be loaded
pygame.image.tostringtransfer image to string buffer
pygame.image.fromstringcreate new Surface from a string buffer
pygame.image.frombuffercreate a new Surface that shares data inside a string buffer

 

   还有很多有用的类可以使用,可以按照自己需求去官网查询,至此对于Pygame我们就有了一个较为完备的影响,在我们的大脑中已经有了大体的轮廓,那么我们与pygame的初次见面也就结束了,接下来便是我们实践动手的时候了。

                                                                                                                                                                    

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值