Skip to content

Instantly share code, notes, and snippets.

@xtai
Last active August 29, 2015 14:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xtai/a810748938e9b6cbc9e0 to your computer and use it in GitHub Desktop.
Save xtai/a810748938e9b6cbc9e0 to your computer and use it in GitHub Desktop.
day.py
# Daylight & Moonlight
# by Xiaoyu Tai
#
# Mountains with rotating Sun and Moon.
# plus sky colors~
from pyglet.gl import *
from math import *
window = pyglet.window.Window(1000, 640)
mountains_color = [[0.1,0.6,0.4, 0.1,0.6,0.4, 0.1,0.6,0.4],
[0.1,0.7,0.5, 0.1,0.7,0.5, 0.1,0.7,0.5],
[0.2,0.8,0.6, 0.2,0.8,0.6, 0.2,0.8,0.6]]
@window.event
def on_draw():
global angle
glClear(pyglet.gl.GL_COLOR_BUFFER_BIT)
glLoadIdentity()
draw_background()
draw_sun([1, 0.1, 0.1], angle)
draw_moon([0.95, 0.95, 0.95], angle - 180)
draw_moutain(400, 1.75, 1)
draw_moutain(760, 1.675, 0)
draw_moutain(650, 2, 2)
draw_moutain(900, 1.25, 1)
draw_moutain(200, 1.5, 1)
draw_moutain(80, 1, 0)
draw_moutain(320, 2, 2)
draw_moutain(550, 2.5, 0)
def draw_background():
global angle
glPushMatrix()
color = [1,0.6,0.2, 0.3,0.4,0.5, 0.3,0.4,0.5, 1,0.6,0.2]
if angle>90 and angle<=180:
color = [
1,1-(0.4*(angle-90)/90),1-(0.8*(angle-90)/90),
0.6-(0.3*(angle-90)/90),0.65-(0.25*(angle-90)/90),0.95-(0.45*(angle-90)/90),
0.6-(0.3*(angle-90)/90),0.65-(0.25*(angle-90)/90),0.95-(0.45*(angle-90)/90),
1,1-(0.4*(angle-90)/90),1-(0.8*(angle-90)/90)]
if angle>0 and angle<=90:
color = [
1,0.6+(0.4*angle/90),0.2+(0.8*angle/90),
0.3+(0.3*angle/90),0.4+(0.25*angle/90),0.5+(0.45*angle/90),
0.3+(0.3*angle/90),0.4+(0.25*angle/90),0.5+(0.45*angle/90),
1,0.6+(0.4*angle/90),0.2+(0.8*angle/90)]
if angle>-90 and angle<=0:
color = [
0.1+0.9*((angle+90)/90),0.15+0.45*((angle+90)/90),0.35-0.15*((angle+90)/90),
0.3*((angle+90)/90),0.4*((angle+90)/90),0.5*((angle+90)/90),
0.3*((angle+90)/90),0.4*((angle+90)/90),0.5*((angle+90)/90),
0.1+0.9*((angle+90)/90),0.15+0.45*((angle+90)/90),0.35-0.15*((angle+90)/90)]
if angle>-180 and angle<=-90:
color = [
1-0.9*((angle+180)/90),0.6-0.45*((angle+180)/90),0.2+0.15*((angle+180)/90),
0.3-0.3*((angle+180)/90),0.4-0.4*((angle+180)/90),0.5-0.5*((angle+180)/90),
0.3-0.3*((angle+180)/90),0.4-0.4*((angle+180)/90),0.5-0.5*((angle+180)/90),
1-0.9*((angle+180)/90),0.6-0.45*((angle+180)/90),0.2+0.15*((angle+180)/90)]
background = pyglet.graphics.vertex_list(4, ('v2f', [0,0, 0,640, 1000,640, 1000,0]), ('c3f', color))
background.draw(GL_TRIANGLE_FAN)
glPopMatrix()
def draw_moutain(x, scale, color):
global mountains_color
glPushMatrix()
glTranslatef(x, 0, 0)
glScalef(scale, scale, 1)
glTranslatef(-70, 0, 0)
glColor3f(mountains_color[color][0],mountains_color[color][1],mountains_color[color][2])
glBegin(GL_TRIANGLES)
glVertex2f(0, 0)
glVertex2f(70, 140)
glVertex2f(140, 0)
glEnd()
glPopMatrix()
def draw_sun(color, angle):
glPushMatrix()
glTranslatef(500, 50, 0)
glRotatef(angle, 0, 0, 1)
glTranslatef(450, 0, 0)
draw_circle(45, color)
glPopMatrix()
def draw_moon(color, angle):
glPushMatrix()
glTranslatef(500, 50, 0)
glRotatef(angle, 0, 0, 1)
glTranslatef(450, 0, 0)
glRotatef(-angle - 20, 0, 0, 1)
glScalef(45, 45, 1)
glColor3f(color[0],color[1],color[2])
points = [1, 0]
for i in xrange(0, 49):
angle = (i / 98.0) * pi * 2
x, y = sin(angle), cos(angle)
points += [x,y]
x, y = sin(angle) * 0.4, cos(angle)
points += [x,y]
circle = pyglet.graphics.vertex_list(99, ('v2f', points))
circle.draw(GL_TRIANGLE_STRIP)
glPopMatrix()
def draw_circle(scale, color):
glPushMatrix()
glScalef(scale,scale,1)
glColor3f(color[0],color[1],color[2])
points = [0,0]
for i in xrange(0, 99):
angle = (i / 98.0) * pi * 2
x, y = sin(angle), cos(angle)
points += [x,y]
circle = pyglet.graphics.vertex_list(100, ('v2f', points))
circle.draw(GL_TRIANGLE_FAN)
glPopMatrix()
# def cloud_A(x, y, size):
# circle(x+(3*size), y+(3*size), (3*size))
# circle(x+(9*size), y+(5*size), (5*size))
# circle(x+(14*size), y+(4*size), (4*size))
# circle(x+(18*size), y+(2*size), (2*size))
# cloud = pyglet.graphics.vertex_list(6, ('v2f', [x+(3*size),y,
# x+(3*size),y+(3*size),
# x+(9*size),y+(5*size),
# x+(14*size),y+(4*size),
# x+(18*size),y+(2*size),
# x+(18*size),y]))
# cloud.draw(GL_TRIANGLE_FAN)
# def cloud_B(x, y, size):
# circle(x+(3*size), y+(3*size), (3*size))
# circle(x+(10*size), y+(6*size), (6*size))
# circle(x+(16*size), y+(4*size), (4*size))
# cloud = pyglet.graphics.vertex_list(5, ('v2f', [x+(3*size),y,
# x+(3*size),y+(3*size),
# x+(10*size),y+(5*size),
# x+(16*size),y+(4*size),
# x+(16*size),y]))
# cloud.draw(GL_TRIANGLE_FAN)
angle = 180
time = 4.5
count = 0
def update(dt):
global angle, time, count
if count == 0:
angle = 90 + (time * time * time)
else:
angle = -90 + (time * time * time)
time -= 0.05
if time < -4.5:
time = 4.5
count += 1
if count == 2:
count = 0
pyglet.clock.schedule_interval(update, 1 / 60.0)
pyglet.app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment