Skip to content

Commit c861d35

Browse files
committed
update/add new example programs
1 parent f5d5aa2 commit c861d35

9 files changed

Lines changed: 362 additions & 2 deletions
118 KB
Loading
101 KB
Loading
-313 KB
Loading
480 KB
Loading

docs/examples/animation-and-variables-drawing-lines.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ end
4242

4343
## Related Examples
4444

45+
* [Doodle Draw](doodle-draw.md)
4546
* [Animation with Events](animation-and-variables-animation-with-events.md)
4647
* [Conditions](animation-and-variables-conditions.md)
4748

docs/examples/basic-pong.md

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
# Basic Pong
2+
3+
This is a basic implementation of Pong in L5.
4+
5+
It demonstrates:
6+
7+
* [table](/reference/table)-based game objects
8+
* mouse input
9+
* simple AI
10+
* collision detection
11+
* score tracking
12+
* game loop structure
13+
* use of randomness
14+
15+
From here, you can add additional features such as:
16+
17+
* speeding up the ball on each paddle hit until a player scores 10 points to win
18+
* a win/lose screen that appears when a player gets 10 points, showing who won and final scores. Click to reset the game and start again.
19+
* smarter enemy AI that better anticipates the direction of the ball's movement
20+
* more players - try adding player 3 and 4 along the top and bottom. Each player should have 2 keys for moving their player paddle.
21+
22+
For more ideas, check out Pippin Barr's [PONGS](https://pippinbarr.com/pongs/info/).
23+
24+
![animation of a game of pong with 2 player paddles trying to hit the red ball](/assets/examples/basic-pong.gif "A basic game of pong with 2 player paddles trying to hit a red ball")
25+
26+
```lua
27+
require("L5")
28+
29+
function setup()
30+
size(800,600)
31+
windowTitle("Pong")
32+
rectMode(CENTER)
33+
34+
-- Global variables
35+
p1 = { -- Player 1 (left, controlled by mouse)
36+
x = 15,
37+
y = height/2,
38+
w = 30,
39+
h = 120,
40+
score = 0
41+
}
42+
43+
p2 = { -- Player 2 (right, AI controlled)
44+
x = width-15,
45+
y = height/2,
46+
w = 30,
47+
h = 120,
48+
score = 0,
49+
yspeed = 5
50+
}
51+
52+
b = { -- The ball
53+
x = width/2,
54+
y = height/2,
55+
w = 30,
56+
h = 30,
57+
xspeed = random(3,10),
58+
yspeed = random(3,10)
59+
}
60+
61+
noCursor()
62+
end
63+
64+
function draw()
65+
background(180,0,180)
66+
67+
-- Left player (P1)
68+
fill(0,255,0)
69+
rect(p1.x, p1.y, p1.w, p1.h)
70+
71+
-- Right player (P2)
72+
fill(0,0,255)
73+
rect(p2.x, p2.y, p2.w, p2.h)
74+
75+
-- Ball
76+
fill(200,0,0)
77+
ellipse(b.x, b.y, b.w, b.h)
78+
79+
-- Scores
80+
fill(0)
81+
textSize(24)
82+
text(p1.score, width/4, 20)
83+
text(p2.score, 3/4*width, 20)
84+
85+
-- Player movement
86+
p1.y = mouseY
87+
88+
-- AI movement (simple - just follows ball)
89+
if b.y < p2.y then
90+
p2.y = p2.y - p2.yspeed
91+
else
92+
p2.y = p2.y + p2.yspeed
93+
end
94+
95+
-- Ball movement
96+
b.x = b.x + b.xspeed
97+
b.y = b.y + b.yspeed
98+
99+
-- Wall collision (top/bottom)
100+
if b.y > height or b.y < 0 then
101+
b.yspeed = b.yspeed * -1
102+
end
103+
104+
-- Scoring (checking for wall collisions)
105+
if b.x < 0 then
106+
p2.score = p2.score + 1
107+
resetBall()
108+
end
109+
110+
if b.x > width then
111+
p1.score = p1.score + 1
112+
resetBall()
113+
end
114+
115+
-- Paddle collision
116+
-- Player 1 (left)
117+
if b.x < p1.x + p1.w/2 and
118+
b.y < p1.y + p1.h/2 and
119+
b.y > p1.y - p1.h/2 then
120+
b.x = b.x + 10
121+
b.xspeed = b.xspeed * -1
122+
end
123+
124+
-- Player 2 (right)
125+
if b.x > p2.x - p2.w/2 and
126+
b.y > p2.y - p2.h/2 and
127+
b.y < p2.y + p2.h/2 then
128+
b.x = b.x - 10
129+
b.xspeed = b.xspeed * -1
130+
end
131+
end
132+
133+
function resetBall()
134+
b.x = width/2
135+
b.y = height/2
136+
-- Random direction
137+
b.xspeed = random(3,10) * (random() > 0.5 and 1 or -1)
138+
b.yspeed = random(3,10) * (random() > 0.5 and 1 or -1)
139+
end
140+
```
141+
142+
## Related References
143+
144+
* [table](/table.md) - changing color modes
145+
146+
## Related Examples
147+
148+
* [Drawing software](/examples/drawing-software)
149+

docs/examples/doodle-draw.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Drawing Software
2+
3+
This is a basic implementation of a program to draw doodles, using random colors.
4+
5+
It demonstrates:
6+
7+
* working with colors
8+
* random RGB color selection
9+
* input and event handling
10+
* current and previous frame mouse positioning
11+
12+
From here, you can add additional features such as:
13+
14+
* buttons to select different colors and brush sizes
15+
* draw with stamps or photos
16+
* the ability to save one's drawing to the computer
17+
* a reset button to clear the screen
18+
* randomly selected prompts that suggest what to draw
19+
* a symmetrical drawing mode
20+
* more experimental features!
21+
22+
For more ideas, check out [Meeting Mr. Kid Pix](https://youtu.be/BxoY-_HQOK0?si=8LWjmoNAkNWWydSk).
23+
24+
![Various doodle lines of different colors are drawin in the window](/assets/examples/drawing-software.gif "Various doodle lines in different colors are drawn in the window")
25+
26+
```lua
27+
require("L5")
28+
29+
function setup()
30+
size(800,600)
31+
windowTitle("Drawing software")
32+
33+
background("midnightblue")
34+
strokeWeight(5)
35+
end
36+
37+
function mouseDragged()
38+
line(mouseX, mouseY, pmouseX, pmouseY)
39+
end
40+
41+
function mousePressed()
42+
stroke(random(255),random(255),random(255))
43+
end
44+
45+
```
46+
47+
## Related References
48+
49+
* [mouseDragged()](/reference/mouseDragged)
50+
* [mousePressed()](/reference/mousePressed)
51+
* [mouseX](/reference/mouseX)
52+
* [mouseY](/reference/mouseY)
53+
* [pmouseX](/reference/pmouseX)
54+
* [pmouseY](/reference/pmouseY)
55+
56+
## Related Examples
57+
58+
* [Basic Pong](/examples/basic-pong)
59+

docs/examples/index.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@
77

88
## Animations and Variables
99

10-
* [Drawing Lines](animation-and-variables-drawing-lines.md) - Draw with the mouse.
10+
* [Doodle Draw](doodle-draw.md) - A basic drawing program tracking mouse movements
11+
* [Drawing Lines](animation-and-variables-drawing-lines.md) - A drawing program demonstrating HSB colorMode
1112
* [Animation with Events](animation-and-variables-animation-with-events.md) - Pause and resume animation.
12-
* [Conditions](animation-and-variables-conditions.md) - Use if and else statements to omake decisions while your sketch runs.
13+
* [Conditions](animation-and-variables-conditions.md) - Use if and else statements to make decisions while your sketch runs.
14+
* [Basic Pong](basic-pong.md) - A simple program demonstrating a basic implementation of pong with enemy AI player
1315

1416
## Imported Media
1517

1618
* [Words](imported-media-words.md) - Load fonts and draw text.
1719
* [Copy Image Data](imported-media-copy-image-data.md) - Paint from an image file onto the canvas.
1820
* [Image Transparency](imported-media-image-transparency.md) - Make an image translucent on the canvas.
1921
* [Video Player](imported-media-video.md) - Create a player for video playback and stylize in the window
22+
23+
## Object-Orientation Patterns
24+
25+
* [Random Robot Objects](random-robot-objects.md) - Demonstrates two different Lua patterns - classes with metatables and simple table objects
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# Random Robot Objects
2+
3+
This object-oriented programming example program demonstrates two different Lua patterns - classes with metatables (Robot) and simple tables with methods (box). It consists of a Robot class definition and simple box object.
4+
5+
The box object demonstrates a simpler object-oriented structure of a table with methods. Lua's power of [first-class functions](https://softwarepatternslexicon.com/lua/lua-programming-fundamentals/first-class-functions-and-closures/) is represented here.
6+
7+
A first-class function means they can be:
8+
9+
* Stored in tables (like in the `box` table)
10+
* Assigned to variables (like `display = function(self) ... end`)
11+
* Passed as arguments to other functions
12+
* Returned from functions
13+
14+
The `box` object demonstrates this with `display` and `move` functions stored as values in the box, just like `x`, `y` and `size` are numbers.
15+
16+
The Robot demonstrates uses of [metatables](https://www.tutorialspoint.com/lua/lua_metatables.htm).
17+
18+
* `Robot.__index = Robot` sets up the metatable
19+
* `setmetatable(robot, Robot)` links each instance to the Robot table
20+
* This enables [method lookup](https://cyevgeniy.github.io/luadocs/02_basic_concepts/ch04.html) - when you call `robot:move()`, Lua looks in the Robot table to find the `move` function
21+
22+
Advanced ways to extend the program:
23+
24+
* add more robot behaviors - make robots change colors when they collide with each other
25+
* score counter - count how many times the box teleports to robots
26+
* different robot types - a FastRobot that moves twice as fast as the others
27+
* mouse interaction - Create new robots at the mouse position when clicked
28+
* Box trail - a line showing the box's history
29+
* Chase - make robots chase the box
30+
* power-ups - add collectible items that change robot speed, size or behavior
31+
* animated sprites - multiple images to animate movement
32+
33+
![Random robots moving around the screen. One robot grabs the yellow box, but it is stolen again several times by other robots](/assets/examples/random-robot-objects.gif "Random robot objects roam around the screen. One robot grabs the yellow box, but it is again stolen several times by other robots.")
34+
35+
```lua
36+
require("L5")
37+
38+
-- Note: The Robot class definition below could be moved to a separate file robot.lua
39+
-- and then loaded with: local Robot = require("robot")
40+
41+
Robot = {}
42+
Robot.__index = Robot -- Tells Lua to use Robot's metatable to find methods
43+
44+
-- Global vars
45+
robots = {} -- Table to hold all robot instances
46+
robotImg = nil
47+
48+
function setup()
49+
size(800, 600)
50+
windowTitle("Robots OOP example")
51+
robotImg = loadImage("assets/robot.png")
52+
imageMode(CENTER)
53+
rectMode(CENTER)
54+
strokeWeight(3)
55+
fill("gold")
56+
stroke("goldenrod")
57+
58+
-- Initialize box position
59+
box.x = random(width)
60+
box.y = random(height)
61+
62+
-- Create 10 robots
63+
for i = 1, 10 do
64+
table.insert(robots, Robot:new())
65+
end
66+
end
67+
68+
function draw()
69+
background("lightblue")
70+
71+
-- Update and draw box
72+
box:move()
73+
box:display()
74+
75+
-- Update and draw all robots
76+
for i = 1, #robots do
77+
robots[i]:move()
78+
robots[i]:display()
79+
end
80+
end
81+
82+
-- Robot class definition
83+
function Robot:new(x, y, w, h)
84+
local robot = {
85+
x = x or random(width),
86+
y = y or random(height),
87+
w = w or 70,
88+
h = h or 70,
89+
xspeed = random(-2, 2),
90+
yspeed = random(-2, 2)
91+
}
92+
setmetatable(robot, Robot) -- Links the instance to Robot class
93+
return robot
94+
end
95+
96+
function Robot:move()
97+
self.x = self.x + self.xspeed
98+
self.y = self.y + self.yspeed
99+
100+
-- Bounce off walls
101+
if self.y > height or self.y < 0 then
102+
self.yspeed = self.yspeed * -1
103+
end
104+
if self.x > width or self.x < 0 then
105+
self.xspeed = self.xspeed * -1
106+
end
107+
end
108+
109+
function Robot:display()
110+
image(robotImg, self.x, self.y, self.w, self.h)
111+
end
112+
113+
-- Box object using simple table with methods
114+
-- Demonstrates first-class functions stored as table values
115+
box = {
116+
x = nil, -- Initialized in setup() after random() is seeded
117+
y = nil,
118+
size = 40,
119+
120+
display = function(self)
121+
square(self.x, self.y, self.size)
122+
end,
123+
124+
move = function(self)
125+
-- Teleport to nearest robot if close enough
126+
for i = 1, #robots do
127+
if dist(self.x, self.y, robots[i].x, robots[i].y) < self.size then
128+
self.x = robots[i].x
129+
self.y = robots[i].y
130+
break
131+
end
132+
end
133+
end
134+
}
135+
```
136+
137+
## Related References
138+
139+
* [for](/reference/for)
140+
* [imageMode()](/reference/imageMode)
141+
* [loadImage()](/reference/loadImage)
142+
* [rectMode()](/reference/rectMode)
143+
* [table](/reference/table)
144+
* [random()](/reference/random)
145+

0 commit comments

Comments
 (0)