Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diffsettings = {
- {240, 32},
- {280, 22},
- {310, 18},
- {330, 12},
- {350, 10},
- {370, 7},
- {390, 6},
- {400, 5},
- {420, 5},
- {450, 5}
- }
- function setup()
- displayMode(FULLSCREEN_NO_BUTTONS)
- diff = 1
- nextdiff = 1200
- state = "intro"
- lives = 2
- points = 0
- debug = false
- showfps = false
- frames = {}
- streaks = Streaks()
- ship = Ship()
- rocks = Rocks()
- font = Font()
- pickups = Pickups()
- ship.py = 0 - ship.height
- rockspeed = diffsettings[1][1]
- rocks.newrock = diffsettings[1][2]
- end
- function draw()
- background(0, 0, 0)
- noSmooth()
- if debug then
- translate(0.25 * WIDTH, 0.25 * HEIGHT)
- scale(0.5, 0.5)
- pushStyle()
- fill(255, 255, 255, 10)
- rect(0, 0, WIDTH, HEIGHT)
- popStyle()
- end
- if showfps then
- table.insert(frames, DeltaTime)
- t = 0
- fps = 0
- longest = 0
- for i,v in ipairs(frames) do
- t = t + v
- fps = fps + 1
- if v > longest then
- longest = v
- end
- end
- for i,v in ipairs(frames) do
- if t < 1 then
- break
- end
- t = t - v
- table.remove(frames, i)
- end
- strokeWidth(1)
- stroke(255, 255, 255, 255)
- font:drawstring(""..fps, WIDTH - 50, HEIGHT - 200)
- font:drawstring(""..longest*1000, WIDTH - 50, HEIGHT - 250)
- font:drawstring(""..diff, WIDTH - 50, HEIGHT - 300)
- end
- if state == "intro" then
- if ship.py < 60 then
- ship.py = ship.py + 1
- else
- state = "play"
- end
- end
- streaks:draw()
- rocks:draw()
- pickups:draw()
- ship:draw()
- --font:drawstring(""..lives, 50, HEIGHT - 50)
- if state == "intro" then
- pushStyle()
- strokeWidth(2)
- scale(2)
- font:drawstring("SWOOSH", WIDTH/4 - 60, HEIGHT/4)
- scale(1)
- popStyle()
- end
- if state == "play" then
- pushStyle()
- stroke(82, 69, 31, 255)
- fill(126, 106, 49, 200)
- strokeWidth(2)
- rectMode(CORNER)
- rect(-10, HEIGHT - 90, 25 + 50 * lives, 70)
- rect(WIDTH - 100, HEIGHT - 90, 130, 70)
- fill(182, 157, 105, 120)
- stroke(155, 130, 57, 255)
- rect(WIDTH - 95, HEIGHT - 85, 130, 60)
- pushStyle()
- smooth()
- stroke(91, 65, 29, 255)
- strokeWidth(5)
- font:drawstring(""..points, WIDTH - 80, HEIGHT - 65)
- stroke(0, 0, 0, 255)
- noSmooth()
- strokeWidth(2)
- font:drawstring(""..points, WIDTH - 80, HEIGHT - 65)
- popStyle()
- for i = 1,lives do
- sprite("Tyrian Remastered:Icon Ship Glow", 50*i - 20, HEIGHT - 55, 38, 44)
- end
- rect(-10, HEIGHT - 85, 20 + 50 * lives, 60)
- popStyle()
- noSmooth()
- end
- if state == "gameover" then
- font:drawstring("YOU GOT "..finalpoints.." POINTS", WIDTH/2 - 190, HEIGHT/2 - 80)
- pushStyle()
- strokeWidth(2)
- scale(2)
- font:drawstring("GAME OVER", WIDTH/4 - 100, HEIGHT/4)
- scale(1)
- popStyle()
- end
- for i,v in ipairs(rocks.rocks) do
- if vec2(v.px, v.py):dist(vec2(ship.px, ship.py)) < v.width/2+ship.width/2 and ship.crashed == false and ship.invulnerable < 1 then
- ship.crashed = true
- sound(SOUND_EXPLODE, 25)
- explosion = Explosion(vec2(ship.px, ship.py))
- if diff > 1 then
- diff = diff - 2
- nextdiff = 2
- end
- end
- end
- for i,v in ipairs(pickups.pickups) do
- if vec2(v.px, v.py):dist(vec2(ship.px, ship.py)) < v.w/2+ship.width/2 and ship.crashed == false then
- if v.type == "gem" then
- sound(SOUND_PICKUP, 82)
- points = points + 9 + diff
- end
- table.remove(pickups.pickups, i)
- end
- end
- if ship.crashed and state == "play" then
- explosion:draw()
- if explosion:isDone() then
- explosion = ""
- if lives > 0 then
- ship.crashed = false
- ship.invulnerable = 150
- lives = lives - 1
- else
- finalpoints = points
- state = "gameover"
- end
- end
- end
- if nextdiff > 1 then
- nextdiff = nextdiff - 1
- else
- if diff < table.maxn(diffsettings) then
- diff = diff + 1
- end
- points = points + diff * 5
- rockspeed = diffsettings[diff][1]
- rocks.newrock = diffsettings[diff][2]
- nextdiff = 1200
- end
- end
- pickuptypes = {
- gem = {38, 54}
- }
- Pickups = class()
- function Pickups:init()
- self.pickups = {}
- self.newgem = math.random(250, 400)
- self.nextgem = self.newgem
- end
- function Pickups:draw()
- if self.nextgem < 1 then
- table.insert(self.pickups, Pickup("gem", pickuptypes["gem"][1], pickuptypes["gem"][2]))
- self.nextgem = math.random(150, 400)
- else
- self.nextgem = self.nextgem - 1
- end
- for i,v in ipairs(self.pickups) do
- v:draw()
- end
- for i,v in ipairs(self.pickups) do
- if v.py < 0 - v.h/2 then
- table.remove(self.pickups, i)
- end
- end
- end
- Pickup = class()
- function Pickup:init(type, w, h)
- self.type = type
- self.w = w
- self.h = h
- self.px = math.random(0 + self.w, WIDTH - self.w)
- self.py = HEIGHT+self.h/2
- self.akey = 1
- end
- function Pickup:draw()
- self.py = self.py - rockspeed*DeltaTime
- if self.type == "gem" then
- if self.akey >= 2 and self.akey <= 12 then
- sprite("Tyrian Remastered:Gem Shine "..math.ceil(self.akey/2), self.px, self.py, self.w, self.h)
- else
- sprite("Tyrian Remastered:Gem Shine "..1, self.px, self.py, self.w, self.h)
- end
- end
- self.akey = self.akey + 1
- if self.akey > 70 then
- self.akey = 1
- end
- end
- Rocks = class()
- function Rocks:init()
- self.rocks = {}
- self.newrock = 60
- self.nextrock = self.newrock
- end
- function Rocks:draw()
- if self.nextrock < 1 then
- table.insert(self.rocks, Rock())
- self.nextrock = math.ceil(self.newrock)
- else
- self.nextrock = self.nextrock - 1
- end
- for i,v in ipairs(self.rocks) do
- v:draw()
- end
- for i,v in ipairs(self.rocks) do
- if v.py < 0 - v.height/2 then
- table.remove(self.rocks, i)
- end
- end
- end
- rocksprites = {
- {1, 50, 48},
- {2, 50, 52},
- {3, 86, 92},
- {4, 82, 104},
- {5, 140, 152},
- {6, 46, 48},
- {7, 44, 40}
- }
- rockspeed = 400
- Rock = class()
- function Rock:init()
- self.sid = rocksprites[math.random(1, table.maxn(rocksprites))][1]
- self.width = rocksprites[self.sid][2]
- self.height = rocksprites[self.sid][3]
- self.px = math.random(0, WIDTH)
- self.py = HEIGHT+self.height/2
- end
- function Rock:draw()
- self.py = self.py - rockspeed*DeltaTime
- sprite("Tyrian Remastered:Rock "..self.sid, self.px, self.py, self.width, self.height)
- end
- Ship = class()
- function Ship:init()
- self.px = WIDTH/2
- self.py = 50
- self.width = 42
- self.height = 54
- self.crashed = false
- self.invulnerable = 0
- end
- function Ship:draw()
- self.px = self.px + Gravity.x*35
- if self.px < self.width/2 then
- self.px = self.width/2
- elseif self.px > WIDTH - self.width/2 then
- self.px = WIDTH - self.width/2
- end
- sid = ""
- if Gravity.x > 0.14 then
- sid = " R2"
- elseif Gravity.x > 0.06 then
- sid = " R1"
- elseif Gravity.x < -0.14 then
- sid = " L2"
- elseif Gravity.x < -0.06 then
- sid = " L1"
- end
- if self.invulnerable > 0 then
- self.invulnerable = self.invulnerable - 1
- end
- if self.crashed == false and not (self.invulnerable < 140 and self.invulnerable > 130) and not (self.invulnerable < 120 and self.invulnerable > 110) then
- if self.invulnerable > 0 then
- sprite("Tyrian Remastered:Energy Orb 2", self.px, self.py, 60, 60)
- end
- sprite("Tyrian Remastered:Ship A"..sid, self.px, self.py, 40, 54)
- if self.invulnerable > 0 and self.invulnerable ~= 8 and self.invulnerable ~= 16 then
- pushStyle()
- stroke(255, 255, 255, 255)
- strokeWidth(1)
- fill(203, 217, 228, 123)
- ellipseMode(CENTER)
- ellipse(self.px, self.py, 80, 80)
- popStyle()
- end
- end
- end
- Streaks = class()
- function Streaks:init()
- self.stars = {}
- self.spawnrate = 1
- for i = 1, 50 do
- table.insert(self.stars, vec3(math.random(0, WIDTH), math.random(0, HEIGHT), math.random(4,15)))
- end
- end
- function Streaks:draw()
- for i = 1, self.spawnrate do
- table.insert(self.stars, vec3(math.random(0, WIDTH), HEIGHT, math.random(4,15)))
- end
- for i,v in ipairs(self.stars) do
- stroke(255, 255, 255, 255)
- strokeWidth(1)
- noSmooth()
- fill(255, 255, 255, 255)
- oy = v.y
- self.stars[i] = vec3(v.x, oy-v.z, v.z)
- line(v.x, oy, v.x, self.stars[i].y)
- end
- for i,v in ipairs(self.stars) do
- if self.stars[i].y < 0 then
- table.remove(self.stars, i)
- end
- end
- end
- Explosion = class()
- function Explosion:init(pos)
- self.position = pos
- self.life = 255
- self.particles = {}
- for i = 1, 75 do
- vel = vec2(math.random(-10,10), math.random(-10,10))
- table.insert(self.particles, Particle(self.position, vel))
- end
- end
- function Explosion:draw()
- stroke(self.life, self.life, self.life, self.life)
- for i,v in ipairs(self.particles) do
- v:draw()
- end
- self.life = self.life - 217*DeltaTime
- if self.life > 128 then
- sprite("Tyrian Remastered:Explosion Huge", self.position.x, self.position.y, 128-self.life/2)
- elseif self.life > 0 then
- sprite("Tyrian Remastered:Explosion Huge", self.position.x, self.position.y, self.life/2)
- end
- end
- function Explosion:isDone()
- if self.life > 0 then
- return false
- else
- return true
- end
- end
- Particle = class()
- function Particle:init(pos, vel)
- self.position = pos
- self.velocity = vel
- end
- function Particle:draw()
- line(self.position.x, self.position.y, self.position.x+self.velocity.x, self.position.y+self.velocity.y)
- self.position = vec2(self.position.x+self.velocity.x*DeltaTime*50,
- self.position.y+self.velocity.y*DeltaTime*50)
- end
- Font = class()
- -- - The Hershey Fonts were originally created by Dr.
- -- A. V. Hershey while working at the
- -- U. S. National Bureau of Standards.
- -- Useful Links:
- -- http://emergent.unpythonic.net/software/hershey
- -- http://paulbourke.net/dataformats/hershey/
- -- Re-encoding of font information and other shenanigans
- -- by Tom Bortels bortels@gmail.com November 2011
- -- all rights reversed (Hail Eris!)
- -- "If I have seen a little further it is by standing
- -- on the shoulders of Giants."
- -- Isaac Newton
- function Font:init()
- -- font data - 2 decimal character # of points,
- -- followed by 2*points of point data
- -- 9->-9, 8-<-8, ... 1->-1, 0->0, A->1, B->2, ... Z->26
- self.code = "9876543210ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- -- this is the Hershey Roman Simplex font for ascii 32-127
- self.fontdata =
- "00160810EUEG11EBDAE0FAEB0516DUDN11LULN1121KYD711QYJ711DLRL11"
- .. "CFQF2620HYH411LYL411QROTLUHUETCRCPDNEMGLMJOIPHQFQCOAL0H0EACC"
- .. "3124UUC011HUJSJQIOGNENCPCRDTFUHUJTMSPSSTUU11QGOFNDNBP0R0TAUC"
- .. "UESGQG3426WLWMVNUNTMSKQFOCMAK0G0EADBCDCFDHEILMMNNPNRMTKUITHR"
- .. "HPIMKJPCRAT0V0WAWB0710ESDTEUFTFREPDO1014KYIWGTEPDKDGEBG2I5K7"
- .. "1014CYEWGTIPJKJGIBG2E5C70816HUHI11CRML11MRCL0526MRM011DIVI08"
- .. "10FAE0DAEBFAF1E3D40226DIVI0510EBDAE0FAEB0222TYB71720IUFTDQCL"
- .. "CIDDFAI0K0NAPDQIQLPQNTKUIU0420FQHRKUK01420DPDQESFTHULUNTOSPQ"
- .. "POOMMJC0Q01520EUPUJMMMOLPKQHQFPCNAK0H0EADBCD0620MUCGRG11MUM0"
- .. "1720OUEUDLEMHNKNNMPKQHQFPCNAK0H0EADBCD2320PROTLUJUGTEQDLDGEC"
- .. "GAJ0K0NAPCQFQGPJNLKMJMGLEJDG0520QUG011CUQU2920HUETDRDPENGMKL"
- .. "NKPIQGQDPBOAL0H0EADBCDCGDIFKILMMONPPPROTLUHU2320PNOKMIJHIHFI"
- .. "DKCNCODRFTIUJUMTORPNPIODMAJ0H0EADC1110ENDMELFMEN11EBDAE0FAEB"
- .. "1410ENDMELFMEN11FAE0DAEBFAF1E3D40324TRDIT00526DLVL11DFVF0324"
- .. "DRTID02018CPCQDSETGUKUMTNSOQOONMMLIJIG11IBHAI0JAIB5527RMQOOP"
- .. "LPJOINHKHHIFKENEPFQH11LPJNIKIHJFKE11RPQHQFSEUEWGXJXLWOVQTSRT"
- .. "OULUITGSEQDOCLCIDFEDGBIAL0O0RATBUC11SPRHRFSE0818IUA011IUQ011"
- .. "DGNG2321DUD011DUMUPTQSRQROQMPLMK11DKMKPJQIRGRDQBPAM0D01821RP"
- .. "QROTMUIUGTERDPCMCHDEECGAI0M0OAQCRE1521DUD011DUKUNTPRQPRMRHQE"
- .. "PCNAK0D01119DUD011DUQU11DKLK11D0Q00818DUD011DUQU11DKLK2221RP"
- .. "QROTMUIUGTERDPCMCHDEECGAI0M0OAQCRERH11MHRH0822DUD011RUR011DK"
- .. "RK0208DUD01016LULEKBJAH0F0DACBBEBG0821DUD011RUDG11ILR00517DU"
- .. "D011D0P01124DUD011DUL011TUL011TUT00822DUD011DUR011RUR02122IU"
- .. "GTERDPCMCHDEECGAI0M0OAQCRESHSMRPQROTMUIU1321DUD011DUMUPTQSRQ"
- .. "RNQLPKMJDJ2422IUGTERDPCMCHDEECGAI0M0OAQCRESHSMRPQROTMUIU11LD"
- .. "R21621DUD011DUMUPTQSRQROQMPLMKDK11KKR02020QROTLUHUETCRCPDNEM"
- .. "GLMJOIPHQFQCOAL0H0EACC0516HUH011AUOU1022DUDFECGAJ0L0OAQCRFRU"
- .. "0518AUI011QUI01124BUG011LUG011LUQ011VUQ00520CUQ011QUC00618AU"
- .. "IKI011QUIK0820QUC011CUQU11C0Q01114DYD711EYE711DYKY11D7K70214"
- .. "0UN31114IYI711JYJ711CYJY11C7J71016FOHRJO11CLHQML11HQH0021602"
- .. "P20710FUETDRDPEOFPEQ1719ONO011OKMMKNHNFMDKCHCFDCFAH0K0MAOC17"
- .. "19DUD011DKFMHNKNMMOKPHPFOCMAK0H0FADC1418OKMMKNHNFMDKCHCFDCFA"
- .. "H0K0MAOC1719OUO011OKMMKNHNFMDKCHCFDCFAH0K0MAOC1718CHOHOJNLMM"
- .. "KNHNFMDKCHCFDCFAH0K0MAOC0812JUHUFTEQE011BNIN2219ONO2N5M6K7H7"
- .. "F611OKMMKNHNFMDKCHCFDCFAH0K0MAOC1019DUD011DJGMINLNNMOJO00808"
- .. "CUDTEUDVCU11DND01110EUFTGUFVEU11FNF3E6C7A70817DUD011NNDD11HH"
- .. "O00208DUD01830DND011DJGMINLNNMOJO011OJRMTNWNYMZJZ01019DND011"
- .. "DJGMINLNNMOJO01719HNFMDKCHCFDCFAH0K0MAOCPFPHOKMMKNHN1719DND7"
- .. "11DKFMHNKNMMOKPHPFOCMAK0H0FADC1719ONO711OKMMKNHNFMDKCHCFDCFA"
- .. "H0K0MAOC0813DND011DHEKGMINLN1717NKMMJNGNDMCKDIFHKGMFNDNCMAJ0"
- .. "G0DACC0812EUEDFAH0J011BNIN1019DNDDEAG0J0LAOD11ONO00516BNH011"
- .. "NNH01122CNG011KNG011KNO011SNO00517CNN011NNC00916BNH011NNH0F4"
- .. "D6B7A70817NNC011CNNN11C0N03914IYGXFWEUESFQGPHNHLFJ11GXFVFTGR"
- .. "HQIOIMHKDIHGIEICHAG0F2F4G611FHHFHDGBFAE1E3F5G6I70208DYD73914"
- .. "EYGXHWIUISHQGPFNFLHJ11GXHVHTGRFQEOEMFKJIFGEEECFAG0H2H4G611HH"
- .. "FFFDGBHAI1I3H5G6E72324CFCHDKFLHLJKNHPGRGTHUJ11CHDJFKHKJJNGPF"
- .. "RFTGUJUL"
- local i=1
- local c=32
- self.font = {}
- while (i < string.len(self.fontdata)) do
- local cs = string.char(c)
- self.font[cs] = {}
- local points = string.sub(self.fontdata, i, i+1)
- self.font[cs].points = points
- self.font[cs].char = cs
- self.font[cs].ascii = c
- self.font[cs].width = string.sub(self.fontdata, i+2, i+3)
- --print(cs,points,self.font[cs].width)
- i = i + 4
- self.font[cs].data = string.sub(self.fontdata, i, i+points*2)
- --print(self.font[cs].data)
- i = i + points*2
- c = c + 1
- end
- i=-9
- self.decode = {}
- for c in self.code:gmatch"." do
- self.decode[c]=i
- i=i+1
- end
- end
- -- returns width in pixels of unscaled, strokeWidth(1) string
- function Font:stringwidth(s)
- local x, l, i = 0, string.len(s)
- for i = 1, l do
- x = x + self.font[s:sub(i, i)].width
- end
- end
- -- draw a string at x,y (skipping offscreen draws)
- function Font:drawstring(s, x, y)
- local l, i
- l = string.len(s)
- for i = 1, l do
- local c = s:sub(i, i)
- local w = self.font[c].width
- if ((x + w) >= 0) then
- x = x + (self:drawchar(c, x, y))
- else
- x = x + w -- skip offscreen left (but track position)
- end
- if (x > WIDTH) then break end -- skip offscreen right
- end
- end
- -- optimized draw string at x,y (old version for reference)
- function Font:olddrawstring(s, x, y)
- local l, i
- l = string.len(s)
- for i = 1, l do
- x = x + (self:drawchar(string.sub(s, i, i), x, y))
- end
- end
- function Font:drawchar(c, x, y)
- local ax, ay, bx, by, minx, maxx = -1, -1, -1, -1, -1, -1
- local p, plot
- local ch = self.font[c]
- for p=1, ch.points do
- ax=bx
- ay=by
- bx=self.decode[ch.data:sub(p*2-1, p*2-1)]
- by=self.decode[ch.data:sub(p*2, p*2)]
- plot=true
- if ((ax==-1) and (ay==-1)) then plot=false end
- if ((bx==-1) and (by==-1)) then plot=false end
- if (plot) then
- line(x+ax, y+ay, x+bx, y+by)
- end
- end
- return ch.width -- for drawstring
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement