-
Notifications
You must be signed in to change notification settings - Fork 10
/
sketch.lisp
56 lines (47 loc) · 1.39 KB
/
sketch.lisp
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
(defpackage :nature-of-code.vectors.example-1
(:export :start-sketch)
(:use :cl :trivial-gamekit)
(:shadow :step))
(in-package :nature-of-code.vectors.example-1)
(defvar *width* 800)
(defvar *height* 600)
(defvar *black* (vec4 0 0 0 1))
(defvar *gray* (vec4 0.5 0.5 0.5 1))
(defclass ball ()
((pos
:initform (vec2 (/ *width* 2) (/ *height* 2))
:accessor pos)
(dx
:initform 2
:accessor dx)
(dy
:initform 3.3
:accessor dy)))
(defmethod step ((ball ball))
(let* ((x (x (pos ball)))
(y (y (pos ball)))
(new-x (+ x (dx ball)))
(new-y (+ y (dy ball))))
(setf (pos ball) (vec2 new-x new-y)) ; Update position
(when (or (< new-x 0) (> new-x *width*)) ; Reverse x direction if needed
(setf (dx ball) (- (dx ball))))
(when (or (< new-y 0) (> new-y *height*)) ; Reverse y direction if needed
(setf (dy ball) (- (dy ball))))))
(defmethod draw ((ball ball))
(draw-circle (pos ball) 25
:fill-paint *gray*
:stroke-paint *black*
:thickness 2))
(defgame sketch ()
((ball
:initform (make-instance 'ball)
:accessor ball))
(:viewport-width *width*)
(:viewport-height *height*)
(:viewport-title "Bouncing ball without vectors"))
(defmethod draw ((this sketch))
(draw (ball this)))
(defmethod act ((this sketch))
(step (ball this)))
(defun start-sketch ()
(start 'sketch))