当前位置:   article > 正文

pygame简单伪3d赛车赛道_scratch伪3d简单代码

scratch伪3d简单代码

转换公式
shiju = 300
self.x1 = shiju * x / z
self.y1 = shiju * y / z
# p3.append((self.x1, self.y1))
# pygame.draw.polygon(screen, (0, 244, 0), self.p)
# return (self.x1,self.y1)
参考原理
用scratch制作一个简单的3D引擎
在这里插入图片描述

import sys
import time

import numpy as np
import pygame
from pygame import surface
from pygame._sprite import Group


class lu:
    def __init__(self):
        self.x = self
        self.y = self
        self.z = self
        self.x1 = self
        self.y1 = self
        self.color = self
        self.screen = self
        self.p = self

    def zhuanhuan(self, x, y, z):
        shiju = 300
        self.x1 = shiju * x / z
        self.y1 = shiju * y / z
        # p3.append((self.x1, self.y1))
        # pygame.draw.polygon(screen, (0, 244, 0), self.p)
        # return (self.x1,self.y1)


pygame.init()  # 初始化pygame
size = width, height = 600, 600  # 设置窗口大小
screen = pygame.display.set_mode(size)  # 显示窗口
color = (255, 255, 255)
zsx = 220
zsy = 100
zenx = 70
zeny = 600
ysx = 320
ysy = 100
yenx = 570
yeny = 600

############################################
f = False
fcclock = pygame.time.Clock()
cjshuzu = []
xsshuzu = []
o = 5
gp = Group()
add = pygame.USEREVENT
pygame.time.set_timer(add, 1)
# for i in range(6):
h1 = lu()
h1.x = 0
h1.y = 0
h1.width = 662
h1.height = 35
h1.screen = screen


# h1.chuangjian()
# cjshuzu.append(h1)


def you(screen, ystx, ysty, yenx, yeny):
    hei = (0, 0, 0)
    start = (ystx, ysty)
    end = (yenx, yeny)
    width = 1222
    pygame.draw.line(screen, hei, start, end, 10)


def zuo(screen, zstx, zsty, zenx, zeny):
    hei = (0, 0, 0)
    start = (zstx, zsty)
    end = (zenx, zeny)
    width = 1262
    pygame.draw.line(screen, hei, start, end, 10)  # aaline抗锯齿


p = []
p1 = []
p2 = []
p3 = []
p4=[]
p5=[]
p6=[]
p7=[]
p8=[]
p9=[]
p10=[]
p11=[]
p12=[]
p13=[]
p14=[]
p15=[]
p16=[]
p17=[]
p18=[]
p19=[]
p20=[]
p21=[]
p22=[]

cp = []
xinp = []
cdp = []

z = [{'x1': 0, 'y1': 100, 'z1': 450}, {'x1': 940, 'y1': 100, 'z1': 450}, {'x1': 940, 'y1': 100, 'z1': 350},
     {'x1': 0, 'y1': 100, 'z1': 350, }]

def huizhi(x, y, z):
    shiju = 300
    x1 = shiju * x / z
    y1 = shiju * y / z
    # p.append((x1,y1))
    return x1, y1

while True:  # 死循环确保窗口一直显示
    ic = 1
    screen.fill(color)

    # pygame.draw.polygon(screen, (0, 244, 0), p)
    fcclock.tick(6)  # 设置图像刷新率,如果刷新率太高,图像闪的太厉害

    for event in pygame.event.get():  # 遍历所有事件
        if event.type == pygame.QUIT:  # 如果单击关闭窗口,则退出
            sys.exit()
            pygame.quit()
            exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a:

                for c in range(len(z)):
                    '''cp[c].x=cp[c].x+1
                    cp[c].zhuanhuan(cp[c].x,cp[c].y,cp[c].z)
                    p3.append((cp[c].x1,cp[c].y1))

                    print(p3)'''
                    z[c]['x1'] = z[c]['x1'] - 3
                    p3.append((huizhi(z[c]['x1'], z[c]['y1'], z[c]['z1'])))
                pygame.draw.polygon(screen, (0, 244, 0), p3)  # 需要跳出循环才能画
                pygame.display.update()
            elif event.key == pygame.K_w:
                for c in range(len(z)):
                    z[c]['z1'] = z[c]['z1'] + 30
                    z[c]['x1'] = z[c]['x1'] + 9#不加的话会向左偏移
                    p3.append((huizhi(z[c]['x1'], z[c]['y1'], z[c]['z1'])))
                pygame.draw.polygon(screen, (0, 244, 0), p3)
                pygame.display.update()
            elif event.key == pygame.K_s:
                for c in range(len(z)):
                    z[c]['z1'] = z[c]['z1'] - 30
                    z[c]['y1'] = z[c]['y1'] +30
                    z[c]['x1'] = z[c]['x1'] - 9
                    p3.append((huizhi(z[c]['x1'], z[c]['y1'], z[c]['z1'])))
                pygame.draw.polygon(screen, (0, 244, 0), p3)
                pygame.display.update()
            elif event.key == pygame.K_d:
                for c in range(len(z)):
                    # z[c]['z1'] = z[c]['z1'] - 10
                    z[c]['x1'] = z[c]['x1'] + 3
                    p3.append((huizhi(z[c]['x1'], z[c]['y1'], z[c]['z1'])))
                pygame.draw.polygon(screen, (0, 244, 0), p3)
                pygame.display.update()


    # pygame.draw.polygon(screen,(0, 144, 0),((-22,-80),(44,-80),(57-102),(-28,-102)))
    #pygame.draw.polygon(screen, (0, 144, 0), points)
    # pygame.draw.polygon(screen, (0, 244, 0), p)
    # 需要不停画,才能避免被背景覆盖,p3如果增加,会在这里画出增加后的更新图形位置
    for c in range(len(z)):

        print(z[c]['x1'])
        z[c]['x1'] = z[c]['x1']
        p3.append((huizhi(z[c]['x1'], z[c]['y1'], z[c]['z1'])))
        p4.append((huizhi(z[c]['x1'], z[c]['y1']+10, z[c]['z1']-60)))#-z是变大,+z是变小
        p5.append((huizhi(z[c]['x1'], z[c]['y1'] + 49, z[c]['z1'] - 60)))  # -z是变大,+z是变小
        p6.append((huizhi(z[c]['x1'], z[c]['y1'] + 100, z[c]['z1'] - 60)))  # -z是变大,+z是变小
        p7.append((huizhi(z[c]['x1'], z[c]['y1'] + 170, z[c]['z1'] - 60)))  # -z是变大,+z是变小
        p8.append((huizhi(z[c]['x1'], z[c]['y1'] + 265, z[c]['z1'] - 60)))  # -z是变大,+z是变小
        p9.append((huizhi(z[c]['x1'], z[c]['y1'] + 390, z[c]['z1'] - 60)))  # -z是变大,+z是变小
        p10.append((huizhi(z[c]['x1'], z[c]['y1'] + 560, z[c]['z1'] - 60)))  # -z是变大,+z是变小
        p11.append((huizhi(z[c]['x1'], z[c]['y1'] -10, z[c]['z1'] +60)))  # -z是变大,+z是变小
        p12.append((huizhi(z[c]['x1'], z[c]['y1'] - 30, z[c]['z1'] + 60)))  # -z是变大,+z是变小
        p13.append((huizhi(z[c]['x1'], z[c]['y1'] - 40, z[c]['z1'] + 60)))  # -z是变大,+z是变小
        p14.append((huizhi(z[c]['x1'], z[c]['y1'] - 50, z[c]['z1'] + 60)))  # -z是变大,+z是变小
        p16.append((huizhi(z[c]['x1'], z[c]['y1'] - 60, z[c]['z1'] + 60)))  # -z是变大,+z是变小
        p17.append((huizhi(z[c]['x1'], z[c]['y1'] - 70, z[c]['z1'] + 60)))  # -z是变大,+z是变小
        p18.append((huizhi(z[c]['x1'], z[c]['y1'] - 80, z[c]['z1'] + 60)))  # -z是变大,+z是变小
        p19.append((huizhi(z[c]['x1'], z[c]['y1'] - 90, z[c]['z1'] + 60)))  # -z是变大,+z是变小
    if (p10[2][0] > 1600):
        print("超了")
        z = [{'x1': 0, 'y1': 100, 'z1': 450}, {'x1': 1090, 'y1': 100, 'z1': 450}, {'x1': 1090, 'y1': 100, 'z1': 350},
             {'x1': 0, 'y1': 100, 'z1': 350, }]
        '''for c in range(len(z1)):
            z1[c]['x1'] = z1[c]['x1']
            p15.append((huizhi(z1[c]['x1'], z1[c]['y1'], z1[c]['z1'])))
        pygame.draw.polygon(screen, (0, 244, 0), p15)'''
    print(p10[2])
    pygame.draw.polygon(screen, (0, 244, 0), p3)
    pygame.draw.polygon(screen, (0, 144, 0), p4)
    pygame.draw.polygon(screen, (0, 244, 0), p5)
    pygame.draw.polygon(screen, (0, 144, 0), p6)
    pygame.draw.polygon(screen, (0, 244, 0), p7)
    pygame.draw.polygon(screen, (0, 144, 0), p8)
    pygame.draw.polygon(screen, (0, 244, 0), p9)
    pygame.draw.polygon(screen, (0, 144, 0), p10)
    pygame.draw.polygon(screen, (0, 144, 0), p11)
    pygame.draw.polygon(screen, (0, 244, 0), p12)
    pygame.draw.polygon(screen, (0, 144, 0), p13)
    pygame.draw.polygon(screen, (0, 244, 0), p14)
    pygame.draw.polygon(screen, (0, 144, 0), p16)
    pygame.draw.polygon(screen, (0, 244, 0), p17)
    pygame.draw.polygon(screen, (0, 144, 0), p18)
    pygame.draw.polygon(screen, (0, 244, 0), p19)
    pygame.draw.polygon(screen, (220, 224, 234), ((220,105),(330,105),(560,590),(80,590)))
    zuo(screen, zsx, zsy, zenx, zeny)
    you(screen, ysx, ysy, yenx, yeny)
    pygame.draw.rect(screen, (116, 194, 243), (0, 0, 600, 100), 0)
    pygame.display.update()
    p3.clear()
    p4.clear()
    p5.clear()
    p6.clear()
    p7.clear()
    p8.clear()
    p9.clear()
    p10.clear()
    p11.clear()
    p12.clear()
    p13.clear()
    p14.clear()
    p15.clear()
    p16.clear()
    p17.clear()
    p18.clear()
    p19.clear()
    pygame.display.flip()  # 更新全部显示

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/135096
推荐阅读
相关标签
  

闽ICP备14008679号