Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace Hw14
- {
- public partial class Form1 : Form
- {
- Random rnd = new Random();
- int ballXspeed = 5;
- int ballYspeed = 5;
- int ballRadius = 20;
- int ballX = 382;
- int ballY = 349;
- private Rectangle UserPlayer;
- int UserPlayerSize = 20;
- private Rectangle bonusBall;
- int bonusBallDiameter = 15;
- bool bonusBallVisible;
- int bonusBallX;
- int bonusBallY;
- int bonusCounter;
- int bonusLiveCounter;
- int userLives = 3;
- int intersects = 0;
- int userScore;
- int scoreMultiplier = 1;
- List<int> scoreList = new List<int>();
- public Form1()
- {
- InitializeComponent();
- }
- private void Pint(object sender, PaintEventArgs e)
- {
- int R = rnd.Next(0, 255);
- int G = rnd.Next(0, 255);
- int B = rnd.Next(0, 255);
- e.Graphics.FillEllipse(new SolidBrush(Color.Red), ballX, ballY, ballRadius, ballRadius);
- e.Graphics.FillRectangle(new SolidBrush(Color.Green), UserPlayer);
- e.Graphics.FillEllipse(new SolidBrush(Color.FromArgb(R, G, B)), bonusBall);
- }
- private void UpdateBallCoordinates()
- {
- ballX += ballXspeed;
- if (ballX >= pictureBox1.Width - ballRadius)
- {
- ballXspeed = -ballXspeed;
- }
- else if (ballX <= 0)
- {
- ballXspeed = -ballXspeed;
- }
- ballY += ballYspeed;
- if (ballY <= 0)
- {
- ballYspeed = -ballYspeed;
- }
- else if (ballY >= pictureBox1.Height - ballRadius)
- {
- ballYspeed = -ballYspeed;
- }
- }
- private void Timer1_Tick(object sender, EventArgs e)
- {
- Intersects();
- UpdateBallCoordinates();
- pictureBox1.Invalidate();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- UserPlayer.Height = UserPlayerSize;
- UserPlayer.Width = UserPlayerSize;
- UserPlayer.X = 50;
- UserPlayer.Y = 50;
- }
- private void Intersects()
- {
- if (intersects == 0)
- {
- if (UserPlayer.IntersectsWith(new Rectangle(ballX, ballY, ballRadius, ballRadius)))
- {
- userLives--;
- userLivesLabel.Text = userLives.ToString();
- intersects = 1;
- }
- }
- else
- {
- intersects++;
- if (intersects == 10)
- intersects = 0;
- }
- if (UserPlayer.IntersectsWith(new Rectangle(bonusBallX, bonusBallY, bonusBallDiameter, bonusBallDiameter)))
- {
- bonusCounter++;
- bonusLiveCounter++;
- bonusCounterLabel.Text = bonusCounter.ToString();
- bonusBallX = -25;
- bonusBallY = -25;
- bonusBall.X = bonusBallX;
- bonusBall.Y = bonusBallY;
- timer2.Stop();
- timer2.Start();
- if (bonusLiveCounter == 5)
- {
- userLives++;
- userLivesLabel.Text = userLives.ToString();
- }
- }
- if (userLives == 0)
- {
- timer1.Stop();
- timer2.Stop();
- pictureBox1.Invalidate();
- gameOverLabel.Visible = true;
- enemySizeButton.Enabled = true;
- enemySpeedButton.Enabled = true;
- startButton.Enabled = true;
- userLives++;
- bonusLiveCounter = 0;
- userLivesLabel.Text = userLives.ToString();
- string line;
- using (StreamReader reader = new StreamReader("yourFileName.txt"))
- {
- while (!reader.EndOfStream)
- {
- line = reader.ReadLine();
- scoreList.Add(Int32.Parse(line));
- }
- }
- }
- }
- private void KeyRegister(object sender, PreviewKeyDownEventArgs e)
- {
- if (e.KeyCode == Keys.Up)
- {
- if (UserPlayer.Y - UserPlayerSize < 0)
- UserPlayer.Y = 0;
- else
- UserPlayer.Y -= 10;
- }
- if (e.KeyCode == Keys.Down)
- {
- if (UserPlayer.Y + UserPlayerSize <= pictureBox1.Height - UserPlayerSize)
- UserPlayer.Y += 10;
- else
- UserPlayer.Y = pictureBox1.Height - UserPlayerSize;
- }
- if (e.KeyCode == Keys.Right)
- {
- if (UserPlayer.X + UserPlayerSize <= pictureBox1.Width - UserPlayerSize)
- UserPlayer.X += 10;
- else
- UserPlayer.X = pictureBox1.Width - UserPlayerSize;
- }
- if (e.KeyCode == Keys.Left)
- {
- if (UserPlayer.X - UserPlayerSize < 0)
- UserPlayer.X = 0;
- else
- UserPlayer.X -= 10;
- }
- }
- private void BonusBall()
- {
- if (bonusBallVisible == true)
- {
- bonusBall.Height = bonusBallDiameter;
- bonusBall.Width = bonusBallDiameter;
- bonusBallX = -25;
- bonusBallY = -25;
- bonusBall.X = bonusBallX;
- bonusBall.Y = bonusBallY;
- bonusBallVisible = false;
- }
- else
- {
- bonusBall.Height = bonusBallDiameter;
- bonusBall.Width = bonusBallDiameter;
- bonusBallX = rnd.Next(pictureBox1.Width - bonusBallDiameter);
- bonusBallY = rnd.Next(pictureBox1.Height - bonusBallDiameter);
- bonusBall.X = bonusBallX;
- bonusBall.Y = bonusBallY;
- bonusBallVisible = true;
- }
- }
- private void Timer2_Tick(object sender, EventArgs e)
- {
- BonusBall();
- Score();
- }
- private void Score()
- {
- userScore = userScore + 1 * scoreMultiplier;
- userScoreLabel.Text = userScore.ToString();
- }
- private void Button1_Click(object sender, EventArgs e)
- {
- if (enemySizeButton.Text == "Small")
- {
- ballRadius = 20;
- }
- else if (enemySizeButton.Text == "Medium")
- {
- ballRadius = 30;
- scoreMultiplier++;
- }
- else if (enemySizeButton.Text == "Big")
- {
- ballRadius = 40;
- scoreMultiplier++;
- scoreMultiplier++;
- }
- if (enemySpeedButton.Text == "Slow")
- {
- ballXspeed = 5;
- ballYspeed = 5;
- }
- else if (enemySpeedButton.Text == "Medium")
- {
- ballXspeed = 10;
- ballYspeed = 10;
- scoreMultiplier++;
- }
- else if (enemySpeedButton.Text == "Fast")
- {
- ballXspeed = 15;
- ballYspeed = 15;
- scoreMultiplier++;
- scoreMultiplier++;
- }
- userLives = 3;
- userScore = 0;
- bonusCounter = 0;
- bonusCounterLabel.Text = bonusCounter.ToString();
- userLivesLabel.Text = userLives.ToString();
- userScoreLabel.Text = userScore.ToString();
- enemySizeButton.Enabled = false;
- enemySpeedButton.Enabled = false;
- startButton.Enabled = false;
- gameOverLabel.Visible = false;
- timer1.Start();
- timer2.Start();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement