Quantcast
Channel: Visual Studio General Questions forum
Viewing all articles
Browse latest Browse all 21115

¿How can I create a counter without timers?

$
0
0
  • Hi, I'm a student and for my final project on Programming II (I'm new on this) I am creating a game that is about a character who walks from right to left to avoid falling objects. 

    I have two windows (Menu and where you play) and two classes (Velociraptor and Meteorito) 

    The problem I have is that when I use the keyboard  the timers stop, so I was wondering if there is another way to solve this without timers. (for the objects (I use 2 timers) and for the elapsed time (I use 2 timers) and for the gif of the character walking (I use 1 timer) .... But for now the character moves only by the mouse position on the x axis. 
    My collisions do not work. 

    I don't know if anyone could help me or give me any advice to fix this, thank you very much!
  • Sorry I speak spanish that's why in the code are some words on spanish and english, if you have any doubt ask me!
//Class of the window where occurs the game

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;
using WMPLib;

namespace Survival
{
    public partial class fMain : Form
    {
        int score, lives, time;
        WMPLib.WindowsMediaPlayer wgun = new WMPLib.WindowsMediaPlayer();
        List<Meteorito> conjunto;
        int vidam = 0;
        private static readonly Random random = new Random();

        public fMain()
        {
            conjunto = new List<Meteorito>();
            InitializeComponent();

            gameTimer.Interval = 1000 / 4;
            gameTimer.Tick += new EventHandler(Update);
            gameTimer.Start();
            StartGame();
        }
        //Where the character is created
        Velociraptor personaje = new Velociraptor(350, 450);
            //METHOD FOR THE COLLISION(it don't works)
/*
        bool detectCollision(int px, int py)
        {


            
            int miaumeowRight = px + Velociraptor.MIAUMEOW_WIDTH;
            int miaumeowDown = py + Velociraptor.MIAUMEOW_HEIGHT;

            int meteoritoRight = px + Meteorito.METEORITO_WIDTH;
            int meteoritoDown = py + Meteorito.METEORITO_HEIGHT;
            foreach (Control ctrl in this.Controls)    // ???
            {
                if (px < meteoritoRight & miaumeowRight > ctrl.Location.X &
                    py < meteoritoDown & meteoritoDown > ctrl.Location.Y)
                    return true;
            }
             return false;
            }
            */
        private void Update(object sender, EventArgs e)
        {
            labelVidas.Text = "Vidas: " + lives.ToString();
            labelPuntos.Text = "Puntos: " + score.ToString();
        }

        private void StartGame()
        {
            score = 0;
            lives = 3;
            time = 1;

            tiempo.Interval = 1000;
            tiempo.Tick += new EventHandler(ActualizarTiempo);
        }

        private void ActualizarTiempo(object sender, EventArgs e)
        {
            if (time > 0)
                time += 1;
            int tiempoSegundos = time;
            labelTiempo.Text = "Tiempo: " + tiempoSegundos.ToString();
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }
        //se pinta el velociraptor, meteoritos
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            personaje.draw(g);
           
            foreach (Meteorito b in conjunto)
                b.draw(g);

            for (int i = 0; i < conjunto.Count; i++)
            {
                if (conjunto[i].colision(Cursor.Position.X - 425, 450))
                {
                    conjunto.RemoveAt(i);
                    break;
                }

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

            int ancho = this.Width;
            conjunto.Add(new Meteorito(random.Next(ancho), 0));
            
        }
//como se movera el meteorito
        private void cronometroMueve_Tick(object sender, EventArgs e)
        {
            vidam += 20;
            if (vidam == 1400)
            {
                int ancho = this.Width;
                conjunto.Add(new Meteorito(random.Next(ancho), 0));
                vidam = 0;
            }
            foreach (Meteorito b in conjunto)
            {
                b.fall();  //WHERE WE MOVE EACH METEOR
                this.Invalidate();
            }

        }

        private void buttonVidas_Click(object sender, EventArgs e)
        {
            if (lives > 0)
                lives--;
        }

        private void buttonPuntos_Click(object sender, EventArgs e)
        {
            score += 10;
        }

        private void labelTiempo_Click(object sender, EventArgs e)
        {

        }

        
  
    }
}
//CLASS WHERE THE OBJECTS FALL  ***with timer***
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace Survival
{
    class Meteorito
    {
        private int posX;
        private int posY;
        private Bitmap image;

        public const int METEORITO_WIDTH = 56; 

//Dimensions of the image 
        public const int METEORITO_HEIGHT = 119;

        public Meteorito()
        {
            Random r = new Random();
            posX = r.Next(0,700);
            posY = 0;
            String filePath = Application.StartupPath + @"\images\";
            filePath += @"meteorito" + ".gif";
            image = new Bitmap(filePath);
        }

         public Meteorito(int nx, int ny)
        {
            posX = nx;
            posY = ny;
            String filePath = Application.StartupPath + @"\images\";
            filePath += @"meteorito" + ".gif";
            image = new Bitmap(filePath);
         }
         public int PosX
         {
             get
             {
                 return posX;
             }
             set
             {
                 posX = value;
             }
         }

         public int PosY
         {
             get
             {
                 return posY;
             }
             set
             {
                 posY = value;
             }
         }

         public void fall()
         {
            posY+=4;
         }
         public void draw(Graphics g)
         {

             String filePath = Application.StartupPath + @"\images\";

             
             filePath += @"meteorito" + ".gif";
             image = new Bitmap(filePath);
             g.DrawImage(image, new Point(posX, posY));
         }
//this is how the meteors move
         public void mueve(int w, int h)
         {
             Random r = new Random();
             PosX += r.Next(-10, 10);
             for(int i= 0; i> 700; i+=3)

                 posY += i;

             if (posX < 0 || PosX > w)
                 PosX = r.Next(0, w);

             if (posY < 0 || PosY > h)
                 PosY = r.Next(0, h);
         }

/*I'm not shure if the public bool of the collisions goes(here) in the classes of the character and meteors or only on the class of the window where we play*/



         public bool colision(int mx, int my)
         {
             int miaumeowRight = mx + Velociraptor.MIAUMEOW_WIDTH;
             int miaumeowDown = my + Velociraptor.MIAUMEOW_HEIGHT;

             int meteoritoRight = mx + Meteorito.METEORITO_WIDTH;
             int meteoritoDown = my + Meteorito.METEORITO_HEIGHT;

             if ((miaumeowRight <= mx && mx <= (meteoritoRight))&& (miaumeowDown <= my && my <= (meteoritoDown)))
                 return true;
             else
                 return false;
         }
    }
}



Viewing all articles
Browse latest Browse all 21115

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>