python游戏开发:初识pygame

本系列博客介绍以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,255
blue = 0,0,200
textImage = myfont.render("Hello Pygame", True, white)

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

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

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

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

import pygame
from pygame.locals import *
'''
想要学习Python?Python学习交流群:1004391443满足你的需求,资料都已经上传群文件,可以自行下载!
'''
white = 255,255,255
blue = 0,0,200

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

myfont = pygame.font.Font(None,60)
textImage = myfont.render("Hello Pygame", True, white)
13 while True:
    for event in pygame.event.get():
        if event.type in (QUIT, KEYDOWN):
            sys.exit()

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

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

2.绘制一个圆形。

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

1 color = 255,255,0
2 position = 300,250
3 radius = 100
4 width = 10
5 pygame.draw.circle(screen, color, position, radius, width)

3.绘制一个矩形。

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

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

import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600,500))
pygame.display.set_caption("Drawing Rectangles")
'''
想要学习Python?Python学习交流群:1004391443满足你的需求,资料都已经上传群文件,可以自行下载!
'''
pos_x = 300
pos_y = 250
vel_x = 2
vel_y = 1

while True:
    for event in pygame.event.get():
        if event.type in (QUIT, KEYDOWN):
            pygame.quit()
            sys.exit()

    screen.fill((0,0,200))
    
    #移动矩形
    pos_x += vel_x
    pos_y += vel_y
    
    #使矩形保持在窗口内
    if pos_x > 500 or pos_x < 0:
        vel_x = -vel_x
    if pos_y > 400 or pos_y < 0:
        vel_y = -vel_y        
    
    #绘制矩形
    color = 255,255,0
    width = 0 #solid fill
    pos = pos_x, pos_y, 100, 100
    pygame.draw.rect(screen, color, pos, width)
    
    pygame.display.update()

4.绘制线条

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

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

5.绘制弧形。

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

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

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

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

import math
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600,500))
pygame.display.set_caption("Drawing Arcs")

while True:
    for event in pygame.event.get():
        if event.type in (QUIT, KEYDOWN):
            pygame.quit()
            sys.exit()

    screen.fill((0,0,200))
    
    #绘制弧形的代码
    color = 255,0,255
    position = 200,150,200,200
    start_angle = math.radians(0)
    end_angle = math.radians(180)
    width = 8
    pygame.draw.arc(screen, color, position, start_angle, end_angle, width)
    
    pygame.display.update()

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

三、画大饼游戏。

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

import math
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600,500))
pygame.display.set_caption("The Pie Game - Press 1,2,3,4")
myfont = pygame.font.Font(None, 60)

color = 200, 80, 60
width = 4
x = 300
y = 250
radius = 200
position = x-radius, y-radius, radius*2, radius*2

piece1 = False
piece2 = False
piece3 = False
piece4 = False

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()
        elif event.type == KEYUP:
            if event.key == pygame.K_ESCAPE:
                sys.exit()
            elif event.key == pygame.K_1:
                piece1 = True
            elif event.key == pygame.K_2:
                piece2 = True
            elif event.key == pygame.K_3:
                piece3 = True
            elif event.key == pygame.K_4:
                piece4 = True
                
    #清屏
    screen.fill((0,0,200))
    
    #绘制4个数字
    textImg1 = myfont.render("1", True, color)
    screen.blit(textImg1, (x+radius/2-20, y-radius/2))
    textImg2 = myfont.render("2", True, color)
    screen.blit(textImg2, (x-radius/2, y-radius/2))
    textImg3 = myfont.render("3", True, color)
    screen.blit(textImg3, (x-radius/2, y+radius/2-20))
    textImg4 = myfont.render("4", True, color)
    screen.blit(textImg4, (x+radius/2-20, y+radius/2-20))


    #判断是否绘制饼
    if piece1:
        start_angle = math.radians(0)
        end_angle = math.radians(90)
        pygame.draw.arc(screen, color, position, start_angle, end_angle, width)
        pygame.draw.line(screen, color, (x,y), (x,y-radius), width)
        pygame.draw.line(screen, color, (x,y), (x+radius,y), width)
    if piece2:
        start_angle = math.radians(90)
        end_angle = math.radians(180)
        pygame.draw.arc(screen, color, position, start_angle, end_angle, width)
        pygame.draw.line(screen, color, (x,y), (x,y-radius), width)
        pygame.draw.line(screen, color, (x,y), (x-radius,y), width)
    if piece3:
        start_angle = math.radians(180)
        end_angle = math.radians(270)
        pygame.draw.arc(screen, color, position, start_angle, end_angle, width)
        pygame.draw.line(screen, color, (x,y), (x-radius,y), width)
        pygame.draw.line(screen, color, (x,y), (x,y+radius), width)
    if piece4:
        start_angle = math.radians(270)
        end_angle = math.radians(360)
        pygame.draw.arc(screen, color, position, start_angle, end_angle, width)
        pygame.draw.line(screen, color, (x,y), (x,y+radius), width)
        pygame.draw.line(screen, color, (x,y), (x+radius,y), width)
        
    #是否4个饼都被绘制完成
    if piece1 and piece2 and piece3 and piece4:
        color = 0,255,0

    pygame.display.update()

现在我们已经了解了一些pygame的基本操作。

猜你喜欢

转载自blog.csdn.net/qq_40925239/article/details/90381582