图形

import pygame
import math
import random

def base_game():
    pygame.init()
    window = pygame.display.set_mode((400, 600))
    pygame.display.set_caption('游戏')
    window.fill((255, 255, 255))

    # ============画图形=============
    # 1.画线
    """
    line(画在哪个地方, 线的颜色, 起点, 终点, 线宽=1)
    """
    pygame.draw.line(window, (0, 255, 0), (10, 10), (100, 100), 4)

    # 2.画线
    """
    lines(画在哪个地方, 线的颜色, 是否关闭, 点列表, 线宽=1)
    """
    point_list = [(200, 200), (250, 10), (300, 200), (150, 100), (350, 100)]
    # pygame.draw.lines(window, (255, 0, 0), True, point_list)

    # 3.画圆
    """
    circle(画在哪个地方, 线的颜色,圆心,半径, 线宽=0)
    """
    pygame.draw.circle(window, (0, 0, 0), (200, 300), 100, 0)
    pygame.draw.circle(window, (255, 0, 0), (200, 300), 90, 0)

    # 4.画多边形
    """
    polygon(画在哪个地方,线的颜色, 点列表,线宽)
    """
    pygame.draw.polygon(window, (255, 0, 0), point_list, 0)

    # 5.画弧线
    """
    arc(画在哪个地方,线的颜色, 矩形, 起始弧度, 终止弧度, 线宽)
    """
    pygame.draw.arc(window, (0, 255, 255), (10, 470, 150, 200), math.pi/4, math.pi/4*3, 5)

    # 6.画矩形
    pygame.draw.rect(window, (100, 20, 50), (200, 470, 100, 50), 2)
    window.fill((255, 255, 255))

    pygame.display.flip()
    point_list = []
    flag = False
    while True:
        # 4.不断检测游戏过程中是否有事件的产生

        for event in pygame.event.get():
            # 只有当事件产生后才会进入for循环
            # print('======')
            if event.type == pygame.QUIT:
                # return
                # 退出!
                exit()

            if event.type == pygame.MOUSEBUTTONDOWN:
                flag = True
                point_list = []
            elif event.type == pygame.MOUSEBUTTONUP:
                flag = False
                # window.fill((255, 255, 255))

            elif event.type == pygame.MOUSEMOTION:
                point_list.append(event.pos)
                # print(point_list)
                if len(point_list) < 2:
                    break
                if flag:
                    pygame.draw.lines(window, (255, 0, 0), False, point_list, 3)
                    pygame.display.flip()




def main():
    base_game()

你可能感兴趣的:(图形)