46 Comments

  1. My code for dino run

    #include <SPI.h>
    #include <Adafruit_GFX.h>
    #include <Adafruit_ST7789.h>

    #define TFT_CS 15
    #define TFT_DC 2
    #define TFT_RST 4
    #define TFT_BL 32
    #define BUTTON 0

    Adafruit_ST7789 tft(TFT_CS, TFT_DC, TFT_RST);

    #define BG ST77XX_BLACK
    #define FG ST77XX_WHITE

    const int GROUND = 112;
    const int DINO_X = 25;

    float dinoY = 86;
    float velocity = 0;
    float cactusX = 260;
    float speed = 4.5;

    int score = 0;
    int highScore = 0;
    int frame = 0;
    int cloud1 = 40;
    int cloud2 = 170;
    int obstacleType = 0;

    bool gameOver = false;
    bool wasPressed = false;
    bool jumping = false;

    void drawDino(int x, int y) {
    // Body
    tft.fillRect(x + 5, y + 9, 15, 11, FG);
    tft.fillRect(x + 13, y + 2, 10, 12, FG);
    tft.fillRect(x + 19, y + 5, 9, 7, FG);
    tft.fillRect(x, y + 13, 7, 4, FG);

    // Eye
    tft.drawPixel(x + 21, y + 5, BG);

    // Animated legs
    if (frame % 2 == 0 && !jumping) {
    tft.fillRect(x + 8, y + 19, 5, 6, FG);
    tft.fillRect(x + 17, y + 17, 5, 8, FG);
    } else {
    tft.fillRect(x + 8, y + 17, 5, 8, FG);
    tft.fillRect(x + 17, y + 19, 5, 6, FG);
    }
    }

    void drawCactus(int x) {
    tft.fillRect(x + 5, 88, 7, 24, FG);
    tft.fillRect(x, 96, 5, 8, FG);
    tft.fillRect(x – 3, 96, 4, 4, FG);
    tft.fillRect(x + 12, 93, 5, 9, FG);
    tft.fillRect(x + 12, 93, 4, 4, FG);
    }

    void drawBird(int x, int y) {
    tft.fillRect(x, y, 18, 5, FG);
    if (frame % 2 == 0) {
    tft.drawLine(x + 3, y, x + 8, y – 6, FG);
    tft.drawLine(x + 8, y – 6, x + 13, y, FG);
    } else {
    tft.drawLine(x + 3, y + 4, x + 8, y + 9, FG);
    tft.drawLine(x + 8, y + 9, x + 13, y + 4, FG);
    }
    }

    void drawCloud(int x, int y) {
    tft.fillCircle(x, y, 5, FG);
    tft.fillCircle(x + 7, y – 3, 7, FG);
    tft.fillCircle(x + 15, y, 5, FG);
    tft.fillRect(x, y, 15, 5, FG);
    }

    void drawGround() {
    tft.drawLine(0, GROUND, 240, GROUND, FG);
    for (int i = 0; i < 240; i += 35) {
    tft.drawPixel(i, 119, FG);
    tft.drawPixel(i + 10, 117, FG);
    }
    }

    void drawScore() {
    tft.fillRect(155, 3, 83, 23, BG);
    tft.setTextColor(FG);
    tft.setTextSize(1);
    tft.setCursor(158, 5);
    tft.print("HI ");
    tft.print(highScore);
    tft.setCursor(158, 16);
    tft.print("SCORE ");
    tft.print(score);
    }

    void restartGame() {
    dinoY = 86;
    velocity = 0;
    cactusX = 260;
    speed = 4.5;
    score = 0;
    frame = 0;
    obstacleType = 0;
    gameOver = false;
    jumping = false;

    tft.fillScreen(BG);
    drawGround();
    drawCloud(cloud1, 35);
    drawCloud(cloud2, 50);
    drawDino(DINO_X, 86);
    drawCactus(260);
    drawScore();
    }

    void gameOverScreen() {
    tft.fillRect(35, 30, 175, 70, BG);
    tft.setTextColor(FG);
    tft.setTextSize(2);
    tft.setCursor(43, 35);
    tft.print("GAME OVER");

    tft.setTextSize(1);
    tft.setCursor(80, 62);
    tft.print("SCORE: ");
    tft.print(score);

    tft.setCursor(60, 85);
    tft.print("BOOT TO RETRY");
    }

    void setup() {
    pinMode(TFT_BL, OUTPUT);
    digitalWrite(TFT_BL, HIGH);
    pinMode(BUTTON, INPUT_PULLUP);

    tft.init(135, 240);
    tft.setRotation(1);
    tft.fillScreen(BG);

    tft.setTextColor(FG);
    tft.setTextSize(2);
    tft.setCursor(35, 45);
    tft.println("DINO RUN");

    delay(1000);
    restartGame();
    }

    void loop() {
    bool pressed = digitalRead(BUTTON) == LOW;
    bool newPress = pressed && !wasPressed;
    wasPressed = pressed;

    if (gameOver) {
    if (newPress) restartGame();
    delay(20);
    return;
    }

    if (newPress && dinoY >= 86) {
    velocity = -8.5;
    jumping = true;
    }

    float oldY = dinoY;
    float oldX = cactusX;

    velocity += 0.45;
    dinoY += velocity;

    if (dinoY >= 86) {
    dinoY = 86;
    velocity = 0;
    jumping = false;
    }

    cactusX -= speed;

    if (cactusX < -20) {
    cactusX = 240 + random(30, 100);
    score++;
    speed += 0.15;
    obstacleType = random(0, 2);
    }

    // Collision detection
    if (cactusX < DINO_X + 25 &&
    cactusX + 17 > DINO_X + 5 &&
    dinoY + 23 > 88) {
    gameOver = true;
    }

    // Clear old dinosaur and obstacle positions
    tft.fillRect(DINO_X – 3, (int)oldY – 8, 35, 38, BG);
    tft.fillRect((int)oldX – 5, 75, 30, 40, BG);

    // Move clouds
    cloud1–;
    cloud2–;

    if (cloud1 < -20) cloud1 = 245;
    if (cloud2 < -20) cloud2 = 245;

    // Draw scene
    drawCloud(cloud1, 35);
    drawCloud(cloud2, 50);
    drawDino(DINO_X, (int)dinoY);

    if (obstacleType == 0) {
    drawCactus((int)cactusX);
    } else {
    drawBird((int)cactusX, 88);
    }

    drawGround();
    drawScore();

    frame++;

    if (score > highScore) {
    highScore = score;
    }

    if (gameOver) {
    gameOverScreen();
    }

    delay(25);
    }

  2. You can make coding understandable. But understandability doesn't translate to line code.

    coding depending entirely on line logic to input those instructions to the computer.

    I've made 4 projects now. Working on my fifth. I'm going going to release 2 to Google play next year.

    You can explain computer science and whatnot but unless they are curious in line logic and syntax then those concepts you teach them won't magically convert it some line logic

    Also python .

    It's easy to read sure

    But it is disgusting. It has zero structure.

    you don't know when an class ends or even begins for that matter.

    It's only good for one time block thing. Usually an short script (like what I did for MittensEternal, the first 4000 elo engine in the world). But that's it.

    If they can read python. upgrade them to java. Far far better. Yes more focused on variables. But it gives you more control over what you are telling the computer. If there's an error. You know which block it is in.

  3. Unpopular opinion: Javascript is the easiest programming language, with the easiest syntax, because you don't need to be extremely careful with all the indents for example. Also not to mention, to build a real app in Python (not console one), you need to learn some libraries, meanwhile basic Javascript (the one you use in browser) already lets you build real non console apps with the help of HTML and CSS

Leave a Reply

Your email address will not be published. Required fields are marked *