3 # game
.io
- A simple ship flying around a window
5 # Updated by Steve Dekorte
6 # Significant cleanup
and simplification by Jonathan Wright
- Oct
2006
11 Ship
:= Object clone do(
17 newSlot("thrust", 0.05)
19 newSlot("turningLeft", false
)
20 newSlot("turningRight", false
)
21 newSlot("thrusting", false
)
26 # Reuse the vectors so we don
't put presure on the garbage collector
27 location = vector(0, 0, 0)
28 velocity = vector(0, 0, 0)
29 impulse = vector(0, 0, 0)
36 glRotated(angle, 0, 0, 1)
41 # nice anti-aliased outline with rounded corners due to the
42 # glEnable(GL_LINE_SMOOTH) and glLineWidth(2)
68 angle = if(angle < 0, angle + 360, angle % 360)
71 a := angle * (Number constants pi / 180)
72 velocity += impulse set(a sin * thrust, a cos * -thrust, 0)
80 centerOnScreen := method(
81 location = screenSize / 2
84 wrapLocation := method(
85 if (location x > screenSize x, location setX(0))
86 if (location x < 0, location setX(screenSize x))
88 if (location y > screenSize y, location setY(0))
89 if (location y < 0, location setY(screenSize y))
93 Screen := Object clone do(
96 newSlot("size") # set by the reshape callback
101 ship = Ship clone setScreenSize(size)
104 width := method(size x)
105 height := method(size y)
107 docSlot("reshape", "Reshape callback for glut.")
108 reshape := method(w, h,
110 scale = vector(2,2) / size
114 glViewport(0, 0, w, h)
115 glMatrixMode(GL_PROJECTION)
117 gluOrtho2D(-1, -1, 1, 1)
118 glMatrixMode(GL_MODELVIEW)
122 docSlot("display", "Display callback for glut.")
124 glClear(GL_COLOR_BUFFER_BIT)
126 glTranslated(-1, -1, 0)
135 docSlot("timer", "Timer callback for glut.")
141 self glutPostRedisplay
144 docSlot("keyboard", "Key down callback for glut.")
145 keyboard := method(key, x, y,
146 if(key asCharacter == "4", ship setTurningLeft(true))
147 if(key asCharacter == "6", ship setTurningRight(true))
148 if(key asCharacter == "8", ship setThrusting(true))
151 docSlot("keyboardUp", "Key up callback for glut.")
152 keyboardUp := method(key, x, y,
153 if(key asCharacter == "4", ship setTurningLeft(false))
154 if(key asCharacter == "6", ship setTurningRight(false))
155 if(key asCharacter == "8", ship setThrusting(false))
159 docSlot("run", "Sets up GLUT, creates the window and sets a few of the opengl defaults.")
161 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
162 glutInitWindowPosition(200, 100)
163 glutInitWindowSize(512, 512)
165 glutCreateWindow("Io Ship")
167 glutEventTarget(Screen clone)
174 glClearColor(0, 0, 0, 1)
175 glutIgnoreKeyRepeat(1)
178 glEnable(GL_LINE_SMOOTH)
179 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
185 writeln("Controls\n forward keypad 8\n left keypad 4\n right keypad 6")
188 # vim:ts=4:sts=4:sw=4:et: