none
Can't get player sprite to move in maze game? RRS feed

  • Question

  • Hi there! Probably a bit of a simple question but I could really use some help!

    I'm trying to make a maze game, but no matter what I do, I can't get the player sprite to move - I've tried with arrow keys, separate If statements.. can't seem to find anything to work, as there's no errors, it just doesn't move! 

    I've pasted what I have so far below, please let me know if you spot anything that might be causing the issue, I'm very much a beginner!

    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 Maze_game
    {
        public partial class Mazegame : Form
        {
            readonly bool goleft;
            readonly bool goright;
            //int score = 0;
            //int points = 13;
            int playerSpeed = 500;
            //int health = 100;
            public Mazegame()
    
            {
                InitializeComponent();
            }
    
            private void MazeGame_Load(object sender, EventArgs e)
            {
    
            }
    
    
            private void timer1_Tick(object sender, EventArgs e)
            {
    
    
                if (goleft)
                {
                    ball.Left -= playerSpeed;
                }
    
                if (goright)
                {
                    ball.Left += playerSpeed;
                }
    
            }
            private void Keyisdown(object sender, KeyEventArgs e)
            {
    
                int x = ball.Location.X;
                int y = ball.Location.Y;
    
                if (e.KeyCode == Keys.W)
    
                    ball.Top = ball.Top - 5;
    
                else if (e.KeyCode == Keys.D)
    
                    ball.Left = ball.Left + 5;
    
                else if (e.KeyCode == Keys.A)
    
                    ball.Left = ball.Left - 5;
    
                else if (e.KeyCode == Keys.S)
    
                    ball.Top = ball.Top + 5;
    
                foreach (Control a in this.Controls) ;
            }
    
    
            private void Mazegame_Load(object sender, EventArgs e)
            {
    
            }
    
            private void KeyIsUp(object sender, KeyEventArgs e)
        {
    
            int x = ball.Location.X;
            int y = ball.Location.Y;
    
            if (e.KeyCode == Keys.W)
            
                ball.Top = ball.Top - 5;
            
    
            else if (e.KeyCode == Keys.D)
            
                ball.Left = ball.Left + 5;
            
            else if (e.KeyCode == Keys.A)
            
                ball.Left = ball.Left - 5;
            
            else if (e.KeyCode == Keys.S)
            
                ball.Top = ball.Top + 5;
            
                foreach (Control a in this.Controls) ;
        }
    
        }
    
    }
    

     
    Friday, December 13, 2019 9:21 PM

All replies

  • There's so much you haven't showed us.  What type of object is "ball"?  Are you actually using the timer?  At what interval?  The timer moves the ball 500 pixels at a time, which mean it's going to move quickly off of the screen.

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Friday, December 13, 2019 9:56 PM
  • Hi togepixels,

    Thank you for posting here.

    Here are some links for making a maze game, you can refer to it.

    Creating a Maze Game Using C# in .NET 4.5.

    A Simple C# Labyrinth and Maze.

    Tutorial 2: Create a Maze.

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    If you still want to make a maze game yourself, please provide us with more information as Tim said.

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 16, 2019 2:56 AM