Advertisement
TermSpar

2D Platformer

Oct 16th, 2018
3,081
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.50 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.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace PlatoformerTut
  12. {
  13.     public partial class Platformer : Form
  14.     {
  15.  
  16.         bool isJumping = false;
  17.         List<Coin> cList = new List<Coin>();
  18.         int score = 0;
  19.  
  20.         public Platformer()
  21.         {
  22.             InitializeComponent();
  23.         }
  24.  
  25.         private void tmrGravity_Tick(object sender, EventArgs e)
  26.         {
  27.             if(!pbPlayer.Bounds.IntersectsWith(pbGround.Bounds) && isJumping == false)
  28.             {
  29.                 pbPlayer.Top += 10;
  30.             }
  31.         }
  32.  
  33.         private void tmrUp_Tick(object sender, EventArgs e)
  34.         {
  35.             pbPlayer.Top -= 10;
  36.             isJumping = true;
  37.         }
  38.  
  39.         private void tmrRight_Tick(object sender, EventArgs e)
  40.         {
  41.             pbPlayer.Left += 10;
  42.         }
  43.  
  44.         private void tmrLeft_Tick(object sender, EventArgs e)
  45.         {
  46.             pbPlayer.Left -= 10;
  47.         }
  48.  
  49.         private void Platformer_KeyDown(object sender, KeyEventArgs e)
  50.         {
  51.             if(e.KeyCode == Keys.Up)
  52.             {
  53.                 tmrUp.Start();
  54.             }
  55.             else if (e.KeyCode == Keys.Right)
  56.             {
  57.                 tmrRight.Start();
  58.             }
  59.             else if (e.KeyCode == Keys.Left)
  60.             {
  61.                 tmrLeft.Start();
  62.             }
  63.         }
  64.  
  65.         private void Platformer_KeyUp(object sender, KeyEventArgs e)
  66.         {
  67.             if (e.KeyCode == Keys.Up)
  68.             {
  69.                 tmrUp.Stop();
  70.                 isJumping = false;
  71.             }
  72.             else if (e.KeyCode == Keys.Right)
  73.             {
  74.                 tmrRight.Stop();
  75.             }
  76.             else if (e.KeyCode == Keys.Left)
  77.             {
  78.                 tmrLeft.Stop();
  79.             }
  80.         }
  81.  
  82.         private void Platformer_Load(object sender, EventArgs e)
  83.         {
  84.             Coin c1 = new Coin();
  85.             Coin c2 = new Coin();
  86.             c1.drawTo(this);
  87.             cList.Add(c1);
  88.             c1.setPos(100, 200);
  89.             c2.drawTo(this);
  90.             cList.Add(c2);
  91.             c2.setPos(200, 200);
  92.         }
  93.  
  94.         private void tmrGameLoop_Tick(object sender, EventArgs e)
  95.         {
  96.             foreach(Coin c in cList)
  97.             {
  98.                 if (pbPlayer.Bounds.IntersectsWith(c.getBounds()))
  99.                 {
  100.                     c.setPos(1001, 1001);
  101.                     score++;
  102.                     lblScore.Text = "Score: " + score;
  103.                 }
  104.             }
  105.         }
  106.     }
  107. }
  108.  
  109. ///////////////////////////////////////////////////////////////////
  110.  
  111. using System;
  112. using System.Collections.Generic;
  113. using System.Linq;
  114. using System.Text;
  115. using System.Threading.Tasks;
  116. using System.Windows.Forms;
  117. using System.Drawing;
  118.  
  119. namespace PlatoformerTut
  120. {
  121.     class Coin
  122.     {
  123.  
  124.         private PictureBox pbCoin = new PictureBox();
  125.  
  126.         public Coin()
  127.         {
  128.             pbCoin.Width = 10;
  129.             pbCoin.Height = 10;
  130.             pbCoin.BackColor = Color.Yellow;
  131.         }
  132.  
  133.         public void drawTo(Form f)
  134.         {
  135.             f.Controls.Add(pbCoin);
  136.         }
  137.  
  138.         public Rectangle getBounds()
  139.         {
  140.             return pbCoin.Bounds;
  141.         }
  142.  
  143.         public void setPos(int x, int y)
  144.         {
  145.             pbCoin.Location = new Point(x, y);
  146.         }
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement