Created
December 22, 2015 05:11
-
-
Save dermotbalson/81af48c279540f6bdbd4 to your computer and use it in GitHub Desktop.
Rocks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--# Rock | |
-- Basic rock game | |
--[[ | |
The game | |
A rock is falling down the screen. | |
It is created 50 pixels above the top of the screen, and with a random x value. | |
It falls at 100 pixels per second, and when it falls off the screen, a new rock appears at the top. | |
You have to avoid the rock hitting you, by touching the left or right of the screen to move out of the way. | |
You can use any of the images built into Codea. | |
--]] | |
function setup() | |
speed = 100 / 60 --falling speed per "draw" (Codea draws 60 times per second) | |
pos=vec2(WIDTH/2,100) --our x position | |
posChange=3 --how much our x position changes when we touch the screen | |
personImg=readImage("Planet Cute:Character Boy") --the image for us | |
personSize=75 --the size of our image on the screen | |
rockImg=readImage("Tyrian Remastered:Rock 4") --the image for the rock | |
rockSize=75 --the size we want to draw the rock | |
addRock() --add the rock | |
end | |
function draw() | |
background(0) | |
rockPos.y=rockPos.y-speed | |
if rockPos.y<0 then --time for another rock! | |
addRock() | |
end | |
--draw the rock, and us | |
sprite(rockImg,rockPos.x,rockPos.y,rockSize) | |
sprite(personImg,pos.x,pos.y,personSize) | |
CheckCollision() | |
end | |
function touched(t) | |
--move left or right, but don't go off the screen | |
if t.x<WIDTH/2 then pos.x=math.max(50,pos.x-posChange) else pos.x=math.min(WIDTH-50,pos.x+posChange) end | |
end | |
function addRock() | |
--choose random x position, but not too close to the edge | |
rockPos=vec2(math.random(50,WIDTH-50),HEIGHT+50) | |
end | |
function CheckCollision() | |
--calculate the distance between us and the rock | |
--if less than the radius of the rock, we are toast | |
if pos:dist(rockPos)<rockSize/2 then | |
sound(SOUND_EXPLODE, 26907) | |
--put code here to change the score, end the game, start again, whatever | |
end | |
end | |
--# MoreRocks | |
-- Multiple rock game | |
--[[ | |
The game | |
Several rocks are falling down the screen. A new one falls, every second. | |
Each rock is created 50 pixels above the top of the screen, and with a random x value. | |
Each rock falls at 100 pixels per second. | |
You have to avoid any of the rocks hitting you, by touching the left or right of the screen to move out of the way. | |
You can use any of the images built into Codea. | |
--]] | |
function setup() | |
speed = 100 / 60 --falling speed per "draw" (Codea draws 60 times per second) | |
pos=vec2(WIDTH/2,100) --our x position | |
posChange=3 --how much our x position changes when we touch the screen | |
personImg=readImage("Planet Cute:Character Boy") --the image for us | |
personSize=75 --the size of our image on the screen | |
rockImg=readImage("Tyrian Remastered:Rock 4") --the image for the rock | |
rockSize=75 --the size we want to draw the rock | |
--** new code below | |
rockPos={} --**** a list of rocks | |
timeBetweenRocks=1 --*** how often we add rocks (in seconds) | |
timeOfLastRock=0 --*** time when we added the last rock | |
end | |
function draw() | |
background(0) | |
--***adjust position for all rocks | |
for i=#rockPos,1,-1 do --*** loop through all rocks $$$$ YOU NEED TO EXPLAIN WHY LOOP IS BACKWARDS $$$$ | |
rockPos[i].y=rockPos[i].y-speed | |
if rockPos[i].y<0 then --*** remove rock if it falls off screen | |
table.remove(rockPos,i) | |
else --draw the rock and check for collisions | |
sprite(rockImg,rockPos[i].x,rockPos[i].y,rockSize) | |
CheckCollision(rockPos[i]) --*** pass through the rock we are comparing | |
end | |
end | |
if ElapsedTime>timeOfLastRock+timeBetweenRocks then --*** time for another rock! | |
addRock() | |
timeOfLastRock=ElapsedTime | |
end | |
sprite(personImg,pos.x,pos.y,personSize) | |
end | |
function touched(t) | |
--move left or right, but don't go off the screen | |
if t.x<WIDTH/2 then pos.x=math.max(50,pos.x-posChange) else pos.x=math.min(WIDTH-50,pos.x+posChange) end | |
end | |
function addRock() | |
--*** add a new rock to the table | |
--choose random x position, but not too close to the edge | |
table.insert(rockPos,vec2(math.random(50,WIDTH-50),HEIGHT+50)) | |
end | |
function CheckCollision(r) --*** we pass through the rock position we are comparing | |
--calculate the distance between us and the rock | |
--if less than the radius of the rock, we are toast | |
if pos:dist(r)<rockSize/2 then | |
sound(SOUND_EXPLODE, 26907) | |
--put code here to change the score, end the game, start again, whatever | |
end | |
end | |
--# BetterRocks | |
-- More interesting rock game | |
--[[ | |
The game | |
These rules are the same as before - | |
Several rocks are falling down the screen. A new one falls, every second. | |
Each rock is created 50 pixels above the top of the screen, and with a random x value. | |
Each rock falls at 100 pixels per second. | |
You have to avoid any of the rocks hitting you, by touching the left or right of the screen to move out of the way. | |
You can use any of the images built into Codea. | |
*** These are new rules to make the game more interesting | |
The rocks should be different images and sizes, rotate at different speeds, and fall at different speeds, | |
and maybe even do different damage | |
--]] | |
function setup() | |
pos=vec2(WIDTH/2,100) --our x position | |
posChange=3 --how much our x position changes when we touch the screen | |
personImg=readImage("Planet Cute:Character Boy") --the image for us | |
personSize=75 --the size of our image on the screen | |
rockImg=readImage("Tyrian Remastered:Rock 4") --the image for the rock | |
rockSize=75 --the size we want to draw the rock | |
LoadRocks() --*** set up the rocks, do this in another function to avoid making a mess | |
timeBetweenRocks=1 -- how often we add rocks (in seconds) | |
timeOfLastRock=0 -- time when we added the last rock | |
end | |
function LoadRocks() --*** new function to set up a list of rocks | |
rocks={} --*** an empty list of rocks (name has changed from rockPos because we are not just storing position) | |
--*** now read in a set of images to choose from | |
rockImages={} --we'll put them in a list | |
--start with the 7 rock images in Codea, we can use a loop for that | |
for i=1,7 do | |
table.insert(rockImages,readImage("Tyrian Remastered:Rock "..i)) | |
end | |
--now add some more, for fun | |
table.insert(rockImages,readImage("Tyrian Remastered:Mine Spiked Huge")) | |
table.insert(rockImages,readImage("Tyrian Remastered:Flame Wave")) | |
table.insert(rockImages,readImage("Tyrian Remastered:Fire Rock")) | |
end | |
function draw() | |
background(0) | |
--adjust position for all rocks | |
for i=#rocks,1,-1 do --loop through all rocks | |
local r = rocks[i] --*** this is just to make the code below easier to read | |
r.pos.y=r.pos.y-r.speed/60 --*** now we use the vaues for each rock | |
r.angle=r.angle+r.rotateSpeed | |
if r.pos.y<0 then --remove rock if it falls off screen | |
table.remove(rocks,i) | |
else --draw the rock and check for collisions | |
pushMatrix() --** we need to translate and then rotate | |
translate(r.pos.x,r.pos.y) | |
rotate(r.angle) | |
sprite(r.image,0,0,r.size) | |
popMatrix() | |
CheckCollision(r) --pass through the rock we are comparing | |
end | |
end | |
if ElapsedTime>timeOfLastRock+timeBetweenRocks then --time for another rock! | |
addRock() | |
timeOfLastRock=ElapsedTime | |
end | |
sprite(personImg,pos.x,pos.y,personSize) | |
end | |
function touched(t) | |
--move left or right, but don't go off the screen | |
if t.x<WIDTH/2 then pos.x=math.max(50,pos.x-posChange) else pos.x=math.min(WIDTH-50,pos.x+posChange) end | |
end | |
function addRock() --*** big changes here! | |
--we need to store a list of items for each rock, and each rock is part of a list | |
--this is how we can do it | |
local t={} --make a new list for this rock's items | |
--*** we'll give the items names to make them easier to use when we draw | |
t.pos=vec2(math.random(50,WIDTH-50),HEIGHT+50) --choose random x position, but not too close to the edge | |
t.speed=math.random(80,120) -- falling speed | |
t.image=rockImages[math.random(1,#rockImages)]-- image to use | |
t.size=math.random(20,50)-- size | |
t.angle=math.random(-360,360)-- rotation angle | |
t.rotateSpeed=math.random()-0.5-- rotation speed | |
t.damage=t.size*t.speed -- damage caused if it hits us | |
--now add our list of items to the list of rocks | |
table.insert(rocks,t) | |
end | |
function CheckCollision(r) --we pass through the rock position we are comparing | |
--calculate the distance between us and the rock | |
--if less than the radius of the rock, we are toast | |
if pos:dist(r.pos)<rockSize/2 then | |
sound(SOUND_EXPLODE, 26907) | |
--put code here to change the score, end the game, start again, whatever | |
end | |
end | |
--# Main | |
-- MultiStep | |
function setup() | |
demos = listProjectTabs() | |
for i=#demos,1,-1 do | |
if demos[i]=="Notes" or demos[i]=="Utility" then table.remove(demos,i) end | |
end | |
table.remove(demos) --remove the last tab | |
startDemo() | |
global = "select a step" | |
end | |
function showList() | |
output.clear() | |
for i=1,#demos do print(i,demos[i]) end | |
end | |
function startDemo() | |
resetMatrix() | |
if cleanup then cleanup() end | |
setup,draw,touched,collide,PrintExplanation=nil,nil,nil,nil,nil | |
lastDemo=Demo or readProjectData("lastDemo") or 1 | |
lastDemo=math.min(lastDemo,#demos) | |
saveProjectData("lastDemo",lastDemo) | |
parameter.clear() | |
parameter.integer("Demo", 1, #demos, lastDemo,showList) | |
parameter.action("Run", startDemo) | |
loadstring(readProjectTab(demos[Demo]))() | |
if PrintExplanation then PrintExplanation() end | |
setup() | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment