python中的pygame小练习(2)基本图形绘制

基本图形绘制

绘制矩形

pygame.draw.rect(screen,BLACK,(50, 50, 150, 50), 0)
#Surface :矩形绘制在哪个Surface对象上
# color:边框颜色
# Rect:指定矩形范围,就是四个左边点的位置
# width: 指定边框大小,0表示填充这个矩形,1表示边框一个像素

绘制多边形
polygon(Surface, color, pointlist, width=0)
#Surface :矩形绘制在哪个Surface对象上
# color:边框颜色
# Rect:指定矩形范围,就是顶点的位置
# width: 指定边框大小,0表示填充这个矩形,1表示边框一个像素
例:

    import pygame
import sys
from pygame.locals import *
pygame.init()
GREEN = (0, 255, 0)
points = [(200, 75), (300, 25), (400, 75), (450, 25), (450, 125), (400, 75), (300, 125)]

size = width, height = 640, 480
screen = pygame.display.set_mode(size)
pygame.display.set_caption("FIshC Demo")
clock = pygame.time.Clock()

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
    screen.fill(WHITE)
    pygame.draw.polygon(screen, GREEN,points, 0)
    #把内存中的画面翻转到屏幕上
    pygame.display.flip()
    #帧率 一秒10帧
    clock.tick(10)

结果:
在这里插入图片描述

circle(Surface, color, pos, radius, width=0)

pos:圆心的位置
radius:半径的大小

import pygame
import sys
from pygame.locals import *
pygame.init()

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

size = width, height = 640, 480
screen = pygame.display.set_mode(size)
pygame.display.set_caption("FIshC Demo")

position = size[0]//2, size[1]//2 #两个除号表示整除

clock = pygame.time.Clock()
moving = False
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
        if event.type == MOUSEBUTTONDOWN:
            if  event.button == 1: #1鼠标左键2鼠标滚轮3鼠标右键4滚轮向上5滚轮向下
                moving = True
        if event.type  == MOUSEBUTTONUP:
            if event.button == 1:
                moving = False

    if moving:
        position = pygame.mouse.get_pos()  #获得鼠标当前位置

    screen.fill(WHITE)
    pygame.draw.circle(screen, RED,position, 25, 1)
    pygame.draw.circle(screen, BLUE, position, 75, 1)
    pygame.draw.circle(screen, GREEN, position, 125, 1)
    #把内存中的画面翻转到屏幕上
    pygame.display.flip()
    #帧率 一秒10帧
    clock.tick(10)

结果:鼠标点击会随鼠标移动
在这里插入图片描述
绘制椭圆
ellipse(Surface, color, Rect, width=0)
Rect是一个矩形,如果椭圆限制在一个长方形内就是椭圆,如果限制在一个正方形内,就是正圆。

import pygame
import sys
from pygame.locals import *
pygame.init()

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

size = width, height = 640, 480
screen = pygame.display.set_mode(size)
pygame.display.set_caption("FIshC Demo")

clock = pygame.time.Clock()
moving = False
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
    screen.fill(WHITE)
    pygame.draw.ellipse(screen, BLACK, (100, 100, 400,  100) , 1)
    pygame.draw.ellipse(screen, BLACK, (200, 50, 200, 200), 1)
    pygame.display.flip()
    #帧率 一秒10帧
    clock.tick(10)

在这里插入图片描述
绘制弧线
act(Surface, color,Rect, start_angle, stop_angle, width=1)
start_angle:表示从椭圆的哪一部分角度开始
stop_angle:表示从椭圆的哪一部分角度结束

例:画了个死鱼眼

import pygame
import sys
import math
from pygame.locals import *
pygame.init()

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


size = width, height = 640, 480
screen = pygame.display.set_mode(size)
pygame.display.set_caption("FIshC Demo")


clock = pygame.time.Clock()
moving = False
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()


    screen.fill(WHITE)

    pygame.draw.arc(screen, BLACK, (100, 100, 400,  100) , 0, math.pi, 1)
    pygame.draw.arc(screen, BLACK, (200, 50, 200, 200),math.pi, math.pi * 2, 1)
    pygame.draw.arc(screen, BLACK, (100, 200, 400, 100), math.pi, math.pi * 2, 1)
    pygame.display.flip()
    #帧率 一秒10帧
    clock.tick(10)

在这里插入图片描述
画线段,一条线和多条线

line(Surface, color, start_pos, end_pos, width=1)

lines(Surface, color, closed, pointlist, width=1)

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
#模块导入 import pygame,sys from pygame.locals import* #初始化pygame pygame.init() #设置窗口大小,单位是像素 screen = pygame.display.set_mode((500,400)) #设置背景颜色 screen.fill((0,0,0)) #设置窗口标题 pygame.display.set_caption("你好,我的朋友") # 绘制一条线 pygame.draw.rect(screen, (0,0,0), [0,100,70,40]) #加载图片 img = pygame.image.load("panda.jpg") #初始化图片位置 imgx = 0 imgy = 10 #加载和播放音频 sound = pygame.mixer.Sound('Sound_Of_The_Sea.ogg') sound.play() #加载背景音乐 pygame.mixer.music.load('TEST1.mp3') #播放背景音乐,第一个参数为播放的次数(-1表示无限循环),第二个参数是设置播放的起点(单位为秒) pygame.mixer.music.play(-1, 30.0) #导入文字格式 fontt=pygame.font.Font(None,50) #配置文字 tex=fontt.render("It is boring!!!",True,(0,0,128),(0,255,0)) #显示文字及坐标 texr=tex.get_rect() texr.center=(10,250) #初始化方向 dire = "right" #设置循环 while 1: #绘制文字 screen.blit(tex,texr) screen.fill((0,0,0)) screen.blit(img,(imgx,imgy)) if dire == "right": imgx+=5 if imgx == 380: dire = 'down' elif dire == 'down': imgy += 5 if imgy == 300: dire = 'left' elif dire == 'left': imgx -= 5 if imgx == 10: dire = 'up' elif dire == 'up': imgy -= 5 if imgy == 10: dire = 'right' #获取事件 for ss in pygame.event.get(): #判断事件 if ss.type == QUIT: #退出Pygame pygame.quit() #退出系统 sys.exit() #绘制屏幕内容 pygame.display.update() #设置帧率 pygame.time.delay(10)

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值