Python游戏开发-05-提高游戏颜值-04-绘制简单图形

1.绘制矩形:pygame.draw.rect()

rect(Surface, color, Rect, width=0) ->Rect

import pygame
import sys
from pygame.locals import *

#初始化Pygame
pygame.init()

#宏定义
WHITE = (255,255,255)
BLACK = (0,0,0)

size = width,height = 640,480 #实际上是元组
screen = pygame.display.set_mode(size)
pygame.display.set_caption("木尧-pygame-简单图形绘制")

clock = pygame.time.Clock()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
            
    screen.fill(WHITE)
    pygame.draw.rect(screen,BLACK,(50,50,150,50),0)
    pygame.draw.rect(screen,BLACK,(250,50,150,50),1)
    pygame.draw.rect(screen,BLACK,(450,50,150,50),10)

    #把内存中的画面翻到屏幕上
    pygame.display.flip()

    #调整帧率 1s绘制10帧
    clock.tick(10)
2.绘制多边形:pygame.draw.polygon()
polygon(Surface, color, pointlist, width=0) -> Rect

#多边形
pointlist = [(200,75),(300,25),(400,75),(450,25),(450,125),(400,75),(300,125)]
pygame.draw.polygon(screen,BLACK,pointlist,0)

3.绘制圆形:pygame.draw.circle()

circle(Surface, color, pos, radius, width=0) -> Rect

Aim:绘制一个同心圆,鼠标点击或者拖拽时他跟着动

import pygame
import sys
from pygame.locals import *

#初始化Pygame
pygame.init()

#宏定义
WHITE = (255,255,255)
BLACK = (0,0,0)

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


size = width,height = 640,480 #实际上是元组
screen = pygame.display.set_mode(size)
pygame.display.set_caption("木尧-pygame-简单图形绘制")

position = size[0]//2,size[1]//2
moving = False

clock = pygame.time.Clock()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
            
        if event.type == MOUSEBUTTONDOWN:
            if event.button == 1:
                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,25)
    pygame.draw.circle(screen,GREEN,position,75,25)
    pygame.draw.circle(screen,BLUE,position,125,25)
    
    #把内存中的画面翻到屏幕上
    pygame.display.flip()

    #调整帧率 1s绘制120帧
    clock.tick(120)
效果图:

4.绘制椭圆:pygame.draw.ellipse()

ellipse(Surface, color, Rect, width=0) -> Rect

    #椭圆
    pygame.draw.ellipse(screen,RED,(100,100,400,100),0)
    pygame.draw.ellipse(screen,RED,(100,100,300,300),2)
5.绘制弧线:pygame.draw.arc()

arc(Surface, color, Rect, start_angle, stop_angle, width=1) -> Rect

    pygame.draw.arc(screen,RED,(100,100,300,100),0,math.pi,5)
    pygame.draw.arc(screen,RED,(220,50,200,200),math.pi,math.pi*2,5)

6.绘制直线

pygame.draw.line()

pygame.draw.lines()

pygame.draw.aaline()

pygame.draw.aalines()

    pygame.draw.line(screen,GREEN,(100,100),(300,400),1)
    pygame.draw.lines(screen, BLACK, False, [[0, 80], [50, 90], [200, 80], [220, 30]], 5)
    pygame.draw.aaline(screen, GREEN, [0, 50],[50, 80], True)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值