"""
矩阵旋转算法,这或许是真的3d?
"""
import pygame
import sys
import math
import numpy
import pygameGUI as pgui
from typing import List
from pygame.locals import *
class Sprites3D:
X, Y, Z = 0, 1, 2
def __init__(self):
self.position = [100,
100,
0,
]
def draw(self, screen):
pygame.draw.circle(screen, (255, 0, 0), [self.position[0], self.position[1]], 10)
def update(self):
angle = math.radians(10)
rz = numpy.array(
[[math.cos(angle), - math.sin(angle), 0, 0],
[math.sin(angle), math.cos(angle), 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1],
]
)
rx = numpy.array(
[
[1, 0, 0, 0],
[0, math.cos(angle), -math.sin(angle), 0],
[0, math.sin(angle), -math.sin(angle), 0],
[0, 0, 0, 1],
]
)
position = numpy.array([
[self.position[0]],
[self.position[1]],
[self.position[2]],
[1]
])
position = rz * position
self.position = [
position[0][0],
position[1][0],
position[2][0],
]
# 颜色常量
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
size = width, height = 800, 600
screen = pygame.display.set_mode(size)
pygame.display.set_caption("title")
clock = pygame.time.Clock()
delay = 60 # 延时计时器(1秒)
# 是否全屏
fullscreen = False
screen_change = False
# 背景颜色设定
bg_color = WHITE
running = True
# 3d对象
s3d = Sprites3D()
# ui======
guis = pgui.Group()
m = pgui.Message(group=guis, text=str(s3d.position[0]) + "\n" + str(s3d.position[1]) + "\n" + str(s3d.position[2]))
m.set_pos("center", [400, 200])
# =========
while running:
# 设定帧数
clock.tick(60)
# 延时计时器刷新
if delay == 0:
delay = 60
delay -= 1
# 检测是否全屏
if fullscreen and screen_change:
screen = pygame.display.set_mode(size, FULLSCREEN, HWSURFACE)
screen_change = False
elif screen_change:
screen = pygame.display.set_mode(size)
screen_change = False
# 事件检测
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
# 鼠标
if event.type == MOUSEBUTTONDOWN:
pass
# 按键按下事件
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
# F11切换全屏
if event.key == K_F11:
fullscreen = not fullscreen
screen_change = True
# 按键抬起事件
if event.type == KEYUP:
pass
# 画背景
screen.fill(bg_color)
# 刷新xxx
m.text = str(s3d.position[0]) + "\n" + str(s3d.position[1]) + "\n" + str(int(s3d.position[2]))
s3d.update()
guis.update()
# 画 xxxx
guis.draw(screen)
s3d.draw(screen)
# 刷新界面
pygame.display.update()