U3L27 Code Your Own Game
Flow Through or wrap around code
if (sprite .x > 400) {
sprite .x = 0;
}
if (sprite .y > 400) {
sprite .y = 0;
}
if (sprite .x < 0) {
sprite .x = 400;
}
if (sprite .y < 0) {
sprite.y = 400;
}
MTTB MFT
Javascript frequently used code
To make your sprite larger or smaller use this code and adjust the number up or down
sprite.scale = 0.5;
To make your sprite rotate
sprite.rotation = 45;
Change the transparency of the sprite
sprite.alpha = 0.5
Starry Background in a Draw Loop U3 L11 Dot A
background("black");
ellipse(randomNumber(0,400),randomNumber(0,400),5,5);
Counter growing from bottom left corner U3 L12
var counter = 0;
function draw(){
background("white");
textSize(counter);
text(counter, 0, 400);
counter = counter + 1;
}
Bubbles from the bottom U3 L12
var bubbles=400;
//inside the draw loop
bubbles = bubbles - 2;
noFill();
stroke("white");
strokeWeight(3);
ellipse(65,bubbles,25);
ellipse(225,bubbles + 25,25);
ellipse(350,bubbles + 100,25);
Instruction Background and Play Background
Unit 3 Lesson 16 Dot 8
Adding A Timer to your game Code.Org Forum
textSize(50);
textAlign(CENTER, CENTER);
var time = 10 - World.seconds;
if (time > 0) {
text(time, 200, 200);
} else {
text("Time's Up!", 200, 200);
}
Adding Points To Your Game U3 L19 Dot 8
var points = 0;
if (ghost.isTouching(coin)) {
points = points + 1;
coin.x = randomNumber(50, 350);
coin.y = randomNumber(50, 350);
}
if (ghost.isTouching(coin2)) {
points = points - 1;
coin2.x = randomNumber(50, 350);
coin2.y = randomNumber(50, 350):
}
if (points > 20) {
background(rgb(0, 255, 0));
coin.visible = false;
coin2.visible = false;
ghost.visible = false;
fill("blue");
textSize(90);
text("You Win!" , 200, 200);
Borders around the play area
Borders around the play area left and right edge
if (fish.x > 400) {
fish.setAnimation("fishL");
fish.velocityX = -50;
}
if (fish.x < 0) {
fish.setAnimation("fishR");
fish.velocityX = 50;
}
Borders around the play area Up Down Left Right (sprite doesn't leave screen)
if (character.x < 50) {
character.velocityX = +10;
}
if (character.x > 350) {
character.velocityX = -10;
}
if (character.y < 75) {
character.velocityY = +10;
}
if (character.y > 325) {
character.velocityY = -10;
}
2 scores for two players
var goal=createSprite(200,28,100,20);
goal.shapeColor="yellow";
goal.setAnimation("goal_post");
goal.scale=0.5;
var goal2=createSprite(200,372,100,20);
goal2.shapeColor="yellow";
goal2.setAnimation("goal_post");
goal2.scale=0.5;
var striker=createSprite(200,200,10,10);
striker.setAnimation("football.jpg_1");
striker.scale=0.1;
var computerscore = 0;
var playerscore=0;
var gamestate="serve";
var playermallet=createSprite(200,50,50,10);
playermallet.shapeColor="black";
playermallet.velocityY=0;
playermallet.setAnimation("kid_42_1");
playermallet.scale=0.1;
var computermallet=createSprite(200,305,50,10);
computermallet.shapeColor="black";
computermallet.setAnimation("gray_robot_2_1");
computermallet.scale=0.05;
function draw() {
background("palegreen");
playermallet.setVelocity(0,0);
if(gamestate==="serve"){
text("press space to serve",150,180);}
text(computerscore,20,220);
if(keyDown("space")&&gamestate==="serve"){
gamestate="play";
striker.setVelocity(3,-4);
}
text(playerscore,20,180);
playermallet.velocityX=0;
playermallet.velocityY=0;
computermallet.velocityX=0;
computermallet.velocityY=0;
if(striker.isTouching(goal)||striker.isTouching(goal2)){
if(striker.isTouching(goal)){
computerscore = computerscore+1;
if(striker.isTouching(goal2)){
playerscore = playerscore+1;
}
gamestate="serve";
striker.x=200;
striker.y=200;
striker.setVelocity(0,0);
}
if(computerscore===5||playerscore===5){
gamestate="over";
text("gameover",180,200);
text("press r for reset",180,220);
}
if(gamestate==="over"&&keyDown("r")){
computerscore=0;
playerscore=0;
gamestate="serve";
}
if(striker.isTouching(goal)||striker.isTouching(goal2)){
striker.x = 200;
striker.y = 200;
}
if(playermallet.isTouching(striker)){
striker.velocityX=-2;
striker.velocityY=3;
}
}
createEdgeSprites();
playermallet.bounceOff(edges);
striker.bounceOff(playermallet);
playermallet.bounceOff(goal);
playermallet.bounceOff(goal2);
computermallet.bounceOff(edges);
computermallet.bounceOff(goal);
computermallet.bounceOff(goal2);
computermallet.bounceOff(playermallet);
//striker.bounceOff(playermallet);
striker.bounceOff(computermallet);
striker.bounceOff(edges);
striker.bounceOff(goal);
striker.bounceOff(goal2);
if(keyDown("left")){
playermallet.x = playermallet.x-10;
}
if(keyDown("right")){
playermallet.x = playermallet.x+10;
}
if(keyDown("UP_ARROW")){
playermallet.velocityX=0;
playermallet.velocityY=-6;
}
if(keyDown("DOWN_ARROW")){
playermallet.velocityX=0;
playermallet.velocityY=6;
}
for (var i=0; i<400; i=i+20){
line(i,200,i+10,200);
}
computermallet.x=striker.x;
drawSprites();
}