• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
antipathy

single pressed key to do whole sprite verse

2 posts in this topic

Hello guys, sorry for a noob question, but I've been googling anwser for hours with no result. Like in title- how to make single pressed key to do such opperation? "IsKeydown" is procesing sprite as long as I hold down the key, and stops when I release the key, and second of my ideas"

 if (NewKeyState.IsKeyDown(Keys.G) && OldKeyState.IsKeyUp(Keys.G)) 
            {

              //  BroPunch();
         
            }
OldKeyState = NewKeyState;

does all the sprite verse in (what I think it is) 1 frame of update, which is obviously to fast.

I'd be thankful for help. Regards

 

0

Share this post


Link to post
Share on other sites

Consider something, in order for your game to render fast it needs to call Draw() and Update() as often as possible (update is not required to but that's going off topic). This means if you have something that requires a certain amount of time to execute after it is triggered, the action will have to last for longer than 1 draw call. Here is an example of such a technique used in XNA 4.0:

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace AnimationExample
{
    public class Game1 : Game
    {
        class Bullet
        {
            public Vector2 Position, Velocity;
            public bool IsDead;
            public Bullet(Vector2 position, Vector2 velocity)
            {
                this.Position = position;
                this.Velocity = velocity;
            }
        }

        List<Bullet> Bullets = new List<Bullet>();
        Random Random1 = new Random();
        GraphicsDeviceManager Graphics;
        SpriteBatch Batch1;
        KeyboardState OldState, NewState;
        Texture2D ProjectileTexture;
        float HalfTextureH, QuaterTextureH;

        public Game1()
        {
            Graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void LoadContent()
        {
            Batch1 = new SpriteBatch(GraphicsDevice);
            ProjectileTexture = Content.Load<Texture2D>("tex1");
            HalfTextureH = ProjectileTexture.Height / 2.0f;
            QuaterTextureH = ProjectileTexture.Height / 4.0f;
        }

        protected override void Update(GameTime gameTime)
        {
            OldState = NewState;
            NewState = Keyboard.GetState();

            // detect key press.
            if (NewState.IsKeyDown(Keys.Space) && OldState.IsKeyUp(Keys.Space))
                ShootBullet();

            // update any existing projectiles.
            Bullets.ForEach(UpdateBullet);

            // remove any projectiels that are dead.
            Bullets.RemoveAll(t => t.IsDead);

            Window.Title = Bullets.Count + " projectile(s) are alive.";
        }

        void ShootBullet(float minSpeed = 2f, float maxSpeed = 10f)
        {
            // get a random y starting coordinate.
            float yPos = ((GraphicsDevice.Viewport.Height - HalfTextureH)
                       * (float)Random1.NextDouble()) - QuaterTextureH;

            // calculate speed.
            float xVel = MathHelper.Clamp(maxSpeed * (float)Random1.NextDouble(), 
                         minSpeed, maxSpeed);

            // add the projectile.
            Bullets.Add(new Bullet(Vector2.UnitY * yPos, Vector2.UnitX * xVel));    
        }

        void UpdateBullet(Bullet bullet)
        {
            bullet.Position += bullet.Velocity;

            // check if the bullet has gone off the screen.
            if (bullet.Position.X > 800) bullet.IsDead = true;
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            Batch1.Begin();
            Bullets.ForEach(DrawBullet);
            Batch1.End();

            base.Draw(gameTime);
        }

        void DrawBullet(Bullet bullet)
        {
            Batch1.Draw(ProjectileTexture, bullet.Position, Color.White);
        }
    }
}

A screenshot for good measure :)

image.jpg

 

 

Here is the texture I used if you wish to try the example out:

 

tex1.png

Note, it is important to understand that if you wish something to happen over a duration of time longer than a single update/draw call, then you will always have to keep track of the state of that action, this is called state management. Each important state of something usually requires a logic update each time Update() is called, this is a core principle of game development.

 

Side Notes

 

The example will likely run slow if you were to run it on the Xbox 360 or Windows Phone because it is unoptimized and creates a lot of garbage. This could easily be rectified by using a fixed array as a resource pool where we only ever create a number of Bullet instances once, each bullet would need an extra bool to say if it was being used or not, however this is out of scope so if you come across that problem later on in development, feel free to PM me.

 

Aimee
 

Edited by AmzBee
1

Share this post


Link to post
Share on other sites

I'm extremly thankful AmzBee , you put a lot of effort into this post. I will dig in to your code in a moment :)

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0