Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Speed problem/Jerky movement

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
aperson



Joined: Sat May 01, 2010 9:48 am
Posts: 1

PostPosted: Sat May 01, 2010 12:31 pm    Post subject: Speed problem/Jerky movement Reply with quote

Hi everybody, I'm trying to complete HCA Assignment 2. But I have struck a problem, the images don't seem to load or sometimes they load after a couple of seconds but the movement is jerky.
Code:

using System;
using System.Drawing;
using System.Collections.Generic;
using SdlDotNet.Core;
using SdlDotNet.Graphics;
using SdlDotNet.Input;

namespace OddOneOut
{
   public class OddOneOut
   {
      Surface sfcGameWindow;
      const int WIDTH  = 1024;
      const int HEIGHT = 768;
      int level = 1;
      List<Surface> TileTypes = new List<Surface>();
      List<Tile> Tiles = new List<Tile>();
      Random Rand = new Random();
      List<int> Directions = new List<int>();
      int DirectionPos;
      
      public OddOneOut ()
      {
         sfcGameWindow = Video.SetVideoMode(WIDTH, HEIGHT);
         
         TileTypes.Add(new Surface("media/card_1.png"));
         TileTypes.Add(new Surface("media/card_2.png"));
         TileTypes.Add(new Surface("media/card_3.png"));
         TileTypes.Add(new Surface("media/card_4.png"));
         TileTypes.Add(new Surface("media/card_5.png"));
         TileTypes.Add(new Surface("media/card_6.png"));
         TileTypes.Add(new Surface("media/card_7.png"));
         TileTypes.Add(new Surface("media/card_8.png"));
         
         GenerateDirections();
         NewLevel();
      }
      
        public void Run() {
            Video.WindowCaption = "Odd One Out";
            Events.Tick += new EventHandler<TickEventArgs>(Events_Tick);
            Events.Quit += new EventHandler<QuitEventArgs>(Events_Quit);
            Events.Run();
        }
       
        void Events_Tick(object sender, TickEventArgs e) {
         Update(e.SecondsElapsed);
         Draw();
        }
       
      void Update(float elapsed){
         foreach(Tile tile in Tiles){
            tile.X += Convert.ToSingle(tile.xmove * elapsed);
            tile.Y += Convert.ToSingle(tile.ymove * elapsed);
         }   
      }
      
      void Draw(){
         sfcGameWindow.Fill(Color.Black);
         
         foreach(Tile tile in Tiles){
            sfcGameWindow.Blit(TileTypes[tile.Type], new Point(Convert.ToInt32(tile.X),
                                                     Convert.ToInt32(tile.Y)));
         }
      }
      
      void GenerateDirections(){
         for (int i = 0; i < 360; ++i) {
            if (i < 10) continue;
              if (i >= 350) continue;
              if (i > 80 && i < 100) continue;
              if (i > 170 && i < 190) continue;
              if (i > 260 && i < 280) continue;
         
              Directions.Add(i);
         }
      }
      
      int ChooseDirection(){
         ++DirectionPos;
         if (DirectionPos == Directions.Count) DirectionPos = 0;
         return Directions[DirectionPos];
      }
      
      void ShuffleList<T>(List<T> list){
         for (int i = 0; i < list.Count; i++){
            T tmp = list[i];
            list.RemoveAt(i);
            list.Insert(Rand.Next(0, list.Count), tmp);
         }
      }
      
      void NewLevel(){
         Tiles.Clear();
         GenerateTiles();
      }
      
      void GenerateTiles(){
         ShuffleList(TileTypes);
         for(int i = 0; i < TileTypes.Count; i++){
            int numberOfTiles;
            if(i == 0) numberOfTiles = 1;
            else numberOfTiles = Rand.Next(2,level+2);
            for(int y = 0; y < numberOfTiles; y++){
               Tile tile = new Tile();
               tile.X = Rand.Next(WIDTH);
               tile.Y = Rand.Next(HEIGHT);
               tile.Speed = Rand.Next(70, 120);
               tile.Direction = ChooseDirection();
               tile.xmove = (Math.Cos(tile.Direction * Math.PI / 180) * tile.Speed);
               tile.ymove = (Math.Sin(tile.Direction * Math.PI / 180) * tile.Speed);            
               tile.Type = i;
               Tiles.Add(tile);
            }
         }
      }
      
        void Events_Quit(object sender, QuitEventArgs e) {
            Events.QuitApplication();
        }
      
   }
}


Code:

using System;
using System.Drawing;
using SdlDotNet.Graphics;

namespace OddOneOut
{
   public class Tile
   {
      public float X, Y;
      public int Direction, Speed, Type;
      public double xmove, ymove;
   }
}



Any help is appreciated!
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast