collab/PineapplePad
git clone https://git.julia.blue/collab/PineapplePad
7214f4a
master
1import pygame 2import math 3import time 4import random 5 6fps = 20 7clk = pygame .time .Clock () 8BLACK = (0 ,0 ,0 ) 9WHITE = (255 ,255 ,255 ) 10BLUE = (0 ,0 ,255 ) 11GREEN = (0 ,255 ,0 ) 12RED = (255 ,0 ,0 ) 13 14 15class v2 (): 16def __init__ (self ,x ,y ): 17self .x = float (x ) 18self .y = float (y ) 19 20def __str__ (self ): 21return "<" + str (self .x )+ "," + str (self .y )+ ">" 22 23 24class v3 (): 25def __init__ (self ,x ,y ,z ): 26self .x = float (x ) 27self .y = float (y ) 28self .z = float (z ) 29 30# s is size of viewing plane a is angle of fov 31def project (self ,s ,a ): 32return \ 33v2 (s .x / 2 + s .x * self .x / (s .x + 2 * self .z * math .tan (a .x )), 34s .y / 2 + s .y * self .y / (s .y + 2 * self .z * math .tan (a .y ))) 35 36def __str__ (self ): 37return "<" + str (self .x )+ "," + str (self .y )+ "," + str (self .z )+ ">" 38 39 40size = v2 (160 ,128 ) 41angle = v2 (math .radians (30 ),math .radians (20 )) 42 43pygame .init () 44pygame .display .set_caption ("Cubefield Graphics Testing" ) 45screen = pygame .display .set_mode ((int (size .x ),int (size .y ))) 46running = True 47 48 49def drawPixel (p ,color = WHITE ): 50screen .set_at ((int (p .x ),int (p .y )),color ) 51 52 53class edge (): 54def __init__ (self ,p1 ,p2 ): 55self .p1 = p1 56self .p2 = p2 57 58def draw (self ,color = GREEN ): 59pygame .draw .line ( 60screen ,color , (int ( 61self .p1 .x ),int ( 62self .p1 .y )), (int ( 63self .p2 .x ),int ( 64self .p2 .y )),1 ) 65 66 67class cube (): 68def __init__ (self ,pos ,size ): 69self .pos = pos 70self .size = size 71self .computePoints () 72 73def computePoints (self ): 74self .p000 = self .pos 75self .p001 = v3 ( 76self .pos .x , 77self .pos .y , 78self .pos .z + 79self .size ) 80self .p010 = v3 ( 81self .pos .x , 82self .pos .y + 83self .size , 84self .pos .z ) 85self .p011 = v3 ( 86self .pos .x , 87self .pos .y + 88self .size , 89self .pos .z + 90self .size ) 91self .p100 = v3 ( 92self .pos .x + 93self .size , 94self .pos .y , 95self .pos .z ) 96self .p101 = v3 ( 97self .pos .x + 98self .size , 99self .pos .y , 100self .pos .z + 101self .size ) 102self .p110 = v3 ( 103self .pos .x + 104self .size , 105self .pos .y + 106self .size , 107self .pos .z ) 108self .p111 = v3 ( 109self .pos .x + 110self .size , 111self .pos .y + 112self .size , 113self .pos .z + 114self .size ) 115 116def renderWireframe (self ): 117self .computePoints () 118topLeft = edge ( 119self .p010 .project ( 120size ,angle ),self .p011 .project ( 121size ,angle )) 122topRight = edge ( 123self .p110 .project ( 124size ,angle ),self .p111 .project ( 125size ,angle )) 126bottomLeft = edge ( 127self .p000 .project ( 128size ,angle ),self .p001 .project ( 129size ,angle )) 130bottomRight = edge ( 131self .p100 .project ( 132size ,angle ),self .p101 .project ( 133size ,angle )) 134topFront = edge ( 135self .p010 .project ( 136size ,angle ),self .p110 .project ( 137size ,angle )) 138topBack = edge ( 139self .p011 .project ( 140size ,angle ),self .p111 .project ( 141size ,angle )) 142bottomFront = edge ( 143self .p000 .project ( 144size ,angle ),self .p100 .project ( 145size ,angle )) 146bottomBack = edge ( 147self .p001 .project ( 148size ,angle ),self .p101 .project ( 149size ,angle )) 150frontLeft = edge ( 151self .p000 .project ( 152size ,angle ),self .p010 .project ( 153size ,angle )) 154frontRight = edge ( 155self .p100 .project ( 156size ,angle ),self .p110 .project ( 157size ,angle )) 158backLeft = edge ( 159self .p001 .project ( 160size ,angle ),self .p011 .project ( 161size ,angle )) 162backRight = edge ( 163self .p101 .project ( 164size ,angle ),self .p111 .project ( 165size ,angle )) 166topFront .draw () 167# topBack.draw() 168bottomFront .draw () 169bottomBack .draw () 170bottomRight .draw () 171bottomLeft .draw () 172frontLeft .draw () 173frontRight .draw () 174if self .pos .x > 0 : 175backLeft .draw () 176topLeft .draw () 177if self .pos .x + self .size < 0 : 178backRight .draw () 179topRight .draw () 180 181 182def drawTri (p1 ,p2 ,p3 ,color = WHITE ): 183pass 184 185 186def drawQuad (p1 ,p2 ,p3 ,p4 ,color = WHITE ): 187pass 188 189 190numCubes = 40 191cubeSize = 12.5 192speed = 6 193cubes = [] 194for i in range (numCubes ): 195cubes .append (cube (v3 (random .random ()* 500 - 250 , 19620 ,i * 2000 / numCubes + random .random ()* 50 - 25 ), 197cubeSize )) 198 199vel = 0 200score = 0 201while running : 202screen .fill (BLACK ) 203keys = pygame .key .get_pressed () 204for event in pygame .event .get (): 205if event .type == pygame .QUIT : 206running = False 207 208if (keys [pygame .K_q ]): 209running = False 210 211if (keys [pygame .K_a ]): 212if vel < 0 : 213vel = 0 214vel = min (speed , (vel + .5 )* 1.25 ) 215if (keys [pygame .K_d ]): 216if vel > 0 : 217vel = 0 218vel = max (- speed , (vel - .5 )* 1.25 ) 219if not (keys [pygame .K_d ]or keys [pygame .K_a ]): 220vel /= 1.25 221if vel < .1 : 222vel = 0 223 224for c in cubes : 225c .pos .x += vel 226c .pos .z -= speed 227if c .pos .z < - 100 : 228score += 1 229c .pos .x = random .random ()* 500 - 250 230c .pos .z = 500 231elif c .pos .z < - 100 + cubeSize and c .pos .x < 0 and c .pos .x + c .size > 0 : 232"You Lose. Score:" ,score 233running = False 234c .renderWireframe () 235 236pygame .draw .polygon (screen ,WHITE , [[75 ,125 ], [80 ,120 ], [85 ,125 ]]) 237 238pygame .transform .flip (screen ,False ,False ) 239pygame .display .flip () 240clk .tick (fps )