Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Making a sprite move randomly accross screen  (Read 5020 times)

0 Members and 1 Guest are viewing this topic.

gl0w

  • Newbie
  • *
  • Posts: 20
    • View Profile
Making a sprite move randomly accross screen
« on: June 21, 2011, 01:56:38 am »
Hello,

I need to make a sprite (ghosts) in pacman game move across the screen randomly. How can i do it ?

Thanks in advance :)

thePyro_13

  • Full Member
  • ***
  • Posts: 156
    • View Profile
Making a sprite move randomly accross screen
« Reply #1 on: June 21, 2011, 06:10:34 am »
Looks up the rand function, either in the standard library or in SFML.
You can get a random number within a range(ie 1 - 4) using this.

Then call each number a direction. 1 == North, 2 == East, so on. Use it to decide what direction to go.

David

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
Making a sprite move randomly accross screen
« Reply #2 on: June 21, 2011, 04:25:02 pm »
But ghosts in pac-man don't move randomly :?

JAssange

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Making a sprite move randomly accross screen
« Reply #3 on: June 21, 2011, 06:51:43 pm »
Yeah the pacman ghosts have a set pattern, from what I remember, its:
Red chases player directly
Orange camps the bottom left corner
Pink heads to the square a few in front of pacman in his current direction.
Blue.... eh I think he follows red but with an offset but I forget.

gl0w

  • Newbie
  • *
  • Posts: 20
    • View Profile
Making a sprite move randomly accross screen
« Reply #4 on: June 21, 2011, 08:14:47 pm »
well guys, your right. But i'm making a game a little different from pacman, but inspired on pacman hah :) Well, now that you talk about it, how can i make the ghosts, go after pacman following the correct path in the game.
I dont have even a game with paths, i need to create the path's first.

I just dont know how to make the path's... yet :x


thanks all :)

JAssange

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Making a sprite move randomly accross screen
« Reply #5 on: June 22, 2011, 11:51:34 am »
You'll want to use a path-finding algorithm like A* so you'll need to break your world up into tiles.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Making a sprite move randomly accross screen
« Reply #6 on: June 22, 2011, 01:13:00 pm »
By the way, don't implement such algorithms on your own. Reuse existing libraries such as Boost.Graph ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

KasHKoW

  • Newbie
  • *
  • Posts: 41
    • View Profile
Making a sprite move randomly accross screen
« Reply #7 on: June 26, 2011, 03:05:11 am »
Quote
By the way, don't implement such algorithms on your own. Reuse existing libraries such as Boost.Graph


Why shouldn't you? I know very well that rewriting code that's already out there is bad, but wouldn't you learn a great deal from this?

Reading this, I actually was inspired to do my own pac-man like game. As I thought designing the enemies would really improve my programming skills(or problem solving).

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Making a sprite move randomly accross screen
« Reply #8 on: June 26, 2011, 04:34:57 am »
Quote from: "KasHKoW"
I know very well that rewriting code that's already out there is bad, but wouldn't you learn a great deal from this?
You would certainly learn something, the question is do you even want that and is it worth a much longer development time? Or do you just want to achieve a result as efficiently as possible?

You also have to consider that an algorithm isn't ready-to-use once it is finished. A lot of testing, bug fixes and optimizations are necessary to get a robust functionality, which requires very much time as well.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

section_two

  • Newbie
  • *
  • Posts: 19
    • View Profile
Making a sprite move randomly accross screen
« Reply #9 on: June 26, 2011, 05:53:24 am »
Quote from: "JAssange"
Yeah the pacman ghosts have a set pattern, from what I remember, its:
Red chases player directly
Orange camps the bottom left corner
Pink heads to the square a few in front of pacman in his current direction.
Blue.... eh I think he follows red but with an offset but I forget.


WOW!!!  I never realized this, and I played pacman "unsigned long" times.

 

anything