pygame学习过程

来源:互联网 发布:js修改数据库字段的值 编辑:程序博客网 时间:2024/06/05 00:24

本系列博客介绍以python+pygame库进行小游戏的开发。有写的不对之处还望各位海涵。

一、pygame简介

 

Pygame 是一组用来开发游戏软件的 Python 程序模块,基于 SDL 库的基础上开发。允许你在 Python 程序中创建功能丰富的游戏和多媒体程序,Pygame 是一个高可移植性的模块可以支持多个操作系统。用它来开发小游戏非常适合。

可以去http://www.pygame.org/hifi.html 下载并安装使用pygame。

二、pygame使用

 

使用pygame的第一步是将pygame库导入到python程序中,以便来使用它

import pygame

然后需要引入pygame中的所有常量。

from pygame.locals import *

再经过初始化以后我们就可以尽情地使用pygame了。初始化pygame:

pygame.init()

通常来说我们需要先创建一个窗口,方便我们与程序的交互。下面创建了一个600 x 500的窗口

screen = pygame.display.set_mode((600,500))

1.打印字体

pygame支持使用pygame.font将文打印到窗口。要打印文本的话首先需要创建一个文字对象

myfont = pygame.font.Font(None,60)

这个文本绘制进程是一个重量级的进程,比较耗费时间,常用的做法是先在内存中创建文本图像,然后将文本当作一个图像来渲染。

white = 255,255,255blue = 0,0,200textImage = myfont.render("Hello Pygame", True, white)

textImage 对象可以使用screen.blit()来绘制。上面代码中的render函数第一个参数是文本,第二个参数是抗锯齿字体,第三个参数是一个颜色值(RGB值)。

要绘制本文,通常的过程是清屏,绘制,然后刷新。

screen.fill(blue)screen.blit(textImage, (100,100))pygame.display.update()

如果此时运行程序的话,会出现一个窗口一闪而过。为了让它长时间的显示,我们需要将它放在一个循环中。

复制代码
 1 import pygame 2 from pygame.locals import * 3  4 white = 255,255,255 5 blue = 0,0,200 6  7 pygame.init() 8 screen = pygame.display.set_mode((600,500)) 9 10 myfont = pygame.font.Font(None,60)11 textImage = myfont.render("Hello Pygame", True, white)1213 while True:14     for event in pygame.event.get():15         if event.type in (QUIT, KEYDOWN):16             sys.exit()17 18     screen.fill(blue)19     screen.blit(textImage, (100,100))20     pygame.display.update()
复制代码

pygame除了打印字体,还有绘制各种常见图形的常见功能。(使用pygame.draw())

2.绘制一个圆形。

使用pygame.draw.circle()方法,该方法需要传递圆的大小,颜色和位置参数。

1 color = 255,255,02 position = 300,2503 radius = 1004 width = 105 pygame.draw.circle(screen, color, position, radius, width)

3.绘制一个矩形。

为了增添一些乐趣,咱们这次绘制一个可以移动的矩形,而不只是单单的在屏幕中间绘制。

首先,需要设置pos_x, pos_y 两个变量来记录矩形的位置信息,然后在创建一对速度变量(vel_x,vel_y),在while循环内不断的更新矩形的位置。当矩形到达屏幕边缘的时候,将速度变量取反,这样就可以产生碰撞的效果了。

复制代码
 1 import pygame 2 from pygame.locals import * 3 pygame.init() 4 screen = pygame.display.set_mode((600,500)) 5 pygame.display.set_caption("Drawing Rectangles") 6  7 pos_x = 300 8 pos_y = 250 9 vel_x = 210 vel_y = 111 12 while True:13     for event in pygame.event.get():14         if event.type in (QUIT, KEYDOWN):15             pygame.quit()16             sys.exit()17 18     screen.fill((0,0,200))19     20     #移动矩形21     pos_x += vel_x22     pos_y += vel_y23     24     #使矩形保持在窗口内25     if pos_x > 500 or pos_x < 0:26         vel_x = -vel_x27     if pos_y > 400 or pos_y < 0:28         vel_y = -vel_y        29     30     #绘制矩形31     color = 255,255,032     width = 0 #solid fill33     pos = pos_x, pos_y, 100, 10034     pygame.draw.rect(screen, color, pos, width)35     36     pygame.display.update()    
复制代码

4.绘制线条

使用pygame.draw.line()方法,该方法,需要传递起始点和终点,还有线条的颜色和宽度

#绘制线条color = 255,255,0width = 8pygame.draw.line(screen, color, (100,100), (500,400), width)

5.绘制弧形。

弧形是圆的一部分,可以使用pygame.draw.arc方法来绘制它,由于这个形状比较复杂,所以它比前几个函数需要跟更多的参数。

首先,需要一个矩形来表示弧形的边界。(需提供矩形左上角的位置,宽度和高度。)弧形就绘制在这个矩形当中。

然后需要提供弧形的起始角度和结束角度。平时在生活中我们一般都是用度为单位来衡量一个角度,但是在几何三角学中,通常使用的是弧度单位。

将角度转化为弧度需要的是math.radians()方法,它包含在math库中,因此使用之前一定不要忘了先引入math库.

复制代码
 1 import math 2 import pygame 3 from pygame.locals import * 4 pygame.init() 5 screen = pygame.display.set_mode((600,500)) 6 pygame.display.set_caption("Drawing Arcs") 7  8 while True: 9     for event in pygame.event.get():10         if event.type in (QUIT, KEYDOWN):11             pygame.quit()12             sys.exit()13 14     screen.fill((0,0,200))15     16     #绘制弧形的代码17     color = 255,0,25518     position = 200,150,200,20019     start_angle = math.radians(0)20     end_angle = math.radians(180)21     width = 822     pygame.draw.arc(screen, color, position, start_angle, end_angle, width)23     24     pygame.display.update()25             
复制代码

 

最后我们通过一个非常简单的小实例来巩固和复习一下上面所学到的知识。

三、画大饼游戏。

当玩家按下1、2、3、4相应的按键时,就会在程序中绘制相应的饼块,当整个饼块都被绘制完成的时候,颜色会变为亮绿色。

复制代码
 1 import math 2 import pygame 3 from pygame.locals import * 4 pygame.init() 5 screen = pygame.display.set_mode((600,500)) 6 pygame.display.set_caption("The Pie Game - Press 1,2,3,4") 7 myfont = pygame.font.Font(None, 60) 8  9 color = 200, 80, 6010 width = 411 x = 30012 y = 25013 radius = 20014 position = x-radius, y-radius, radius*2, radius*215 16 piece1 = False17 piece2 = False18 piece3 = False19 piece4 = False20 21 while True:22     for event in pygame.event.get():23         if event.type == QUIT:24             exit()25         elif event.type == KEYUP:26             if event.key == pygame.K_ESCAPE:27                 sys.exit()28             elif event.key == pygame.K_1:29                 piece1 = True30             elif event.key == pygame.K_2:31                 piece2 = True32             elif event.key == pygame.K_3:33                 piece3 = True34             elif event.key == pygame.K_4:35                 piece4 = True36                 37     #清屏38     screen.fill((0,0,200))39     40     #绘制4个数字41     textImg1 = myfont.render("1", True, color)42     screen.blit(textImg1, (x+radius/2-20, y-radius/2))43     textImg2 = myfont.render("2", True, color)44     screen.blit(textImg2, (x-radius/2, y-radius/2))45     textImg3 = myfont.render("3", True, color)46     screen.blit(textImg3, (x-radius/2, y+radius/2-20))47     textImg4 = myfont.render("4", True, color)48     screen.blit(textImg4, (x+radius/2-20, y+radius/2-20))49 50 51     #判断是否绘制饼52     if piece1:53         start_angle = math.radians(0)54         end_angle = math.radians(90)55         pygame.draw.arc(screen, color, position, start_angle, end_angle, width)56         pygame.draw.line(screen, color, (x,y), (x,y-radius), width)57         pygame.draw.line(screen, color, (x,y), (x+radius,y), width)58     if piece2:59         start_angle = math.radians(90)60         end_angle = math.radians(180)61         pygame.draw.arc(screen, color, position, start_angle, end_angle, width)62         pygame.draw.line(screen, color, (x,y), (x,y-radius), width)63         pygame.draw.line(screen, color, (x,y), (x-radius,y), width)64     if piece3:65         start_angle = math.radians(180)66         end_angle = math.radians(270)67         pygame.draw.arc(screen, color, position, start_angle, end_angle, width)68         pygame.draw.line(screen, color, (x,y), (x-radius,y), width)69         pygame.draw.line(screen, color, (x,y), (x,y+radius), width)70     if piece4:71         start_angle = math.radians(270)72         end_angle = math.radians(360)73         pygame.draw.arc(screen, color, position, start_angle, end_angle, width)74         pygame.draw.line(screen, color, (x,y), (x,y+radius), width)75         pygame.draw.line(screen, color, (x,y), (x+radius,y), width)76         77     #是否4个饼都被绘制完成78     if piece1 and piece2 and piece3 and piece4:79         color = 0,255,080 81     pygame.display.update()
复制代码

 

现在我们已经了解了一些pygame的基本操作,下个博客我们将会一起学习pygame中的IO、数据相关知识。

本文为转载文章,不承担任何责任!
0 0
原创粉丝点击