初识pygame

一、pygame简介

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

二、pygame使用

# 1.使用pygame的第一步是将pygame库导入到python程序中,以便来使用它
import pygame
# 2.然后需要引入pygame中的所有常量
from pygame.locals import *
# 3.再经过初始化以后我们就可以尽情地使用pygame了。初始化pygame:
pygame.init()
# 4.通常来说我们需要先创建一个窗口,方便我们与程序的交互。下面创建了一个600 x 500的窗口
screen = pygame.display.set_mode((600,500))


1.打印字体

    附加一个小知识点:关于RGB
Q:为什么将RGB(0,0,0)定义为黑色而不是白色?
A:可以将颜色理解为光线的强弱
太阳光最强约等于白色,可以从太阳光折射出七彩
RGB(0,0,0)表示黑色
RGB(255,255,255)表示白色
pygame支持使用pygame.font将文打印到窗口。要打印文本的话首先需要创建一个文字对象

myfont = pygame.font.Font(None,60)
  • 这个文本绘制进程是一个重量级的进程,比较耗费时间
  • 常用的做法是先在内存中创建文本图像然后将文本当作一个图像来渲染
white = 255,255,255
blue = 0,0,200
textImage = myfont.render("Hello Pygame", True, white)
'''上面代码中的render函数第一个参数是文本,第二个参数是抗锯齿字体,第三个参数是一个颜色值(RGB值)'''
  • render()函数用于创建一个文本图像
  • textImage 对象可以使用screen.blit()来绘制

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

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

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

import sys

import pygame
from pygame.locals import *

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)
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.绘制一个圆形

'''
添加如下代码
color = 255,255,0
position = 300,250
radius = 100
width = 10
pygame.draw.circle(screen, color, position, radius, width)
'''
import pygame
import sys
from pygame.locals import *

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)

color = 255,255,0
position = 300,250
radius = 100
width = 10

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.draw.circle(screen, color, position, radius, width)
    pygame.display.update()

在这里插入图片描述

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")

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)
'''
import pygame
import sys
from pygame.locals import *

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)

color = 255,255,0
width = 8

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.draw.line(screen, color, (100,100), (500,400), width)
    pygame.display.update()

在这里插入图片描述

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()

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值