0% found this document useful (0 votes)
3 views

Program

Uploaded by

danthereaL
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Program

Uploaded by

danthereaL
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Program.

cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Breakout
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Breakout
{
public partial class Form1 : Form
{

bool duteStanga;
bool duteDreapta;
bool isGameOver;

int scor;
int mingeX;
int mingeY;
int vitezaPlayer;

Random rnd = new Random();

PictureBox[] sirBlockuri;

public Form1()
{
InitializeComponent();

scoateBlockui();
}

private void setupGame()


{
isGameOver = false;
scor = 0;
mingeX = 5;
mingeY = 5;
vitezaPlayer = 10;
txtScore.Text = "Scor: " + scor;

ball.Left = 376;
ball.Top = 328;

player.Left = 347;

gameTImer.Start();

foreach (Control x in this.Controls)


{
if (x is PictureBox && (string)x.Tag == "blocks")
{
x.BackColor = Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256));
}
}
}

private void gameOver(string message)


{
isGameOver = true;
gameTImer.Stop();

txtScore.Text = "Scor: " + scor + " " + message;


}

private void scoateBlockui()

{
sirBlockuri = new PictureBox[15];

int a = 0;

int top = 50;


int left = 100;

for (int i = 0; i < sirBlockuri.Length; i++)


{
sirBlockuri[i] = new PictureBox();
sirBlockuri[i].Height = 32;
sirBlockuri[i].Width = 100;
sirBlockuri[i].Tag = "blocks";
sirBlockuri[i].BackColor = Color.White;
if (a == 5)
{
top = top + 50;
left = 100;
a = 0;
}

if (a < 5)
{
a++;
sirBlockuri[i].Left = left;
sirBlockuri[i].Top = top;
this.Controls.Add(sirBlockuri[i]);
left = left + 130;
}

}
setupGame();
}

private void puneBlockuri()


{
foreach (PictureBox x in sirBlockuri)
{
this.Controls.Remove(x);
}
}

private void mainGameTimerEvent(object sender, EventArgs e)


{
txtScore.Text = "Scor: " + scor;

if (duteStanga == true && player.Left > 0)


{
player.Left -= vitezaPlayer;
}

if (duteDreapta == true && player.Left < 700)


{
player.Left += vitezaPlayer;
}

ball.Left += mingeX;
ball.Top += mingeY;

if (ball.Left < 0 || ball.Left > 775)


{
mingeX = -mingeX;
}
if (ball.Top < 0)
{
mingeY = -mingeY;
}
if (ball.Bounds.IntersectsWith(player.Bounds))
{

ball.Top = 463;

mingeY = rnd.Next(5, 12) * -1;

if (mingeX < 0)
{
mingeX = rnd.Next(5, 12) * -1;
}
else
{
mingeX = rnd.Next(5, 12);
}
}

foreach (Control x in this.Controls)


{
if (x is PictureBox && (string)x.Tag == "blocks")
{
if (ball.Bounds.IntersectsWith(x.Bounds))
{
scor += 1;

mingeY = -mingeY;

this.Controls.Remove(x);
}
}

if (scor == 15)
{
gameOver("Ai castigat!! Apasa Enter ca sa joci din nou !");
}

if (ball.Top > 580)


{
gameOver("Ai pierdut!! Apasa Enter ca sa incerci din nou");
}

private void keyisdown(object sender, KeyEventArgs e)


{

if (e.KeyCode == Keys.Left)
{
duteStanga = true;
}
if (e.KeyCode == Keys.Right)
{
duteDreapta = true;
}

private void keyisup(object sender, KeyEventArgs e)


{
if (e.KeyCode == Keys.Left)
{
duteStanga = false;
}
if (e.KeyCode == Keys.Right)
{
duteDreapta = false;
}
if (e.KeyCode == Keys.Enter && isGameOver == true)
{
puneBlockuri();
scoateBlockui();
}
}
}
}

Form1.Designer.cs
namespace Breakout
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise,
false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.txtScore = new System.Windows.Forms.Label();
this.player = new System.Windows.Forms.PictureBox();
this.ball = new System.Windows.Forms.PictureBox();
this.gameTImer = new System.Windows.Forms.Timer(this.components);
((System.ComponentModel.ISupportInitialize)(this.player)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.ball)).BeginInit();
this.SuspendLayout();
//
// txtScore
//
this.txtScore.AutoSize = true;
this.txtScore.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F,
System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.txtScore.ForeColor = System.Drawing.Color.White;
this.txtScore.Location = new System.Drawing.Point(17, 16);
this.txtScore.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.txtScore.Name = "txtScore";
this.txtScore.Size = new System.Drawing.Size(95, 29);
this.txtScore.TabIndex = 0;
this.txtScore.Text = "Scor: 0";
//
// player
//
this.player.BackColor = System.Drawing.Color.MistyRose;
this.player.Location = new System.Drawing.Point(463, 606);
this.player.Margin = new System.Windows.Forms.Padding(4);
this.player.Name = "player";
this.player.Size = new System.Drawing.Size(133, 39);
this.player.TabIndex = 1;
this.player.TabStop = false;
//
// ball
//
this.ball.BackColor = System.Drawing.Color.Red;
this.ball.Location = new System.Drawing.Point(503, 366);
this.ball.Margin = new System.Windows.Forms.Padding(4);
this.ball.Name = "ball";
this.ball.Size = new System.Drawing.Size(33, 28);
this.ball.TabIndex = 1;
this.ball.TabStop = false;
//
// gameTImer
//
this.gameTImer.Interval = 20;
this.gameTImer.Tick += new System.EventHandler(this.mainGameTimerEvent);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(0)))),
((int)(((byte)(64)))));
this.ClientSize = new System.Drawing.Size(1067, 660);
this.Controls.Add(this.ball);
this.Controls.Add(this.player);
this.Controls.Add(this.txtScore);
this.Margin = new System.Windows.Forms.Padding(4);
this.Name = "Form1";
this.Text = "BreakOut";
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.keyisdown);
this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.keyisup);
((System.ComponentModel.ISupportInitialize)(this.player)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.ball)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();

#endregion

private System.Windows.Forms.Label txtScore;


private System.Windows.Forms.PictureBox player;
private System.Windows.Forms.PictureBox ball;
private System.Windows.Forms.Timer gameTImer;
}
}

You might also like