Advertisement
Nojus_Globys

mar14

Mar 14th, 2023
748
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.88 KB | None | 0 0
  1.  
  2. PImage
  3.     player,
  4.     donut;
  5.  
  6. float
  7.     playerX,
  8.     playerY,
  9.     playerSize,
  10.     step,
  11.    
  12.     donutX,
  13.     donutY,
  14.     donutSize;
  15.  
  16. int score = 0;
  17.  
  18. boolean
  19.   left, // false
  20.   right, // false
  21.   up, // false
  22.   down; // false
  23.  
  24. void setup () {
  25.     size(1200, 600);
  26.     imageMode (CENTER);
  27.  
  28.     player = loadImage("data/player.png");
  29.     donut = loadImage("data/donut.png");
  30.    
  31.     playerX = width * 0.5;
  32.     playerY = height * 0.5;
  33.     playerSize = height * 0.15;
  34.     step = height * 0.02;
  35.  
  36.     donutX = donutY = donutSize = height * 0.1;
  37. }
  38.  
  39. void donut () {
  40.    image (donut, donutX, donutY, donutSize, donutSize);
  41.    
  42.    //float a = donutY - playerY;
  43.    //float b = donutX - playerX;
  44.    //float c = sqrt (a * a + b * b);
  45.    
  46.    float c = dist (donutX, donutY, playerX, playerY);
  47.    
  48.    if (c < (donutSize + playerSize)/2) {
  49.      donutX = random (width - donutSize / 2);
  50.      donutY = random (height - donutSize / 2);
  51.      //score = score + 1;
  52.      //score += 1;
  53.      ++score;
  54.    }
  55. }
  56.  
  57. void player () {
  58.     image (player, playerX, playerY, playerSize, playerSize);
  59.    
  60.     if (left) // if (left == true)
  61.       playerX -= step;
  62.     if (right)
  63.       playerX += step;
  64.     if (up)
  65.       playerY -= step;
  66.     if (down)
  67.       playerY += step;
  68. }
  69.  
  70. void keyPressed () {
  71.   if (key == 'a')
  72.    left = true;
  73.   if (key == 'd')
  74.     right = true;
  75.   if (key == 'w')
  76.     up = true;
  77.   if (key == 's')
  78.     down = true;
  79. }
  80.  
  81. void keyReleased () {
  82.   left = right = up = down = false;
  83. }
  84.  
  85. void score () {
  86.   fill (0); // tekstas bus juodas
  87.   textSize (donutSize); // teksto dydis bus lygus spurgos dydžiui
  88.   text (score, width - donutSize * 1.5, donutSize);
  89.  
  90.   if (score == 3){
  91.     background (100);
  92.     textAlign (CENTER);
  93.     text ("Game Over", width / 2, height / 2);
  94.   }
  95. }
  96.  
  97. void draw () {
  98.   background (255);
  99.   donut ();
  100.   player ();
  101.   score ();
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement