Saturday, December 24, 2022

Membuat Game Snake Menggunakan Java

 


Source gamesnake.java :

package com.snake;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Font;

import java.awt.FontMetrics;

import java.awt.Graphics;

import java.awt.Image;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyAdapter;

import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;

import javax.swing.JPanel;

import javax.swing.Timer;

public class Board extends JPanel implements ActionListener {

    private final int B_WIDTH = 300;

    private final int B_HEIGHT = 300;

    private final int DOT_SIZE = 10;

    private final int ALL_DOTS = 900;

    private final int RAND_POS = 29;

    private final int DELAY = 140;

    private final int x[] = new int[ALL_DOTS];

    private final int y[] = new int[ALL_DOTS];

    private int dots;

    private int apple_x;

    private int apple_y;

    private boolean leftDirection = false;

    private boolean rightDirection = true;

    private boolean upDirection = false;

    private boolean downDirection = false;

    private boolean inGame = true;

    private Timer timer;

    private Image ball;

    private Image apple;

    private Image head;


    public Board() {

        initBoard();

    }

    

    private void initBoard() {

        addKeyListener(new TAdapter());

        setBackground(Color.black);

        setFocusable(true);


        setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));

        loadImages();

        initGame();

    }


    private void loadImages() {

        ImageIcon iid = new ImageIcon("src/resources/dot.png");

        ball = iid.getImage();


        ImageIcon iia = new ImageIcon("src/resources/apple.png");

        apple = iia.getImage();


        ImageIcon iih = new ImageIcon("src/resources/head.png");

        head = iih.getImage();

    }


    private void initGame() {

        dots = 3;


        for (int z = 0; z < dots; z++) {

            x[z] = 50 - z * 10;

            y[z] = 50;

        }

        

        locateApple();


        timer = new Timer(DELAY, this);

        timer.start();

    }


    @Override

    public void paintComponent(Graphics g) {

        super.paintComponent(g);

        doDrawing(g);

    }

    

    private void doDrawing(Graphics g) {

        if (inGame) {


            g.drawImage(apple, apple_x, apple_y, this);


            for (int z = 0; z < dots; z++) {

                if (z == 0) {

                    g.drawImage(head, x[z], y[z], this);

                } else {

                    g.drawImage(ball, x[z], y[z], this);

                }

            }


            Toolkit.getDefaultToolkit().sync();


        } else {


            gameOver(g);

        }        

    }


    private void gameOver(Graphics g) {        

        String msg = "Game Over";

        Font small = new Font("Helvetica", Font.BOLD, 14);

        FontMetrics metr = getFontMetrics(small);


        g.setColor(Color.white);

        g.setFont(small);

        g.drawString(msg, (B_WIDTH - metr.stringWidth(msg)) / 2, B_HEIGHT / 2);

    }


    private void checkApple() {

        if ((x[0] == apple_x) && (y[0] == apple_y)) {


            dots++;

            locateApple();

        }

    }


    private void move() {

        for (int z = dots; z > 0; z--) {

            x[z] = x[(z - 1)];

            y[z] = y[(z - 1)];

        }


        if (leftDirection) {

            x[0] -= DOT_SIZE;

        }


        if (rightDirection) {

            x[0] += DOT_SIZE;

        }


        if (upDirection) {

            y[0] -= DOT_SIZE;

        }


        if (downDirection) {

            y[0] += DOT_SIZE;

        }

    }


    private void checkCollision() {

        for (int z = dots; z > 0; z--) {


            if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) {

                inGame = false;

            }

        }


        if (y[0] >= B_HEIGHT) {

            inGame = false;

        }


        if (y[0] < 0) {

            inGame = false;

        }


        if (x[0] >= B_WIDTH) {

            inGame = false;

        }


        if (x[0] < 0) {

            inGame = false;

        }

        

        if (!inGame) {

            timer.stop();

        }

    }


    private void locateApple() {

        int r = (int) (Math.random() * RAND_POS);

        apple_x = ((r * DOT_SIZE));


        r = (int) (Math.random() * RAND_POS);

        apple_y = ((r * DOT_SIZE));

    }


    @Override

    public void actionPerformed(ActionEvent e) {

        if (inGame) {


            checkApple();

            checkCollision();

            move();

        }


        repaint();

    }


    private class TAdapter extends KeyAdapter {

        @Override

        public void keyPressed(KeyEvent e) {


            int key = e.getKeyCode();


            if ((key == KeyEvent.VK_LEFT) && (!rightDirection)) {

                leftDirection = true;

                upDirection = false;

                downDirection = false;

            }


            if ((key == KeyEvent.VK_RIGHT) && (!leftDirection)) {

                rightDirection = true;

                upDirection = false;

                downDirection = false;

            }


            if ((key == KeyEvent.VK_UP) && (!downDirection)) {

                upDirection = true;

                rightDirection = false;

                leftDirection = false;

            }


            if ((key == KeyEvent.VK_DOWN) && (!upDirection)) {

                downDirection = true;

                rightDirection = false;

                leftDirection = false;

            }

        }

    }

}


Hasil :


Membuat Game Snake Menggunakan Javascript

 


Berikut adalah contoh membuat game snake :

1. Buat Function Javascript  pada file : index.html 

<html>

<head>

  <title>Snake Inwepo</title>

</head>

<body>

<!-- Halaman Permainan (HTML) --> 

  <canvas id="gc" width="400" height="400"></canvas> 

<script>
<!-- Script Permainan (Javascript) -->
window.onload=function() {
//Cari elemen dengan nama ID yang telah di tentukan
canv=document.getElementById("gc");
//Konteks Kanvas / Halaman Permainan
ctx=canv.getContext("2d");
//Pastikan bahwa program / web menerima input keyboard
document.addEventListener("keydown",keyPush);
//Atur jeda / interval pada permainan
setInterval(game,1000/15);
}
//Atur Konstanta / Ketetapan Awal Pada Permainan
px=py=10;
gs=tc=20;
ax=ay=15;
xv=yv=0;
trail=[];
tail = 5;
function game() {
//Atur Batasan (Boundary) Pada Halaman Permainan
px+=xv;
py+=yv;
if(px<0) {
px= tc-1;
}
if(px>tc-1) {
px= 0;
}
if(py<0) {
py= tc-1;
}
if(py>tc-1) {
py= 0;
}
//Atur Warna Pada Halaman Permainan
ctx.fillStyle="black";
ctx.fillRect(0,0,canv.width,canv.height);
//Atur Warna Pada Snake
ctx.fillStyle="red";
for(var i=0;i<trail.length;i++) {
ctx.fillRect(trail[i].x*gs,trail[i].y*gs,gs-2,gs-2);
if(trail[i].x==px && trail[i].y==py) {
tail = 5;
}
}
//Atur Panjang Pada Ekor Snake-nya
trail.push({x:px,y:py});
while(trail.length>tail) {
trail.shift();
}
//Jika Snake Memakan Food-nya, Acak Lokasi Food-nya
if(ax==px && ay==py) {
tail++;
ax=Math.floor(Math.random()*tc);
ay=Math.floor(Math.random()*tc);
}
//Atur Warna Pada Food-nya
ctx.fillStyle="purple";
ctx.fillRect(ax*gs,ay*gs,gs-2,gs-2);
}
//Atur Mekanisme Inputan-nya berdasarkan keyCode-nya
function keyPush(evt) {
switch(evt.keyCode) {
//Input Panah Kiri
case 37:
xv=-1;yv=0;
break;
//Input Panah Atas
case 38:
xv=0;yv=-1;
break;
//Input Panah Kanan
case 39:
xv=1;yv=0;
break;
//Input Panah Bawah
case 40:
xv=0;yv=1;
break;
}
}
</script>

</body>

</html>


Jalankan :



Friday, December 23, 2022

Membuat Progress Penjualan Stock Menggunakan Google Sheet

 


1. Masuk akun google. lalu pilih 9 titik pada atas kanan, lalu pilih Sheet


2.  Klik Tanda Plus ( + ), dibawah kanan




3. Buat data stock dan penjualan stock seperti gambar dibawah ini :


4. Kemudian isikan rumus pada :

AI3 : =SUM(D3:AH3) / C3
AJ3 : =SPARKLINE(D3:AH3; {"charttype"\"bar"; "max"\C3 ;"color1"\"green"; "color2"\"yellow"})


5. Kemudian pada kursor posisi cell AI3 dan AJ3, ( menjadi icon + ) tarik sampai ke cell AI18 & AJ18



6. Isikan qty cell kolom D3 - AH3 dan lainnya 



7. Klik Buat Salinan untuk save



8.  Pilih Folder dan kasih nama file, lalu klik Buat Salinan



Memunculkan Simbol & Emoji Pada OS Mac

  Memunculkan Simbol & Emoji  1. Buka aplikasi Pages / Notes pada Macbook. 2. Klik pada Menubar Edit --> Pilih Emoji and Symbols a...