Skip to content

Instantly share code, notes, and snippets.

@SuperDoxin
Created April 22, 2015 12:08
Show Gist options
  • Save SuperDoxin/d9054018fdc9961cfa54 to your computer and use it in GitHub Desktop.
Save SuperDoxin/d9054018fdc9961cfa54 to your computer and use it in GitHub Desktop.
seasick
from __future__ import division
from collections import namedtuple
import pygame
import math
import numpy as np
W,H=640,480#screen resolution
DIST=10#distance of the horizon
Y_SHIFT=200#how much to shift the plane down
Y_UNDU=100#how much to undulate the plane up and down
WOBBLE=math.pi/16#how much to wobble
WAVE=math.pi/16#how much to wobble on the z axis
XLINE_STEP=150#how distant the lines are on the X axis
YLINE_STEP=1#how distant the lines are on the y axis
SPEED=0.1
mult=max(W,H)
HW,HZ=int(W//2),int(H//2)
Point=namedtuple("Point",["x","y","z"])
def unity():
return np.array([
[1,0,0,0],
[0,1,0,0],
[0,0,1,0],
[0,0,0,1]
])
def rotz(v):
c=math.cos
s=math.sin
return np.array([
[c(v),-s(v),0,0],
[s(v),c(v),0,0],
[0,0,1,0],
[0,0,0,1]
])
def project(oldpoint,matrix=None):
if matrix==None:
oldpoint=Point(*oldpoint)
else:
arr=np.array([list(oldpoint)+[0]]).transpose()
op=matrix.dot(arr)
oldpoint=Point(*op[:-1])
return Point(oldpoint.x/oldpoint.z+HW,oldpoint.y/oldpoint.z+HZ,oldpoint.z)
def line3d(surface,color,start,end,matrix=None):
start=project(start,matrix)
end=project(end,matrix)
pygame.draw.line(surface,color,start[:-1],end[:-1])
pygame.init()
screen=pygame.display.set_mode((W,H))
clock=pygame.time.Clock()
framecount=0
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.display.quit()
raise SystemExit()
screen.fill((0,0,0))
matrix=rotz(math.sin(framecount/7)*WOBBLE)
y=Y_SHIFT+math.sin(framecount/11)*Y_UNDU
line3d(screen,(255,255,255),(-W*DIST,y,DIST),(W*DIST,y,DIST),matrix)
for z in range(DIST*100,1,-YLINE_STEP*100):
z=(z/100)-(framecount*SPEED)%YLINE_STEP
line3d(screen,(255,255,255),(-W*DIST,y,z),(W*DIST,y,z),matrix)
for x in range(int(-W*DIST),int(W*DIST),XLINE_STEP):
line3d(screen,(255,255,255),(x,y,DIST),((x,y,0.1)),matrix)
pygame.display.flip()
framecount+=1
clock.tick(30)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment