Advertisement
AalborgHTX

Bilspil med brug af eventhandlere

Mar 16th, 2020
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.84 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 Eventhandler_spil
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.  
  20.         //Eventhandler bliver kaldt, når brugeren presser en knap på tastaturet ned
  21.         private void btnSpiller1_KeyPress(object sender, KeyPressEventArgs e)
  22.         {
  23.             // Er tasten a trykket ned?
  24.             if (e.KeyChar == 'a')
  25.             {
  26.                 //Tag knappen nuværende position og træk 10 pixel fra i x retningen, så bilen kører mod venstre.
  27.                 btnSpiller1.Location = new Point(btnSpiller1.Location.X - 10, btnSpiller1.Location.Y);
  28.             }
  29.  
  30.             // Er tasten d trykket ned?
  31.             if (e.KeyChar == 'd')
  32.             {
  33.                 //Tag knappen nuværende position og læg 10 pixel fra i x retningen, så bilen kører mod højre.
  34.                 btnSpiller1.Location = new Point(btnSpiller1.Location.X + 10, btnSpiller1.Location.Y);
  35.             }
  36.  
  37.             // Er tasten w trykket ned?
  38.             if (e.KeyChar == 'w')
  39.             {
  40.                 //Tag knappen nuværende position og træk 10 pixel fra i y retningen, så bilen kører opad.
  41.                 btnSpiller1.Location = new Point(btnSpiller1.Location.X, btnSpiller1.Location.Y-10);
  42.             }
  43.  
  44.             // Er tasten s trykket ned?
  45.             if (e.KeyChar == 's')
  46.             {
  47.                 //Tag knappen nuværende position og læg 10 pixel fra i y retningen, så bilen kører nead
  48.                 btnSpiller1.Location = new Point(btnSpiller1.Location.X, btnSpiller1.Location.Y+10);
  49.             }
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement