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.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace Eventhandler_spil
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- //Eventhandler bliver kaldt, når brugeren presser en knap på tastaturet ned
- private void btnSpiller1_KeyPress(object sender, KeyPressEventArgs e)
- {
- // Er tasten a trykket ned?
- if (e.KeyChar == 'a')
- {
- //Tag knappen nuværende position og træk 10 pixel fra i x retningen, så bilen kører mod venstre.
- btnSpiller1.Location = new Point(btnSpiller1.Location.X - 10, btnSpiller1.Location.Y);
- }
- // Er tasten d trykket ned?
- if (e.KeyChar == 'd')
- {
- //Tag knappen nuværende position og læg 10 pixel fra i x retningen, så bilen kører mod højre.
- btnSpiller1.Location = new Point(btnSpiller1.Location.X + 10, btnSpiller1.Location.Y);
- }
- // Er tasten w trykket ned?
- if (e.KeyChar == 'w')
- {
- //Tag knappen nuværende position og træk 10 pixel fra i y retningen, så bilen kører opad.
- btnSpiller1.Location = new Point(btnSpiller1.Location.X, btnSpiller1.Location.Y-10);
- }
- // Er tasten s trykket ned?
- if (e.KeyChar == 's')
- {
- //Tag knappen nuværende position og læg 10 pixel fra i y retningen, så bilen kører nead
- btnSpiller1.Location = new Point(btnSpiller1.Location.X, btnSpiller1.Location.Y+10);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement