3 Replies - 843 Views - Last Post: 22 July 2012 - 05:19 PM Rate Topic: -----

#1 paki-boy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-July 12

how to add sound in snake game in c#

Posted 22 July 2012 - 04:28 PM

i have created a snake game in c#.net but i don't know how to add sound and how to access it...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Random randfood = new Random();
        Graphics paper;
        snake snake = new snake();
        food food;

        bool up = false;
        bool down = false;
        bool right = false;
        bool left = false;
        int score = 0;

        public Form1()
        {
            InitializeComponent();
            food = new food(randfood);
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            paper = e.Graphics;
            food.drawfood(paper);
            snake.Drawsnake(paper);
            for (int i = 0; i < snake.SnakeRec.Length; i++)
            {
                if (snake.SnakeRec[i].IntersectsWith(food.foodRec))
                {
                    food.foodlocation(randfood);
                }
            
            }

        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyCode == Keys.Space)
            {
                timer1.Enabled = true;
                Spacebarlabel.Text = "";
                down = false;
                up = false;
                right = true;
                left = false;
            
            }
            if (e.KeyData == Keys.Down)
            {
                down = true;
                up = false;
                right = false;
                left = false;
            }
            if (e.KeyData == Keys.Up)
            {
                down = false;
                up = true;
                right = false;
                left = false;
            }
            if (e.KeyData == Keys.Right)
            {
                down = false;
                up = false;
                right = true;
                left = false;
            }
            if (e.KeyData == Keys.Left)
            {
                down = false;
                up = false;
                right = false;
                left = true;
            }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            scorelabel.Text = Convert.ToString(score);
            
            if (down)
            {
                snake.movedown();
            }
            if (up)
            {
                snake.moveup();
            }
            if (right)
            {
                snake.moveright();
            }
            if (left)
            {
                snake.moveleft();
            }
            for (int i = 0; i < snake.SnakeRec.Length; i++)
            {
                if (snake.SnakeRec[i].IntersectsWith(food.foodRec))
                {
                    score += 10;
                    snake.growsnake();
                    food.foodlocation(randfood);
                }

            }
            collision();
            this.Invalidate();
        }
        public void collision()
        {
            for (int i = 1; i < snake.SnakeRec.Length; i++)
            {
                if (snake.SnakeRec[0].IntersectsWith(snake.SnakeRec[i]))
                {
                    restart(); 
                
                }
            
            }

            if (snake.SnakeRec[0].X < 0 || snake.SnakeRec[0].X > 290)
            {
                restart();
            
            }
            if (snake.SnakeRec[0].Y < 0 || snake.SnakeRec[0].Y > 290)
            {

                restart();
            
            }
            
        }
        public void restart()
        {
            timer1.Enabled = false;
            MessageBox.Show("snake is dead.you scored "+score);
            snakescorelabel.Text = "0";
            score = 0;
            Spacebarlabel.Text = "press space bar to begin";
            snake = new snake();
            
        
        }

        private void soundlabel_Click(object sender, EventArgs e)
        {
            
        }
    }
}

this is my code

This post has been edited by Atli: 22 July 2012 - 04:39 PM
Reason for edit:: Added [code] ... [/code] tags.


Is This A Good Question/Topic? 0
  • +

Replies To: how to add sound in snake game in c#

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 4965
  • View blog
  • Posts: 10,563
  • Joined: 02-June 10

Re: how to add sound in snake game in c#

Posted 22 July 2012 - 04:50 PM

I just have to address how you are handling the keydown code:

Instead of that massive and repetitive if...if...if..if in lines 50-88 you only need 4 lines:

            down = e.KeyData == Keys.Down;
            up = e.KeyData == Keys.Up;
            right = e.KeyData == Keys.Right;
            left = e.KeyData == Keys.Left;


Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 2039
  • View blog
  • Posts: 6,072
  • Joined: 05-May 12

Re: how to add sound in snake game in c#

Posted 22 July 2012 - 04:54 PM

Here are are couple things directly from MSDN:
How to play a sound from WinForms: http://msdn.microsof...ibrary/4y171b18
How to play a beep from WinForms: http://msdn.microsof...y/h62wtc8c.aspx
Was This Post Helpful? 1
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 2039
  • View blog
  • Posts: 6,072
  • Joined: 05-May 12

Re: how to add sound in snake game in c#

Posted 22 July 2012 - 05:19 PM

And "Thank You! Thank You! Thank You!" for writing a Snake game that uses the arrow keys for controls. Most everybody else seems to assume that WASD is the appropriate controls. Not everybody uses keyboards where the WASD keys are arranged in an inverted T.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1