最新pygame学习(二)——绘制线条、圆、矩形等图案_pygame(2)

2、绘制网格

通过循环绘制一个网格大小为三十像素的网络

import pygame #导包
from pygame.locals import*
import sys

black=0,0,0
lightgreen=144,238,144

screen_width=600
screen_height=600

pygame.init() #初始化
screen = pygame.display.set_mode(size=(screen_width,screen_height),flags=0)
#绘制一个600*600的框框

# 设置网格大小
grid_size = 30  # 每个网格的大小为30个像素

# 绘制网格
for i in range(0, screen_width, grid_size):
    pygame.draw.line(screen, lightgreen, (i, 0), (i, screen_height))
for i in range(0, screen_height, grid_size):
    pygame.draw.line(screen, lightgreen, (0, i), (screen_width, i))

while True:
    for event in pygame.event.get():
        if event.type in (QUIT,KEYDOWN):
            sys.exit()#python的退出程序

    linewidth=4
    pygame.display.update()  # 刷新展示


3、绘制圆

pygame.draw.circle(screen,lightgreen,position,radius,linewidth)

通过pygame.draw.circle()可以画圆

  • screen代表屏幕
  • lightgreen代表着圆的颜色
  • position代表着圆心所在的位置
  • radius代表着圆的半径
  • linewidth代表着线的粗细
import pygame #导包
from pygame.locals import*
import sys

black=0,0,0
lightgreen=144,238,144

screen_width=600
screen_height=600

pygame.init() #初始化
screen = pygame.display.set_mode(size=(screen_width,screen_height),flags=0)
#绘制一个600*600的框框


# 主循环
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    color = 255,255,255
    position = 300,250
    radius = 100
    width = 10
    linewidth=4
    screen.fill(color)
    pygame.draw.circle(screen,lightgreen,position,radius,linewidth)

    pygame.display.update()  # 刷新展示


4、绘制矩形

**pygame.draw.rect()**是pygame库中的一个函数,用于在给定的屏幕上绘制一个矩形。

函数的参数解释如下:

  • screen: 这是你要在其上绘制矩形的pygame Surface 对象。
  • lightgreen: 这是矩形的颜色。它是一个包含RGB值的元组,例如 (144, 238, 144)。
  • pos: 这是矩形左上角的坐标 + 以左上标点为原点的右下角坐标,下面给出了具体例子。
  • width: 这是矩形的宽度。这是一个整数。
import pygame #导包
from pygame.locals import*
import sys

screen_width=600
screen_height=600
pygame.init() #初始化
screen = pygame.display.set_mode(size=(screen_width,screen_height))
pygame.display.set_caption("这是标题")
pos_x = 300
pos_y =300

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    lightgreen=144,238,144
    width = 0
    pos = pos_x, pos_y, 300, 300
    pygame.draw.rect(screen, lightgreen, pos, width)#300*300的矩形
    pygame.display.update()

绘制右下角的矩形区域:

5、绘制一个会动的矩形

设置屏幕的宽度和高度为600x600像素。 定义矩形的横向和纵向移动速度为0.14像素/帧。  每次循环将背景填充为黑色。 根据速度更新矩形的位置。 当矩形超出屏幕边界时,改变其速度的方向,使其反向移动。 使用pygame.draw.rect()方法绘制矩形,设置颜色为黄色,宽度为0(实心矩形)。

import pygame #导包
from pygame.locals import*
import sys

screen_width=600
screen_height=600
pygame.init() #初始化
screen = pygame.display.set_mode(size=(screen_width,screen_height))
pygame.display.set_caption("这是标题")
pos_x = 300
pos_y =300
vel_x = 0.14
vel_y = 0.1#粗略滴可以看作矩形的移动速度
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    screen.fill((0,0,0))#每次循环都要将背景置为黑色
    pos_x += vel_x
    pos_y += vel_y
    #当矩形超过屏幕范围后返回
    if pos_x > 500 or pos_x < 0:
        vel_x = -vel_x
    if pos_y > 500 or pos_y < 0:
        vel_y = -vel_y
#绘制矩形
    color = 255, 255, 1
    width = 0
    pos = pos_x,pos_y, 100, 100
    pygame.draw.rect(screen,color,pos,width)
    pygame.display.update()

test2.5

6、还有哪些常用的绘制图形方法?

Pygame 提供了多种绘制图形的方法,这些方法主要用于在 Surface 对象上绘制基本的几何形状。以下是一些常用的 Pygame 绘制图形的方法:

学习路线:

这个方向初期比较容易入门一些,掌握一些基本技术,拿起各种现成的工具就可以开黑了。不过,要想从脚本小子变成黑客大神,这个方向越往后,需要学习和掌握的东西就会越来越多以下是网络渗透需要学习的内容:
在这里插入图片描述

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以点击这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 22
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值