Thread: Frogger

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    11

    Frogger

    I am having trouble making my frogger game work and needed some help:

    Code:
    private void timer1_Tick(object sender, EventArgs e)
            {
                
    
                for (int i = 1; i <= 6; i++)
                {
    
                    // move falling obejct [i]
                    LeftCarX[i] = LeftCarX[i] + LeftCarSpeed[i];
    
                    // check if falling object [i] has been hit 
                    if ((LeftCarY[i] + LeftCarSize) > (ClientSize.Width - FroggerFrog.Width))
                    {
                        if (LeftCarX[i] - 20 < x)     //-20/+20 added to make sure falling
                        {                           //ojbect bigger than dribbler 
                            if ((LeftCarX[i] + LeftCarSize + 20) > (x + FroggerFrog.Width))
                            {
                                // object [i] has been hit
                                // Increase score - move back to top
                                Console.Beep();
                                //txtScore.Text = Convert.ToString(Convert.ToInt32(txtScore.Text) + 1);
                                LeftCarY[i] = -LeftCarSize;
                                LeftCarSpeed[i] = myrandom.Next(4) + 3;
                            }
                        }
                    }
                    // check for moving off bottom
                    if ((LeftCarY[i] + LeftCarSize) > ClientSize.Height)
                    {
                        // object [i] reaches bottom without being hit
                        // Move back to top with new speed
                        LeftCarY[i] = -LeftCarSize;
                        LeftCarSpeed[i] = myrandom.Next(4) + 3;
                    }
    
                    // redraw falling object [i] and dribbler at new location
                    myGraphics.DrawImage(FroggerFrog, x, ClientSize.Height - FroggerFrog.Width, FroggerFrog.Width, FroggerFrog.Height);
                    myGraphics.DrawImage(LeftCars[i], LeftCarX[i], LeftCarY[i], LeftCarSize, LeftCarSize);
                } 
            }
    
            private void BtnStart_Click_1(object sender, EventArgs e)
            {
                NewGame();
                timer1.Enabled = true;
                timer2.Enabled = true;
                timer3.Enabled = true;
                timer4.Enabled = true;
                timer5.Enabled = true;
                timer6.Enabled = true;
    
                // set each ball off side of panel and give new speed
                for (int i = 0; i < 6; i++)
                {
                    LeftCarX[i] = -LeftCarSize;
                    LeftCarSpeed[i] = myrandom.Next(4) + 3;
                }
    
                // Set man near center
                x = (int)(ClientSize.Width / 2);
    
                
                // Give form focus so it can accept KeyDown events
                this.Focus();
    
    
            }
            
            
    
    
        }
    
    }
    I can get the frog to appear but the cars won't and i keep getting errors any help would be appreciated.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You should be doing all of your drawing in the form's Paint event.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    11
    Whenever i put the cars under draw stuff:
    g.DrawImage(LeftCars, LeftCarX, LeftCarY);
    it's always red for some reason but not for the frog I don't know why either???

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    11
    Code:
    private void FrmFrogger_Paint(object sender, PaintEventArgs e)
            {
                Graphics g = e.Graphics;
    
                DrawStuff(g);
            }
    
    
            private void DrawStuff(Graphics g)
            {
                g.DrawImage(background, 0, 0);
                
                Font font = new Font("Arial", 9, FontStyle.Bold);
                SolidBrush yellowBrush = new SolidBrush(Color.Yellow);
    
                Font GameOverfont = new Font("Arial", 40, FontStyle.Bold);
                SolidBrush blueBrush = new SolidBrush(Color.Blue);
    
               
    
                g.DrawString("Move Up and down with the 8 and 2 Keys and left or Right with 4 and 6", font, yellowBrush, 200, 3);
                g.DrawString("Current Score -> " + score.ToString(), font, yellowBrush, 5, 5);
                g.DrawString("Time Left -> " + lives.ToString(), font, yellowBrush, 9, 20);
    
              
                g.DrawImage(FroggerFrog, x, y);
                g.DrawImage(LeftCars, LeftCarX, LeftCarY);
                g.DrawImage(Safety, SafetyX, SafetyY);
                g.DrawImage(Safety2, Safety2X, Safety2Y);
                g.DrawImage(Safety3, Safety3X, Safety3Y);
                g.DrawImage(Safety4, Safety4X, Safety4Y);
                g.DrawImage(Safety5, Safety5X, Safety5Y);
    
    
                if (countdown <= 0)
                {
                    g.DrawString("GAME OVER !!!", GameOverfont, blueBrush, ClientSize.Width / 2 - 200, ClientSize.Height / 2);
                    g.DrawString("Press the Start Button to Play Again", font, yellowBrush, ClientSize.Width / 2 - 100, ClientSize.Height / 2 + 60);
                    // StopGame();
                }
    
            }

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    11
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D; //Added for double buffering
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Media;         //needed for SoundPlayer Class
    
    namespace Frogger
    {
        public partial class FrmFrogger : Form
        {
            public FrmFrogger()
            {
                InitializeComponent();
                this.DoubleBuffered = true;
            }
    
            Graphics myGraphics;
            Random myrandom = new Random();
    
    
            // Sound file strings
            SoundPlayer alert = new SoundPlayer();
            SoundPlayer dead = new SoundPlayer();
            SoundPlayer coin = new SoundPlayer();
            SoundPlayer froggerhop = new SoundPlayer();
            SoundPlayer squash = new SoundPlayer();
            SoundPlayer backgroundmusic = new SoundPlayer();
    
            int[] LeftCarX = new int[7];
            int[] LeftCarY = new int[7];
    
            int[] LeftCarSpeed = new int[7];
    
            // Image arrays of cars
           Image[] LeftCars = new Image[7];
    
    
            int[] RightCarX = new int[5];
            int[] RightCarY = new int[5];
    
            int[] RightCarSpeed = new int[5];
            Image[] RightCars = new Image[5];
    
            int[] LogX = new int[11];
            int[] LogY = new int[11];
    
            int[] LogSpeed = new int[11];
            Image[] Logs = new Image[11];
    
            // Image arrays of logs
            bool flag = false;
    
    
            Image background;
            Image FroggerFrog;
            Image Safety;
            Image Safety2;
            Image Safety3;
            Image Safety4;
            Image Safety5;
            
    
            int x, y;//starting position of frog
    
            //Keep track of how you are doing
            int score;
            int lives;
            int countdown;
    
           
    
            int SafetyX;
            int SafetyY;
    
            int Safety2X;
            int Safety2Y;
    
    
            int Safety3X;
            int Safety3Y;
    
            int Safety4X;
            int Safety4Y;
    
            int Safety5X;
            int Safety5Y;
    
    
            //Size of moving Objects
            int LeftCarSize;
            int RightCarSize;
            int LogSize;
            int SafetySize;
            int Safety2Size;
            int Safety3Size;
            int Safety4Size;
            int Safety5Size;
    
            private void FrmFrogger_Paint(object sender, PaintEventArgs e)
            {
                Graphics g = e.Graphics;
    
                DrawStuff(g);
            }
    
    
            private void DrawStuff(Graphics g)
            {
                g.DrawImage(background, 0, 0);
                
                Font font = new Font("Arial", 9, FontStyle.Bold);
                SolidBrush yellowBrush = new SolidBrush(Color.Yellow);
    
                Font GameOverfont = new Font("Arial", 40, FontStyle.Bold);
                SolidBrush blueBrush = new SolidBrush(Color.Blue);
    
               
    
                g.DrawString("Move Up and down with the 8 and 2 Keys and left or Right with 4 and 6", font, yellowBrush, 200, 3);
                g.DrawString("Current Score -> " + score.ToString(), font, yellowBrush, 5, 5);
                g.DrawString("Time Left -> " + lives.ToString(), font, yellowBrush, 9, 20);
    
              
                g.DrawImage(FroggerFrog, x, y);
                g.DrawImage(LeftCars, LeftCarX, LeftCarY);
                g.DrawImage(Safety, SafetyX, SafetyY);
                g.DrawImage(Safety2, Safety2X, Safety2Y);
                g.DrawImage(Safety3, Safety3X, Safety3Y);
                g.DrawImage(Safety4, Safety4X, Safety4Y);
                g.DrawImage(Safety5, Safety5X, Safety5Y);
    
    
                if (countdown <= 0)
                {
                    g.DrawString("GAME OVER !!!", GameOverfont, blueBrush, ClientSize.Width / 2 - 200, ClientSize.Height / 2);
                    g.DrawString("Press the Start Button to Play Again", font, yellowBrush, ClientSize.Width / 2 - 100, ClientSize.Height / 2 + 60);
                    // StopGame();
                }
    
            }
    
            private void FrmFrogger_Load(object sender, EventArgs e)
            {
                background = Image.FromFile(Application.StartupPath + @"\frogger2.jpg");
    
                FroggerFrog = Image.FromFile(Application.StartupPath + @"\froggerfrog.gif");
    
                for (int i = 1; i <= 6; i++)
                {
                    LeftCars[i] = Image.FromFile(Application.StartupPath + @"\LeftCar" + i.ToString() + ".gif");
                }
    
                for (int x = 1; x <= 4; x++)
                {
                    RightCars[x] = Image.FromFile(Application.StartupPath + @"\RightCar" + x.ToString() + ".gif");
                }
    
    
                for (int l = 1; l <= 10; l++)
                {
                    Logs[l] = Image.FromFile(Application.StartupPath + @"\Log" + l.ToString() + ".gif");
                }
    
    
    
                // Have the balls spread across the panel with 20 pixels borders
                LeftCarSize = (int)((ClientSize.Width - 6 * 20) / 4);
    
                RightCarSize = (int)((ClientSize.Width - 6 * 20) / 4);
                LogSize = (int)((ClientSize.Width - 6 * 20) / 4);
    
                int c = 10;
    
                for (int i = 1; i <= 6; i++)
                {
                    LeftCarY[i] = y;
                    c = c + LeftCarSize + 20;
                }
    
                for (int x = 1; x <= 4; x++)
                {
                    RightCarY[x] = y;
                    c = c + RightCarSize + 20;
                }
    
    
                for (int l = 1; l <= 10; l++)
                {
                    LogY[x] = y;
                    c = c + LogSize + 20;
                }
    
                // Give form focus
                this.Focus();
    
                Safety = Image.FromFile(Application.StartupPath + @"\Safety.gif");
                Safety2 = Image.FromFile(Application.StartupPath + @"\Safety2.gif");
                Safety3 = Image.FromFile(Application.StartupPath + @"\Safety3.gif");
                Safety4 = Image.FromFile(Application.StartupPath + @"\Safety4.gif");
                Safety5 = Image.FromFile(Application.StartupPath + @"\Safety5.gif");
    
    
                score = 0;
                lives = 3;
    
                SafetyX = 25;
                SafetyY = 30;
    
                Safety2X = 155;
                Safety2Y = 30;
    
                Safety3X = 285;
                Safety3Y = 30;
    
                Safety4X = 410;
                Safety4Y = 30;
    
                Safety5X = 540;
                Safety5Y = 30;
    
    
                x = ClientSize.Width / 3;
                y = ClientSize.Height - FroggerFrog.Height;
    
    
                alert.SoundLocation = Application.StartupPath + @"\alert.wav";
                backgroundmusic.SoundLocation = Application.StartupPath + @"\backgroundmusic.mp3";
                coin.SoundLocation = Application.StartupPath + @"\coin.wav";
                dead.SoundLocation = Application.StartupPath + @"\dead.wav";
                froggerhop.SoundLocation = Application.StartupPath + @"\froggerhop.wav";
                squash.SoundLocation = Application.StartupPath + @"\squash.wav";
    
                alert.LoadAsync();
    
                backgroundmusic.LoadAsync();
    
                coin.LoadAsync();
    
                dead.LoadAsync();
    
                froggerhop.LoadAsync();
    
                squash.LoadAsync();
    
                this.Focus();
            }
    
           
    
    
            private void NewGame()
            {
                score = 0;
                countdown = 60;
                lives = 3;
                
                //backgroundmusic.Play();   background stops playing when shooting bullet
                //axWindowsMediaPlayer1.URL = Application.StartupPath + @"\opening.wav";
    
                
            
            }
    
            private void StopGame()
            {
                timer1.Enabled = false;
                timer2.Enabled = false;
                timer3.Enabled = false;
                timer4.Enabled = false;
                timer5.Enabled = false;
                timer6.Enabled = false;
    
                //axWindowsMediaPlayer1.close();
            }
    
            private void FrmFrogger_KeyDown(object sender, KeyEventArgs e)
            {
                //Make sure to turn Num Lock on
    
                if (e.KeyCode == Keys.NumPad6)
                {
                    x += 5;
                }
                else if (e.KeyCode == Keys.NumPad4)
                {
                    x -= 5;
                }
                else if (e.KeyCode == Keys.NumPad8)
                {
                    y -= 5;
                }
    
                else if (e.KeyCode == Keys.NumPad2)
                {
                    y += 5;
                }
    
                Invalidate();
                
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                
    
                for (int i = 1; i <= 6; i++)
                {
    
                    // move falling obejct [i]
                    LeftCarX[i] = LeftCarX[i] + LeftCarSpeed[i];
    
                    // check if falling object [i] has been hit 
                    if ((LeftCarY[i] + LeftCarSize) > (ClientSize.Width - FroggerFrog.Width))
                    {
                        if (LeftCarX[i] - 20 < x)     //-20/+20 added to make sure falling
                        {                           //ojbect bigger than dribbler 
                            if ((LeftCarX[i] + LeftCarSize + 20) > (x + FroggerFrog.Width))
                            {
                                // object [i] has been hit
                                // Increase score - move back to top
                                Console.Beep();
                                //txtScore.Text = Convert.ToString(Convert.ToInt32(txtScore.Text) + 1);
                                LeftCarY[i] = -LeftCarSize;
                                LeftCarSpeed[i] = myrandom.Next(4) + 3;
                            }
                        }
                    }
                    // check for moving off bottom
                    if ((LeftCarY[i] + LeftCarSize) > ClientSize.Height)
                    {
                        // object [i] reaches bottom without being hit
                        // Move back to top with new speed
                        LeftCarY[i] = -LeftCarSize;
                        LeftCarSpeed[i] = myrandom.Next(4) + 3;
                       
                    }
                   
                } 
            }
    
            private void BtnStart_Click_1(object sender, EventArgs e)
            {
                NewGame();
                timer1.Enabled = true;
                timer2.Enabled = true;
                timer3.Enabled = true;
                timer4.Enabled = true;
                timer5.Enabled = true;
                timer6.Enabled = true;
    
                // set each ball off side of panel and give new speed
                for (int i = 0; i < 6; i++)
                {
                    LeftCarX[i] = -LeftCarSize;
                    LeftCarSpeed[i] = myrandom.Next(4) + 3;
                }
    
                // Set man near center
                x = (int)(ClientSize.Width / 2);
    
                
                // Give form focus so it can accept KeyDown events
                this.Focus();
    
    
            }
            
            
    
    
        }
    
    }
    Heres the whole program maybe it will help.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Where are your timers defined? Could you use timer names that are more descriptive? Usually Invalidate() is called inside a timer unless you really only want the form redrawn when the user presses a key.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    11
    Well I'm really just trying to get the cars to appear first so I haven't really gone into to much detail however each timer is for the different car directions and then the logs if I can get the cars to appear but they won't any idea why???

  8. #8
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Why do some FOR loops start at 1 and others start at 0? Does this mean that the left car at 0 in the array does not get an image loaded for it?

    BTW Have you looked at XNA? MS's game framework that handles many common tasks in game programming. Is free and uses C#.

    Quote Originally Posted by itsme86 View Post
    You should be doing all of your drawing in the form's Paint event.
    Ummm...No. Your PAINT should be as fast as possible, doing the minimum amount of drawing you can manage. A slow PAINT will cause shearing and flicker.

    You should draw in response to time (and user events) then call for a PAINT once all the drawing has been done. The PAINT code should be one BitBlt() from a back buffer (or memory DC) to the screen DC.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I never have a problem with shearing or flicker. Probably because I simply set Form.DoubleBuffered to true and let it handle that for me.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed