Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE HTML>
- <canvas id="canvas">
- </cavas>
- <script>
- canvas.width = 1000;
- canvas.height = 800;
- window.onload=function(){
- c = document.getElementById("canvas");
- ctx = c.getContext("2d");
- //load images
- path = new Image();
- path.src = "rockypath.png";
- sidebar = new Image();
- sidebar.src = "sideBar.png";
- red = new Image()
- red.src="red.png"
- blue = new Image()
- blue.src="blue.png"
- green = new Image()
- green.src="green.png"
- yellow = new Image()
- yellow.src="yellow.png"
- pink = new Image()
- pink.src="pink.png"
- black = new Image()
- black.src="black.png"
- //listeners
- canvas.addEventListener('mouseup', mousepressU)
- canvas.addEventListener('mousedown', mousepressD)
- canvas.addEventListener('mousemove', mousepos)
- document.addEventListener('keyup', keydown)
- startRound();
- }
- objects=[]
- towers=[]
- gs=40
- drawcircle=false
- function startRound(){
- enemies=[{picture:blue,x:startX,y:startY,level:"blue",speed:0.3,direction:"right",number:10,spacing:0},{picture:green,x:(startX-100),y:startY,level:"green",speed:0.4,direction:"right",number:5,spacing:200}]
- summon()
- setInterval(startMove,1)
- }
- startY=400+gs/2
- startX=-50
- health=100
- money=300
- pickup=0
- upgrademenu=false
- function summon(){
- for(i=0;i<enemies.length;i++){
- for(j=0;j<(enemies[i].number);j++){
- //packing enemies for start
- objects.push({picture:enemies[i].picture,x:((enemies[i].x)-(enemies[i].spacing*j)),y:startY,level:enemies[i].level,speed:enemies[i].speed,
- direction:enemies[i].direction,score:0+(enemies[i].x)-(enemies[i].spacing*j)})
- }
- }
- /*objects.push({picture:red,x:startX,y:startY,level:"red",speed:0.2,direction:"right",number:10,spacing:100})
- objects.push({picture:blue,x:startX,y:startY,level:"blue",speed:0.3,direction:"right",number:1,spacing:1})
- objects.push({picture:green,x:startX,y:startY,level:"green",speed:0.4,direction:"right",number:1,spacing:1})
- objects.push({picture:yellow,x:startX,y:startY,level:"yellow",speed:0.5,direction:"right",number:1,spacing:1})
- objects.push({picture:pink,x:startX,y:startY,level:"pink",speed:0.6,direction:"right",number:1,spacing:1})
- objects.push({picture:black,x:startX,y:startY,level:"black",speed:0.7,direction:"right",number:1,spacing:1})*/
- }
- function startMove(){
- try{
- ctx.drawImage(sidebar,800,0);
- ctx.drawImage(path,0,0,800,800);
- for(i=0;i<objects.length;i++){
- //add score
- objects[i].score+=(objects[i].speed)
- //if right
- if(objects[i].direction=="right"){
- objects[i].x+=objects[i].speed
- colour=objects[i].level
- ctx.drawImage(objects[i].picture, objects[i].x,objects[i].y,gs,gs)
- //first turn
- if(objects[i].x>=100&&objects[i].x<=101&&objects[i].y==420){
- objects[i].x=100
- objects[i].direction="down"
- }
- //third turn
- if(objects[i].x>=260&&objects[i].x<=261&&objects[i].y==500){
- objects[i].x=260
- objects[i].direction="up"
- }
- //seventh turn
- if(objects[i].x>=660&&objects[i].x<=661&&objects[i].y==100){
- objects[i].x=660
- objects[i].direction="down"
- }
- //eleventh turn
- if(objects[i].x>=580&&objects[i].x<=581&&objects[i].y==420){
- objects[i].x=580
- objects[i].direction="down"
- }
- }
- //if down
- if(objects[i].direction=="down"){
- objects[i].y+=objects[i].speed
- ctx.fillStyle=objects[i].level
- ctx.drawImage(objects[i].picture, objects[i].x,objects[i].y,gs,gs)
- //second turn
- if(objects[i].y>=500&&objects[i].y<=501&&objects[i].x==100){
- objects[i].y=500
- objects[i].direction="right"
- }
- //eight turn
- if(objects[i].y>=260&&objects[i].y<=261&&objects[i].x==660){
- objects[i].y=260
- objects[i].direction="left"
- }
- //tenth turn
- if(objects[i].y>=420&&objects[i].y<=421&&objects[i].x==420){
- objects[i].y=420
- objects[i].direction="right"
- }
- //twelfth turn
- if(objects[i].y>=660&&objects[i].y<=661&&objects[i].x==580){
- objects[i].y=660
- objects[i].direction="left"
- }
- //popping object
- if(objects[i].y>=800){
- health-=(objects[i].speed*10)-1
- objects.splice(i,1)
- }
- }
- //if up
- if(objects[i].direction=="up"){
- objects[i].y-=objects[i].speed
- ctx.fillStyle=objects[i].level
- ctx.drawImage(objects[i].picture, objects[i].x,objects[i].y,gs,gs)
- //fourth turn
- if(objects[i].y<=260&&objects[i].y>=259&&objects[i].x==260){
- objects[i].y=260
- objects[i].direction="left"
- }
- //sixth turn
- if(objects[i].y<=100&&objects[i].y>=99&&objects[i].x==100){
- objects[i].y=100
- objects[i].direction="right"
- }
- }
- //if left
- if(objects[i].direction=="left"){
- objects[i].x-=objects[i].speed
- ctx.fillStyle=objects[i].level
- ctx.drawImage(objects[i].picture, objects[i].x,objects[i].y,gs,gs)
- //fifth turn
- if(objects[i].x<=100&&objects[i].x>=99&&objects[i].y==260){
- objects[i].x=100
- objects[i].direction="up"
- }
- //ninth turn
- if(objects[i].x<=420&&objects[i].x>=419&&objects[i].y==260){
- objects[i].x=420
- objects[i].direction="down"
- }
- //thirteenth turn
- if(objects[i].x<=260&&objects[i].x>=259&&objects[i].y==660){
- objects[i].x=260
- objects[i].direction="down"
- }
- }
- }
- }
- catch(err){
- }
- //render towers
- for(i=0;i<towers.length;i++){
- ctx.fillStyle="purple"
- ctx.fillRect(towers[i].x,towers[i].y,gs,gs)
- }
- ctx.font="48px serif"
- ctx.fillStyle="black"
- ctx.fillText(health,880,115)
- ctx.font="48px serif"
- ctx.fillStyle="black"
- ctx.fillText(money,880,63)
- //towers
- ctx.fillStyle="purple"
- ctx.fillRect(830,158,gs,gs)
- firstX=830
- firstY=158
- firstW=gs
- firstH=gs
- firstR=100
- if(pickup!=1){
- ctx.fillStyle="purple"
- ctx.fillRect(firstX,firstY,firstW,firstH)
- }
- if(pickup==1){
- ctx.fillStyle="purple"
- ctx.fillRect(mouseX-20,mouseY-20,firstW,firstH)
- }
- if(drawcircle){
- ctx.beginPath();
- ctx.arc(mouseX,mouseY,firstR,0,2*Math.PI);
- ctx.stroke();
- }
- }
- function mousepressD(e){
- mouseCX = e.clientX;
- mouseCY = e.clientY;
- if(mouseCX>=838&&mouseCX<=877&&mouseCY>=166&&mouseCY<=205&&pickup==0){
- pickup=1
- drawcircle=true
- }
- }
- function mousepressU(e){
- mouseCX = e.clientX;
- mouseCY = e.clientY;
- ontop=false
- if(pickup==1){
- //check placement path
- if((mouseCX>20&&mouseCX<70&&mouseCY<390&&mouseCY>20)||(mouseCX>20&&mouseCX<790&&mouseCY>20&&mouseCY<70)||(mouseCY>20&&mouseCY<790&&mouseCX>740&&mouseCX<790)||
- (mouseCY>340&&mouseCY<790&&mouseCX>660&&mouseCX<790)||(mouseCX>20&&mouseCX<230&&mouseCY>580&&mouseCY<790)||(mouseCX>340&&mouseCX<790&&mouseCY>740&&mouseCY<790)||
- (mouseCX>180&&mouseCX<630&&mouseCY>180&&mouseCY<230)||(mouseCX>340&&mouseCX<390&&mouseCY>180&&mouseCY<630)||(mouseCX>340&&mouseCX<550&&mouseCY>500&&mouseCY<630)||
- (mouseCX>20&&mouseCX<230&&mouseCY>340&&mouseCY<390)||(mouseCX>180&&mouseCX<230&&mouseCY>340&&mouseCY<470)||(mouseCX>500&&mouseCX<790&&mouseCY>340&&mouseCY<390)||
- (mouseCX>20&&mouseCX<70&&mouseCY>500&&mouseCY<790)||(mouseCX>260&&mouseCX<310&&mouseCY>580&&mouseCY<630)){
- for(j=0;j<towers.length;j++){
- if(mouseCX>(towers[j].x)-10&&mouseCY>(towers[j].y)-10&&mouseCX<(towers[j].x)+70&&mouseCY<(towers[j].y)+70){
- ontop=true
- }
- }
- if(!ontop){
- pickup=0
- towers.push({x:mouseCX-28,y:mouseCY-28})
- drawcircle=false
- }
- }
- }
- }
- function mousepos(evt){
- rect=canvas.getBoundingClientRect();
- mouseX=evt.clientX - rect.left;
- mouseY=evt.clientY - rect.top
- }
- function keydown(e){
- if(e.keyCode==27){
- pickup=0
- drawcircle=false
- }
- }
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement