Advertisement
Nojus_Globys

dino

Feb 22nd, 2023
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.72 KB | Software | 0 0
  1.  
  2. PImage
  3.     dino,
  4.     cactus,
  5.     gameover;
  6.  
  7. int
  8.     score = 0,
  9.     road,
  10.  
  11.     dinoX,
  12.     dinoY,
  13.     dinoStep,
  14.     dinoMaxStep = 0,
  15.  
  16.     cactusX,
  17.     cactusY,
  18.     cactusSpeed;
  19.  
  20. void setup() {
  21.     size (1200, 600);
  22.     fill (0);
  23.  
  24.     dino = loadImage("dino.png");
  25.     cactus = loadImage("cactus.png");
  26.     gameover = loadImage("gameover.jpg");
  27.  
  28.     road = int(height * 0.6);
  29.  
  30.     dinoX = dino.width;
  31.     dinoY = road - dino.height - cactus.height;
  32.  
  33.     cactusX = width + cactus.width;
  34.     cactusY = road - cactus.height;
  35.     cactusSpeed = width / 70;
  36.    
  37. }
  38.  
  39. void cactus() {
  40.     image(cactus, cactusX, cactusY);
  41.     cactusX -= cactusSpeed;
  42.     if (cactusX < -cactus.width) {
  43.         ++score;
  44.         cactusX = width + cactus.width;
  45.     }
  46. }
  47.  
  48. void dino () {
  49.     image(dino, dinoX, dinoY);
  50.     dinoY += dinoStep;
  51.     dinoStep += 2;
  52.  
  53.     if (dinoY > road - dino.height) {
  54.         dinoY = road - dino.height;
  55.         if (dinoMaxStep == 0)
  56.             dinoMaxStep = dinoStep;
  57.     }
  58. }
  59.  
  60. boolean gameOver () {
  61.     if ((dinoY > road - cactus.height) && (dinoX + dino.width / 2 > cactusX) && (dinoX < cactusX + cactus.width/2))
  62.         return true;
  63.     else
  64.         return false;
  65. }
  66.  
  67. void keyPressed () {
  68.     if (key == ' ' && dinoY == road - dino.height)
  69.         dinoStep = -dinoMaxStep;
  70. }
  71.  
  72. void mouseClicked() {
  73.     if (gameOver()) {
  74.         score = 0;
  75.         cactusX = width + cactus.width;
  76.         dinoX = dino.width;
  77.     }
  78. }
  79.  
  80. void draw() {
  81.     background(255);
  82.     rect(0, road, width, height * 0.001);
  83.     text("Score: " + score, width * 0.9, height * 0.05);
  84.    
  85.     if (!gameOver()) {
  86.         dino ();
  87.         cactus();
  88.     } else
  89.         image(gameover, 0, 0, width, height);
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement