Advertisement
Guest User

Hw13

a guest
Dec 7th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.58 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11.  
  12. namespace Hw14
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         Random rnd = new Random();
  17.         int ballXspeed = 5;
  18.         int ballYspeed = 5;
  19.         int ballRadius = 20;
  20.         int ballX = 382;
  21.         int ballY = 349;
  22.  
  23.         private Rectangle UserPlayer;
  24.         int UserPlayerSize = 20;
  25.  
  26.         private Rectangle bonusBall;
  27.         int bonusBallDiameter = 15;
  28.         bool bonusBallVisible;
  29.         int bonusBallX;
  30.         int bonusBallY;
  31.         int bonusCounter;
  32.         int bonusLiveCounter;
  33.  
  34.         int userLives = 3;
  35.  
  36.         int intersects = 0;
  37.  
  38.         int userScore;
  39.         int scoreMultiplier = 1;
  40.  
  41.         List<int> scoreList = new List<int>();
  42.  
  43.         public Form1()
  44.         {
  45.             InitializeComponent();
  46.         }
  47.  
  48.         private void Pint(object sender, PaintEventArgs e)
  49.         {
  50.             int R = rnd.Next(0, 255);
  51.             int G = rnd.Next(0, 255);
  52.             int B = rnd.Next(0, 255);
  53.  
  54.             e.Graphics.FillEllipse(new SolidBrush(Color.Red), ballX, ballY, ballRadius, ballRadius);
  55.             e.Graphics.FillRectangle(new SolidBrush(Color.Green), UserPlayer);
  56.             e.Graphics.FillEllipse(new SolidBrush(Color.FromArgb(R, G, B)), bonusBall);
  57.         }
  58.  
  59.         private void UpdateBallCoordinates()
  60.         {
  61.             ballX += ballXspeed;
  62.  
  63.             if (ballX >= pictureBox1.Width - ballRadius)
  64.             {
  65.                 ballXspeed = -ballXspeed;
  66.             }
  67.             else if (ballX <= 0)
  68.             {
  69.                 ballXspeed = -ballXspeed;
  70.             }
  71.  
  72.             ballY += ballYspeed;
  73.  
  74.             if (ballY <= 0)
  75.             {
  76.                 ballYspeed = -ballYspeed;
  77.             }
  78.             else if (ballY >= pictureBox1.Height - ballRadius)
  79.             {
  80.                 ballYspeed = -ballYspeed;
  81.             }
  82.         }
  83.  
  84.         private void Timer1_Tick(object sender, EventArgs e)
  85.         {        
  86.             Intersects();
  87.             UpdateBallCoordinates();
  88.             pictureBox1.Invalidate();            
  89.         }
  90.  
  91.         private void Form1_Load(object sender, EventArgs e)
  92.         {
  93.             UserPlayer.Height = UserPlayerSize;
  94.             UserPlayer.Width = UserPlayerSize;
  95.             UserPlayer.X = 50;
  96.             UserPlayer.Y = 50;
  97.         }
  98.  
  99.         private void Intersects()
  100.         {
  101.             if (intersects == 0)
  102.             {
  103.                 if (UserPlayer.IntersectsWith(new Rectangle(ballX, ballY, ballRadius, ballRadius)))
  104.                 {
  105.                     userLives--;
  106.                     userLivesLabel.Text = userLives.ToString();
  107.                     intersects = 1;
  108.                 }
  109.             }
  110.             else
  111.             {
  112.                 intersects++;
  113.                 if (intersects == 10)
  114.                     intersects = 0;              
  115.             }
  116.  
  117.  
  118.             if (UserPlayer.IntersectsWith(new Rectangle(bonusBallX, bonusBallY, bonusBallDiameter, bonusBallDiameter)))
  119.             {
  120.                 bonusCounter++;
  121.                 bonusLiveCounter++;
  122.                 bonusCounterLabel.Text = bonusCounter.ToString();
  123.                 bonusBallX = -25;
  124.                 bonusBallY = -25;
  125.                 bonusBall.X = bonusBallX;
  126.                 bonusBall.Y = bonusBallY;
  127.                 timer2.Stop();
  128.                 timer2.Start();
  129.  
  130.                 if (bonusLiveCounter == 5)
  131.                 {
  132.                     userLives++;
  133.                     userLivesLabel.Text = userLives.ToString();
  134.                 }
  135.             }
  136.  
  137.             if (userLives == 0)
  138.             {
  139.                 timer1.Stop();
  140.                 timer2.Stop();
  141.                 pictureBox1.Invalidate();
  142.                 gameOverLabel.Visible = true;
  143.                 enemySizeButton.Enabled = true;
  144.                 enemySpeedButton.Enabled = true;
  145.                 startButton.Enabled = true;
  146.                 userLives++;
  147.                 bonusLiveCounter = 0;
  148.                 userLivesLabel.Text = userLives.ToString();
  149.  
  150.                 string line;
  151.                 using (StreamReader reader = new StreamReader("yourFileName.txt"))
  152.                 {
  153.                     while (!reader.EndOfStream)
  154.                     {
  155.                         line = reader.ReadLine();
  156.                         scoreList.Add(Int32.Parse(line));
  157.                     }
  158.                 }
  159.  
  160.                
  161.             }
  162.  
  163.         }
  164.  
  165.         private void KeyRegister(object sender, PreviewKeyDownEventArgs e)
  166.         {
  167.             if (e.KeyCode == Keys.Up)
  168.             {
  169.                 if (UserPlayer.Y - UserPlayerSize < 0)
  170.                     UserPlayer.Y = 0;              
  171.                 else
  172.                     UserPlayer.Y -= 10;                
  173.             }
  174.  
  175.             if (e.KeyCode == Keys.Down)
  176.             {
  177.                 if (UserPlayer.Y + UserPlayerSize <= pictureBox1.Height - UserPlayerSize)
  178.                     UserPlayer.Y += 10;
  179.                 else
  180.                     UserPlayer.Y = pictureBox1.Height - UserPlayerSize;                
  181.             }
  182.  
  183.  
  184.             if (e.KeyCode == Keys.Right)
  185.             {
  186.                 if (UserPlayer.X + UserPlayerSize <= pictureBox1.Width - UserPlayerSize)
  187.                     UserPlayer.X += 10;
  188.                 else              
  189.                     UserPlayer.X = pictureBox1.Width - UserPlayerSize;              
  190.             }
  191.  
  192.             if (e.KeyCode == Keys.Left)
  193.             {
  194.                 if (UserPlayer.X  - UserPlayerSize < 0)
  195.                     UserPlayer.X = 0;
  196.                 else
  197.                     UserPlayer.X -= 10;
  198.             }
  199.         }
  200.  
  201.  
  202.         private void BonusBall()
  203.         {
  204.             if (bonusBallVisible == true)
  205.             {
  206.                 bonusBall.Height = bonusBallDiameter;
  207.                 bonusBall.Width = bonusBallDiameter;
  208.                 bonusBallX = -25;
  209.                 bonusBallY = -25;
  210.                 bonusBall.X = bonusBallX;
  211.                 bonusBall.Y = bonusBallY;
  212.                 bonusBallVisible = false;
  213.             }
  214.             else
  215.             {
  216.                 bonusBall.Height = bonusBallDiameter;
  217.                 bonusBall.Width = bonusBallDiameter;
  218.                 bonusBallX = rnd.Next(pictureBox1.Width - bonusBallDiameter);
  219.                 bonusBallY = rnd.Next(pictureBox1.Height - bonusBallDiameter);
  220.                 bonusBall.X = bonusBallX;
  221.                 bonusBall.Y = bonusBallY;
  222.                 bonusBallVisible = true;
  223.  
  224.             }
  225.         }
  226.  
  227.         private void Timer2_Tick(object sender, EventArgs e)
  228.         {
  229.             BonusBall();
  230.             Score();
  231.         }
  232.  
  233.         private void Score()
  234.         {
  235.             userScore = userScore + 1 * scoreMultiplier;
  236.             userScoreLabel.Text = userScore.ToString();
  237.         }
  238.  
  239.         private void Button1_Click(object sender, EventArgs e)
  240.         {
  241.             if (enemySizeButton.Text == "Small")
  242.             {
  243.                 ballRadius = 20;
  244.             }
  245.             else if (enemySizeButton.Text == "Medium")
  246.             {
  247.                 ballRadius = 30;
  248.                 scoreMultiplier++;
  249.             }
  250.             else if (enemySizeButton.Text == "Big")
  251.             {
  252.                 ballRadius = 40;
  253.                 scoreMultiplier++;
  254.                 scoreMultiplier++;
  255.             }
  256.  
  257.  
  258.             if (enemySpeedButton.Text == "Slow")
  259.             {
  260.                 ballXspeed = 5;
  261.                 ballYspeed = 5;
  262.             }
  263.             else if (enemySpeedButton.Text == "Medium")
  264.             {
  265.                 ballXspeed = 10;
  266.                 ballYspeed = 10;
  267.                 scoreMultiplier++;
  268.             }
  269.             else if (enemySpeedButton.Text == "Fast")
  270.             {
  271.                 ballXspeed = 15;
  272.                 ballYspeed = 15;
  273.                 scoreMultiplier++;
  274.                 scoreMultiplier++;
  275.             }
  276.  
  277.  
  278.  
  279.             userLives = 3;
  280.             userScore = 0;
  281.             bonusCounter = 0;
  282.             bonusCounterLabel.Text = bonusCounter.ToString();
  283.             userLivesLabel.Text = userLives.ToString();
  284.             userScoreLabel.Text = userScore.ToString();
  285.  
  286.             enemySizeButton.Enabled = false;
  287.             enemySpeedButton.Enabled = false;
  288.             startButton.Enabled = false;
  289.             gameOverLabel.Visible = false;
  290.  
  291.             timer1.Start();
  292.             timer2.Start();
  293.         }
  294.     }
  295. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement