Algoritma game Flappy Bird :
Player : single player
Burung : berwarna biru dan
memiliki sifat semangat untuk menghindari pipa
Tombol Spasi : berfungsi untuk
membuat posisi burung naik dan turun
Level : ada 2 level yaitu : level
1 – level yang kecepatan dan lompatan burung pelan hingga skor 2
Level 2 – level yang kecepatan dan lompatan
burung cepat hingga mendapat skor untuk menang 10
Pertama
pemain akan di minta menekan tombol START lalu pemain akan memainkan posisi
burung naik dan turun melewati rintangan berupa pipa-pipa dan tidak boleh
menyentuhnya dengan cara menekan tombol SPASI di Keyboard.lalu saat mencapai
nilai 3 levelnya akan bertambah menjadi level 2 di tandai dengan bergantinya
background dan bertambahnya kecepatan naik turun dari burung serta kecepatan
dari rintangan pipa-pipa.pemain harus mengumpulkan nilai sebanyak-banyaknya
untuk mendapat nilai tertinggi dan nilai untuk menjadi pemenang adalah 10. Jika
menyentuh rintangan berupa pipa-pipa maka pemain akan kalah dan mendapat pesan
berupa nilai yang di dapat dan pemain akan diberi pertanyaan apakah ingin mengulang
kembali game ini?jika iya maka akan kembali ke game seperti awal dan jika tidak
maka akan keluar dari program game ini.
Alur Program
1. Pilih
Start
2. Mulai
permainan dengan tombol spasi untuk
memainkan posisi burung naik dan turun
3. Hindari
pipa dan kumpulkan banyak skor
4. Jika
mendapat skor 10,pemain menjadi pemenang
5. GameOver
jika pemain menyentuh rintangan pipa dan muncul skor yang di dapat
6. Muncul
pertanyaan “Apakah akan memainkan
gamenya kembali?”
A)tidak,akan keluar
B)Iya,akan kembali ke menu start
7. selesai
ini adalah source code dari game flappy bird yang telah saya buat
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Media;
using game.Properties;
namespace game
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<int> Pipe1 = new List<int>();
List<int> Pipe2 = new List<int>();
int PipeWidth = 55;
int PipeDifferentY = 140;
int PipeDifferentX = 180;
bool start = true;
bool running;
int step = 6;
int OriginalX, OriginalY;
bool ResetPipes = false;
int points;
bool inPipe = false;
int score;
int ScoreDifferent;
private void Die()
{
running = false;
timer2.Enabled = false;
timer3.Enabled = false;
button1.Visible = true;
button1.Enabled = true;
ReadAndShowScore();
points = 0;
flappyBird.Location = new Point(OriginalX, OriginalY);
ResetPipes = true;
Pipe1.Clear();
Pipe2.Clear();
}
private void ReadAndShowScore()
{
using (StreamReader reader = new StreamReader("Score.ini"))
{
score = int.Parse(reader.ReadToEnd());
reader.Close();
if (int.Parse(label1.Text) == 0 | int.Parse(label1.Text) > 0)
{
ScoreDifferent = score - int.Parse(label1.Text) + 1;
}
if (score < int.Parse(label1.Text))
{
MessageBox.Show(string.Format("score kamu adalah {1}. melebihi score sebelumnya yang cuma dapat{0}",score, label1.Text), "Flappy Bird", MessageBoxButtons.OK, MessageBoxIcon.Information);
if (MessageBox.Show("Apakah Anda Ingin Mengulang Game Ini ???", "Konfirmasi", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
Application.Exit();
}
using (StreamWriter writer = new StreamWriter("Score.ini"))
{
writer.Write(label1.Text);
writer.Close();
}
}
if (score > int.Parse(label1.Text))
{
MessageBox.Show(string.Format("cuma dapat {0}. score terbaiknya {1}",label1.Text, score), "Flappy Bird", MessageBoxButtons.OK, MessageBoxIcon.Information);
if (MessageBox.Show("Apakah Anda Ingin Mengulang Game Ini ???", "Konfirmasi",MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
Application.Exit();
}
}
if (score == int.Parse(label1.Text))
{
MessageBox.Show(string.Format("score yang kamu dapat {0}, scorenya sama dengan score yang telah ada",score, "Flappy Bird", MessageBoxButtons.OK, MessageBoxIcon.Information));
if (MessageBox.Show("Apakah Anda Ingin Mengulang Game Ini ???", "Konfirmasi", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
Application.Exit();
}
}
}
}
private void StartGame()
{
ResetPipes = false;
timer1.Enabled = true;
timer2.Enabled = true;
timer3.Enabled = true;
Random random = new Random();
int num = random.Next(40, (this.Height - this.PipeDifferentY));
int num1 = num + this.PipeDifferentY;
Pipe1.Clear();
Pipe1.Add(this.Width);
Pipe1.Add(num);
Pipe1.Add(this.Width);
Pipe1.Add(num1);
num = random.Next(40,this.Height - this.PipeDifferentY);
num1 = num + this.PipeDifferentY;
Pipe2.Clear();
Pipe2.Add(this.Width - PipeDifferentX);
Pipe2.Add(num);
Pipe2.Add(this.Width);
Pipe2.Add(num1);
button1.Visible = false;
button1.Enabled = false;
running = true;
Focus();
}
private void Form1_Load(object sender, EventArgs e)
{
OriginalX = flappyBird.Location.X;
OriginalY = flappyBird.Location.Y;
if (!File.Exists("Score.ini"))
{
File.Create("Score.ini").Dispose();
}
}
private void timer1_Tick(object sender, EventArgs e)
{
this.Invalidate();
}
private void button1_Click(object sender, EventArgs e)
{
StartGame();
}
private void timer2_Tick(object sender, EventArgs e)
{
if (Pipe1[0] + PipeWidth <= 0 | start == true)
{
Random rnd = new Random();
int px = this.Width;
int py = rnd.Next(40, (this.Height - PipeDifferentY));
var p2x = px;
var p2y = py + PipeDifferentY;
Pipe1.Clear();
Pipe1.Add(px);
Pipe1.Add(py);
Pipe1.Add(p2x);
Pipe1.Add(p2y);
}
else
{
Pipe1[0] = Pipe1[0] - 2;
Pipe1[2] = Pipe1[2] - 2;
}
if (Pipe2[0] + PipeWidth <= 0)
{
Random rnd = new Random();
int px = this.Width;
int py = rnd.Next(40, (this.Height -PipeDifferentY));
var p2x = px;
var p2y = py + PipeDifferentY;
int [] pl = { px, py, p2x, p2y };
Pipe2.Clear();
Pipe2.Add(px);
Pipe2.Add(py);
Pipe2.Add(p2x);
Pipe2.Add(p2y);
}
else
{
Pipe2[0] = Pipe2[0] - 2;
Pipe2[2] = Pipe2[2] - 2;
}
if (start == true)
{
start = false;
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
if (!ResetPipes && Pipe1.Any() && Pipe2.Any())
{
e.Graphics.FillRectangle(Brushes.DarkGreen, new Rectangle(Pipe1[0], 0, PipeWidth, Pipe1[1]));
e.Graphics.FillRectangle(Brushes.DarkGreen, new Rectangle(Pipe1[0] - 10, Pipe1[3] - PipeDifferentY, 75, 15));
e.Graphics.FillRectangle(Brushes.DarkGreen, new Rectangle(Pipe1[2], Pipe1[3], PipeWidth, this.Height - Pipe1[3]));
e.Graphics.FillRectangle(Brushes.DarkGreen, new Rectangle(Pipe1[2] - 10, Pipe1[3], 75, 15));
e.Graphics.FillRectangle(Brushes.DarkGreen, new Rectangle(Pipe2[0], 0, PipeWidth, Pipe2[1]));
e.Graphics.FillRectangle(Brushes.DarkGreen, new Rectangle(Pipe2[0] - 10, Pipe2[3] - PipeDifferentY, 75, 15));
e.Graphics.FillRectangle(Brushes.DarkGreen, new Rectangle(Pipe2[2], Pipe2[3], PipeWidth, this.Height - Pipe2[3]));
e.Graphics.FillRectangle(Brushes.DarkGreen, new Rectangle(Pipe2[2] - 10, Pipe2[3], 75, 15));
}
}
public void die1()
{
running = false;
timer2.Enabled = false;
timer3.Enabled = false;
button1.Visible = true;
button1.Enabled = true;
points = 0;
flappyBird.Location = new Point(OriginalX, OriginalY);
ResetPipes = true;
Pipe1.Clear();
Pipe2.Clear();
}
private void CheckForPoint()
{
if (Convert.ToInt16(label1.Text) <= 2)
{
BackgroundImage = Image.FromFile("an.png");
label2.Text = "Level 1";
}
else if (Convert.ToInt16(label1.Text) >= 10)
{
die1();
if (MessageBox.Show("Menang dengan mendapat skor 10 ,ingin mengulang game ini???", "Konfirmasi", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
Application.Exit();
}
else
{
StartGame();
}
}
else if (Convert.ToInt16(label1.Text) >= 2)
{
BackgroundImage = Image.FromFile("pp.png");
label2.Text = "Level 2";
}
Rectangle rec = flappyBird.Bounds;
Rectangle rec1 = new Rectangle(Pipe1[2] + 20, Pipe1[3] - PipeDifferentY, 15, PipeDifferentY);
Rectangle rec2 = new Rectangle(Pipe2[2] + 20, Pipe2[3] - PipeDifferentY, 15, PipeDifferentY);
Rectangle intersect1 = Rectangle.Intersect(rec, rec1);
Rectangle intersect2 = Rectangle.Intersect(rec, rec2);
if (!ResetPipes | start)
{
if (intersect1 != Rectangle.Empty | intersect2 != Rectangle.Empty)
{
if (!inPipe)
{
points++;
SoundPlayer sp = new SoundPlayer(game.Properties.Resources.point);
sp.Play();
inPipe = true;
}
}
else
{
inPipe = false;
}
}
}
private void CheckForCollision()
{
Rectangle rec = flappyBird.Bounds;
Rectangle rec1 = new Rectangle(Pipe1[0], 0, PipeWidth, Pipe1[1]);
Rectangle rec2 = new Rectangle(Pipe1[2], Pipe1[3], PipeWidth, this.Height - Pipe1[3]);
Rectangle rec3 = new Rectangle(Pipe2[0], 0, PipeWidth, Pipe2[1]);
Rectangle rec4 = new Rectangle(Pipe2[2], Pipe2[3], PipeWidth, this.Height - Pipe2[3]);
Rectangle intersect1 = Rectangle.Intersect(rec, rec1);
Rectangle intersect2 = Rectangle.Intersect(rec, rec2);
Rectangle intersect3 = Rectangle.Intersect(rec, rec3);
Rectangle intersect4 = Rectangle.Intersect(rec, rec4);
if (!ResetPipes | start)
{
if (intersect1 != Rectangle.Empty | intersect2 != Rectangle.Empty | intersect3 != Rectangle.Empty | intersect4 != Rectangle.Empty)
{
SoundPlayer sp = new SoundPlayer(game.Properties.Resources.collision);
sp.Play();
Die();
}
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Space:
step = -6;
flappyBird.Image = Resources.bird_down;
break;
}
}
private void timer3_Tick(object sender, EventArgs e)
{
flappyBird.Location = new Point(flappyBird.Location.X, flappyBird.Location.Y + step);
if (flappyBird.Location.Y < 0)
{
flappyBird.Location = new Point(flappyBird.Location.X, 75);
}
if (flappyBird.Location.Y + flappyBird.Height > this.ClientSize.Height)
{
flappyBird.Location = new Point(flappyBird.Location.X, this.ClientSize.Height - flappyBird.Height);
}
CheckForCollision();
if (running)
{
CheckForPoint();
}
label1.Text = Convert.ToString(points);
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Space:
step = 6;
flappyBird.Image = Resources.bird_down;
break;
}
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
}
}
Tidak ada komentar:
Posting Komentar