pygame伪3d 地面旋转

import cv2
import pygame
import sys
from pygame import gfxdraw
import numpy as np

(width, height) = (800, 600)
pygame.init()
screen = pygame.display.set_mode((width, height))
image = pygame.image.load("11.jpg").convert()
d3=pygame.surfarray.array3d(image)
f=250#数字大会显示更前面的图片
sc=300#数字越大视角越高
color=(255, 255, 255)
#print(len(d3[0][0]))#len(d3 )就是有多少张表 一个[0]是有多高 两个是每个表有几列 三个就是具体数
#print(d3[0][0][0])
#print(d3)
#sarray=pygame.surfarray.array3d(pygame.surface((50,50)))
sarray=pygame.surfarray.array3d(pygame.Surface((width,height)))
#sarray[:]=(255,255,0)变成黄色,替换所有为(255,255,0)

tsize=image.get_size()#图片宽高
print(tsize)
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            mouse = pygame.mouse.get_pos()
            '''pxarray = pygame.PixelArray(screensurf)
            pixel = pygame.Color(pxarray[mouse[0], mouse[1]])#获取像素
            print (pixel)
            print (screensurf.get_at(mouse))#颜色
            print(mouse[0], mouse[1],mouse)#点击坐标
            print(img.get_at(mouse))#颜色'''
            #pygame.Surface.set_at(screen, (140, 140), (img.get_at(mouse)))
            print(mouse[0], mouse[1], mouse)  # 点击坐标
    time=pygame.time.get_ticks()*0.01#获取时间,毫秒
    pos=np.array([time,0])
    print(pos[0],pos[1])
    angle=np.sin(time*0.3)
    sin=np.sin(angle)
    cos=np.cos(angle)
    for i in range(width):
        for j in range(int(height/2),height):
            x=(width/2)-i
            y=j+f
            z=j-(height/2)+0.01
            '''
            def huizhi(x, y, z):
            shiju = 300
            x1 = shiju * x / z
            y1 = shiju * y / z
            # p.append((x1,y1))
            return x1, y1
            '''
            rx=(x*cos-y*sin)
            ry=(x*sin+y*cos)
            px=(rx/z+pos[1])*sc
            py=(ry/z+pos[0])*sc
            '''
            px=(x/z+pos[1])*sc
            py=(y/z-pos[0])*sc#+pos是向后,-是向前
            横着走,向右
            px=(x/z+pos[0])*sc
            py=(y/z+pos[1])*sc
            '''
            fp=int(px%tsize[0]),int(py%tsize[1])#tsize【0】,【1】是图片宽高
            fc=d3[fp]
            sarray[i,j]=fc#-i让图片旋转180,-y让上方同样显示图片,达到天空的目的
    pygame.surfarray.blit_array(screen,sarray)
    pygame.display.update()


你可能感兴趣的:(pygame,3d,python)