好文档 - 专业文书写作范文服务资料分享网站

Python之pygame的学习

天下 分享 时间: 加入收藏 我要投稿 点赞

学习pygame-2D游戏,SoEasy!

1.先安装pygame

pygame是Python的一款功能强大的库,它可以对图像、动画、文字、音频等进行操作,也经常用于2D游戏开发。

a) 使用python的pip工具: pip3 install pygame

2.第一个窗口-HelloWorld

a) 游戏窗口的创建 pygame.display.setmode(分辨率) 以下为源码: # 导入所需的模块 import pygame, sys

# 初始化pygame pygame.init()

# 设置窗口的大小,单位为像素

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

# 设置窗口标题

pygame.display.set_caption('Hello World')

# 程序主循环 while True:

# 获取事件

for event in pygame.event.get(): # 判断事件是否为退出事件 if event.type == QUIT: # 退出pygame pygame.quit() # 退出系统

sys.exit()

# 绘制屏幕内容

pygame.display.update()

3.游戏循环:

为什么会有一个while True ,主要用来:处理事件,更新游戏状态,绘制游戏内容到屏幕

4.绘制图形

Pygame的坐标原点(0,0)点位于左上角,X轴自左向右,Y轴自上向下,单位为像素。 这里介绍一下常用的方法:

pygame.draw.line(Surface, color, start_pos, end_pos, width)此方法用于绘制一条线段

pygame.draw.aaline(Surface, color, start_pos, end_pos, blend)此方法用于绘制一条抗锯齿的线 pygame.draw.lines(Surface, color, closed, pointlist, width)此方法用于绘制一条折线 pygame.draw.rect(Surface, color, Rect)此方法用于绘制一个矩形

pygame.draw.rect(Surface, color, Rect, width)此方法用于绘制一个矩形框 pygame.draw.ellipse(Surface, color, Rect)此方法用于绘制一个椭圆

pygame.draw.ellipse(Surface, color, Rect, width)此方法用于绘制一个椭圆框

pygame.draw.polygon(Surface, color, pointlist, width)此方法用于绘制一个多边形

pygame.draw.arc(Surface, color, Rect, start_angle, stop_angle, width)此方法用于绘制一条弧线 pygame.draw.circle(Surface, color, Rect, radius)此方法用于绘制一个圆

代码如下:

# 导入需要的模块 import pygame, sys

from pygame.locals import * from math import pi

# 初始化pygame pygame.init()

# 设置窗口的大小,单位为像素

screen = pygame.display.set_mode((400, 300))

# 设置窗口标题

pygame.display.set_caption('Drawing')

# 定义颜色 BLACK = (0, 0, 0)

WHITE = (255, 255, 255) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255)

# 设置背景颜色 screen.fill(WHITE)

# 绘制一条线

pygame.draw.line(screen, GREEN, [0, 0], [50, 30], 5)

# 绘制一条抗锯齿的线

pygame.draw.aaline(screen, GREEN, [0, 50], [50, 80], True)

# 绘制一条折线

pygame.draw.lines(screen, BLACK, False,

[[0, 80], [50, 90], [200, 80], [220, 30]], 5)

# 绘制一个空心矩形

pygame.draw.rect(screen, BLACK, [75, 10, 50, 20], 2)

# 绘制一个矩形

pygame.draw.rect(screen, BLACK, [150, 10, 50, 20])

# 绘制一个空心椭圆

pygame.draw.ellipse(screen, RED, [225, 10, 50, 20], 2)

# 绘制一个椭圆

pygame.draw.ellipse(screen, RED, [300, 10, 50, 20])

# 绘制多边形

pygame.draw.polygon(screen, BLACK, [[100, 100], [0, 200], [200, 200]], 5)

# 绘制多条弧线

pygame.draw.arc(screen, BLACK, [210, 75, 150, 125], 0, pi / 2, 2) pygame.draw.arc(screen, GREEN, [210, 75, 150, 125], pi / 2, pi, 2) pygame.draw.arc(screen, BLUE, [210, 75, 150, 125], pi, 3 * pi / 2, 2) pygame.draw.arc(screen, RED, [210, 75, 150, 125], 3 * pi / 2, 2 * pi, 2)

# 绘制一个圆

pygame.draw.circle(screen, BLUE, [60, 250], 40)

# 程序主循环 while True:

# 获取事件

for event in pygame.event.get(): # 判断事件是否为退出事件 if event.type == QUIT: # 退出pygame pygame.quit() # 退出系统 sys.exit()

# 绘制屏幕内容

pygame.display.update()

运行效果:

5.绘制字体

Python之pygame的学习

学习pygame-2D游戏,SoEasy!1.先安装pygamepygame是Python的一款功能强大的库,它可以对图像、动画、文字、音频等进行操作,也经常用于2D游戏开发。a)使用python的pip工具:pip3installpygame2.第一个窗口-HelloWorl
推荐度:
点击下载文档文档为doc格式
52xoe9wohp7u3cm9b9nu86wqu5roq7003av
领取福利

微信扫码领取福利

微信扫码分享