Python教程

python pygame 立体图形——翻转

本文主要是介绍python pygame 立体图形——翻转,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
import sys,math
import pygame
#使用pygame之前必须初始化
pygame.init()
#设置主屏窗口
screen = pygame.display.set_mode((800,600))

#设置窗口标题
pygame.display.set_caption('')
# source = pygame.Surface((50, 50), flags=pygame.HWSURFACE)
# 如果没有下列主循环代码,运行结果会一闪而过

# x1, y1 = 200, 200
# x2, y2 = 500, 200
# x3, y3 = 150, 400
# x4, y4 = 450, 400
#
# x5,y5 = 200,100
# x6,y6 = 500,100
# x7,y7 = 150,200
# x8,y8 = 450,200

x0,y0 = 300,300
x10,y10 = 300,150

angle1 = 45
angle2 = 135
angle3 = 225
angle4 = 315


def count(angle,length):
    # y = y2-y1
    y = math.sin(math.pi * (angle / 180)) * length
    y2 = int(y + y0)
    # x = x2-x1
    x = math.cos(math.pi * (angle / 180)) * length
    x2 = int(x + x0)

    return x2,y2
def count1(angle,length):
    # y = y2-y1
    y = math.sin(math.pi * (angle / 180)) * length
    y2 = int(y + y10)
    # x = x2-x1
    x = math.cos(math.pi * (angle / 180)) * length
    x2 = int(x + x10)

    return x2,y2
a = True
# 更新屏幕内容
pygame.display.flip()
x1, y1 = count(angle1, 50)
x2, y2 = count(angle2, 50)
x3, y3 = count(angle3, 50)
x4, y4 = count(angle4, 50)

x11, y11 = count1(angle1, 50)
x22, y22 = count1(angle2, 50)
x33, y33 = count1(angle3, 50)
x44, y44 = count1(angle4, 50)
while True:


    # #填充主窗口的背景颜色,参数值RGB(颜色元组)
    screen.fill((255, 255, 255))
    # 循环获取事件,监听事件
    for event in pygame.event.get():
        # 判断用户是否点了关闭按钮
        if event.type == pygame.QUIT:
            #卸载所有模块
            pygame.quit()
            #终止程序
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                print('右')
                angle1 -= 10
                angle2 -= 10
                angle3 -= 10
                angle4 -= 10
                x1, y1 = count(angle1, 50)
                x2, y2 = count(angle2, 50)
                x3, y3 = count(angle3, 50)
                x4, y4 = count(angle4, 50)

                x11, y11 = count1(angle1, 50)
                x22, y22 = count1(angle2, 50)
                x33, y33 = count1(angle3, 50)
                x44, y44 = count1(angle4, 50)
            elif event.key == pygame.K_LEFT:
                print('左')
                angle1 += 10
                angle2 += 10
                angle3 += 10
                angle4 += 10
                x1, y1 = count(angle1, 50)
                x2, y2 = count(angle2, 50)
                x3, y3 = count(angle3, 50)
                x4, y4 = count(angle4, 50)

                x11, y11 = count1(angle1, 50)
                x22, y22 = count1(angle2, 50)
                x33, y33 = count1(angle3, 50)
                x44, y44 = count1(angle4, 50)
            elif event.key == pygame.K_UP:
                print('上')
                y0 -= 5
                y1 -= 10
                y2 -= 10

                y10 -= 5
                y11 -= 10
                y22 -= 10


            elif event.key == pygame.K_DOWN:
                print('下')
                y0  += 5
                y1  += 10
                y2  += 10

                y10 += 5
                y11 += 10
                y22 += 10

    def rect():
        pygame.draw.line(screen, (1, 1, 1), (x0,y0),(x1,y1), 2)
        pygame.draw.line(screen, (1, 1, 1), (x0,y0),(x2,y2), 2)
        pygame.draw.line(screen, (1, 1, 1), (x0,y0),(x3,y3), 2)
        pygame.draw.line(screen, (1, 1, 1), (x0,y0),(x4,y4), 2)

        pygame.draw.line(screen, (1, 1, 1), (x1,y1),(x2,y2), 2)
        pygame.draw.line(screen, (1, 1, 1), (x2,y2),(x3,y3), 2)
        pygame.draw.line(screen, (1, 1, 1), (x3,y3),(x4,y4), 2)
        pygame.draw.line(screen, (1, 1, 1), (x4,y4),(x1,y1), 2)
    rect()

    def rect():
        pygame.draw.line(screen, (1, 1, 1), (x10,y10),(x11, y11), 2)
        pygame.draw.line(screen, (1, 1, 1), (x10,y10),(x22, y22), 2)
        pygame.draw.line(screen, (1, 1, 1), (x10,y10),(x33, y33), 2)
        pygame.draw.line(screen, (1, 1, 1), (x10,y10),(x44, y44), 2)

        pygame.draw.line(screen, (1, 1, 1), (x11, y11),(x22, y22), 2)
        pygame.draw.line(screen, (1, 1, 1), (x22, y22),(x33, y33), 2)
        pygame.draw.line(screen, (1, 1, 1), (x33, y33),(x44, y44), 2)
        pygame.draw.line(screen, (1, 1, 1), (x44, y44),(x11, y11), 2)
    rect()
    def Vertical_line():#竖线
        pygame.draw.line(screen, (1, 1, 1), (x11, y11), (x1,y1), 2)
        pygame.draw.line(screen, (1, 1, 1), (x22, y22), (x2,y2), 2)
        pygame.draw.line(screen, (1, 1, 1), (x33, y33), (x3,y3), 2)
        pygame.draw.line(screen, (1, 1, 1), (x44, y44), (x4,y4), 2)
    Vertical_line()
    def parallelogram(): #平行四边形
        pygame.draw.line(screen, (1, 1, 1), (x1, y1), (x2, y2), 2)
        pygame.draw.line(screen, (1, 1, 1), (x2, y2), (x4, y4), 2)
        pygame.draw.line(screen, (1, 1, 1), (x3, y3), (x4, y4), 2)
        pygame.draw.line(screen, (1, 1, 1), (x1, y1), (x3, y3), 2)

    # parallelogram()

    screen.blit(screen, (0, 0))
    # 定义频率
    clock = pygame.time.Clock()
    # 设定刷新帧率
    clock.tick(60) # 越大刷新的越快
    pygame.display.update()
这篇关于python pygame 立体图形——翻转的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!