I am almost done with this, it only needs a menu... and it needs a bug fixed. One bug, and its pesky.
It should display a line going up and down, and one going horizontal each time you hit the target. but instead, on the first time you hit the target, it only displays one line. i know the vertical one exists, though, because i have collided with it during gameplay, in which case it only shows up for a split second before quitting the program.
Code:
Code:
http://pastebin.com/MtPfiagb
EDIT:
seems like it always happens at 600 points, which makes me think it has to do with the pseudorandom number generator.
EDIT2:
fixed it by changing
Code:
to
Code:
EDIT:
Ok, its released
http://dl.dropbox.com/u/63621708/calcs/progs/luaZM/aspirin.zip
(its in the queue, but it hasnt been accepted yet)
It should display a line going up and down, and one going horizontal each time you hit the target. but instead, on the first time you hit the target, it only displays one line. i know the vertical one exists, though, because i have collided with it during gameplay, in which case it only shows up for a split second before quitting the program.
Code:
Code:
print("starting aspirin...")
--function variables
local drawRectFill = zmg.drawRectFill
local drawCircle = zmg.drawCircle
local fastCopy = zmg.fastCopy
local makeColor = zmg.makeColor
local drawPoint = zmg.drawPoint
local drawLine = zmg.drawLine
local clear = zmg.clear
local drawText = zmg.drawText
local keyDirectPoll = zmg.keyDirectPoll
local keyDirect = zmg.keyDirect
local floor = math.floor
local random = math.random
--variables
local key = {F1=79, F2=69, F3=59, F4=49, F5=39, F6=29, Alpha=77, Exit=47, Optn=68, Up=28, Down=37, Left=38, Right=27}
local color = {bg=makeColor("black"),fg=makeColor("blue"),fg2=makeColor("red"),line=makeColor("lightblue")}
--lines={{x1,y1,direction(-1,1),direction(0=horiz,1=vert)},{...}}
local lines={}
local player={x=384/2,y=216/2,size=5,speed=2}
local target={x=384/2,y=216/4,size=10}
local linewidth=20
local linespeed=2
local score=0
--screen vars
local SCREEN_WIDTH = 384
local SCREEN_HEIGHT = 216
--game loop
keyDirectPoll()
while exit~=1 do
if keyDirect(key.Exit)>0 then
exit=1
end
--clear screen
drawRectFill(0,0,SCREEN_WIDTH,SCREEN_HEIGHT,color.bg)
--display score
drawText(1,1,"SCORE: " .. score,color.fg2,color.bg)
--[[debug
drawText(1,20,"DEBUG: " .. #lines,color.fg2,color.bg)
--]]
--keys
if keyDirect(key.Left)>0 and player.x>0 then player.x=player.x-player.speed
elseif keyDirect(key.Right)>0 and player.x<SCREEN_WIDTH then player.x=player.x+player.speed
end
if keyDirect(key.Up)>0 and player.y>0 then player.y=player.y-player.speed
elseif keyDirect(key.Down)>0 and player.y<SCREEN_HEIGHT then player.y=player.y+player.speed
end
--calculations
--check collision with target
if player.x<target.x+target.size and player.x>target.x-target.size
and player.y-player.size<target.y+target.size and player.y+player.size>target.y-target.size then
target.x=random(0,SCREEN_WIDTH)
target.y=random(0,SCREEN_HEIGHT)
score=score+100
-- create lines
if player.y>SCREEN_HEIGHT/2 then
lines[#lines+1]={random(0,SCREEN_WIDTH/2),random(0,SCREEN_HEIGHT/2-player.size),1,0}
else
lines[#lines+1]={random(0,SCREEN_WIDTH/2),random(SCREEN_HEIGHT/2+player.size,SCREEN_HEIGHT),1,0}
end
if player.x>SCREEN_WIDTH/2 then
lines[#lines+2]={random(0,SCREEN_WIDTH/2-player.size),random(0,SCREEN_HEIGHT/2),1,1}
else
lines[#lines+2]={random(SCREEN_WIDTH/2+player.size,SCREEN_WIDTH),random(0,SCREEN_HEIGHT/2),1,1}
fastCopy()
end
end
--lines
for i=1,#lines,1 do
--if horizontal
if lines[i][4]==0 then
--reverse direction if hit edge
if lines[i][1]>SCREEN_WIDTH-linewidth or lines[i][1]<0 then
lines[i][3]=lines[i][3]*-1
end
--check collisions
if lines[i][1]+linewidth>player.x-player.size and lines[i][1]<player.x+player.size
and lines[i][2]>player.y-player.size and lines[i][2]<player.y+player.size then
print("you lose")
exit=1
end
--move it along
lines[i][1]=lines[i][1]+linespeed*lines[i][3]
--draw it
drawLine(lines[i][1],lines[i][2],lines[i][1]+linewidth,lines[i][2],color.line)
end
--if vertical
if lines[i][4]==1 then
--reverse direction if hit edge
if lines[i][2]>SCREEN_HEIGHT-linewidth or lines[i][2]<0 then
lines[i][3]=lines[i][3]*-1
end
--check collisions
if lines[i][2]+linewidth>player.y-player.size and lines[i][2]<player.y+player.size
and lines[i][1]>player.x-player.size and lines[i][1]<player.x+player.size then
print("you lose")
exit=1
end
--move it along
lines[i][2]=lines[i][2]+linespeed*lines[i][3]
--draw it
drawLine(lines[i][1],lines[i][2],lines[i][1],lines[i][2]+linewidth,color.line)
end
end
keyDirectPoll()
--display
drawCircle(player.x,player.y,player.size,color.fg)
drawCircle(target.x,target.y,target.size,color.fg2)
fastCopy()
keyDirectPoll()
end
http://pastebin.com/MtPfiagb
EDIT:
seems like it always happens at 600 points, which makes me think it has to do with the pseudorandom number generator.
EDIT2:
fixed it by changing
Code:
lines[#lines+2]={random(SCREEN_WIDTH/2+player.size,SCREEN_WIDTH),random(0,SCREEN_HEIGHT/2),1,1}
to
Code:
lines[#lines+1]={random(SCREEN_WIDTH/2+player.size,SCREEN_WIDTH),random(0,SCREEN_HEIGHT/2),1,1}
EDIT:
Ok, its released
http://dl.dropbox.com/u/63621708/calcs/progs/luaZM/aspirin.zip
(its in the queue, but it hasnt been accepted yet)