This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2008-02-04 12:51:57

freddie-few
Scratcher
Registered: 2008-02-04
Posts: 6

Computer Player

On Scratch, how do you create the "Computer"? For example, if you had a racing game, and you needed the "Computer" to race aswell. But obviously as a player you shouldn't be able to play as the Computer.

Offline

 

#2 2008-02-04 13:08:34

Llamalover
Scratcher
Registered: 2007-05-18
Posts: 100+

Re: Computer Player

That's actually quite hard! Check this out : http://scratch.mit.edu/forums/viewtopic.php?pid=15790#p15790 for some help, but, if you want, I could make a quick demo to show you how


Be nice, I'm an old lady  wink

Offline

 

#3 2008-02-04 22:44:30

EdnaC
Scratcher
Registered: 2007-08-28
Posts: 100+

Re: Computer Player

As Llama points out, this can be tricky.  You have to program that part yourself, using rules for what the "computer" should do in different situations.  The easy approach is to have the enemy just point toward the "player" and move, but this can get old pretty quickly.

I have a jet fighter game: http://scratch.mit.edu/projects/EdnaC/33613 that lets you choose "one player" mode in which you are up against the computer (sometimes called the "AI", for Artificial Intelligence).  A good bit of work was required to get the AI to appear to be "intelligent".   There is some additional discussion (and a description of the rules that define the AI in "StealthFighter") in this thread:

http://scratch.mit.edu/forums/viewtopic.php?pid=14956#p14956

-MrEd

Offline

 

#4 2008-02-05 01:08:01

Mayhem
Scratcher
Registered: 2007-05-26
Posts: 1000+

Re: Computer Player

It depends alot on the game, obviously.  The AI for a "pong" opponent will be considerably different to that for an enemy fighter jet or a pac-man ghost.

You will need to specofy what you need your Computer Opponent to do.


Web-spinning Spider:  http://scratch.mit.edu/projects/Mayhem/18456
3D Dungeon Adventure:  http://scratch.mit.edu/projects/Mayhem/23570
Starfighter X: http://scratch.mit.edu/projects/Mayhem/21825
Wandering Knight: http://scratch.mit.edu/projects/Mayhem/28484

Offline

 

#5 2008-02-05 13:28:18

chalkmarrow
Scratcher
Registered: 2007-05-18
Posts: 100+

Re: Computer Player

A.I. is one of the toughest problems in software, and it varies quite a bit depending upon the project, as Mayhem suggests. Ednac's project is a good example. The best way to start is to turn off your computer, sit in a dimly lit room, and just concentrate on what you would expect a non-perfect player would do, then write it out on a blank sheet of paper in a messy fashion using words, arrows, etc. as high level concepts. THEN, when you've thought about it enough, convert your thoughts into computer code to implement the general idea. For example, in the case of pong, the general idea would be something like "move smoothly from current position to where the ball is going to intersect at some set speed that depends on the level, with a random mistake built into the y-coordinate, which also depends on the level."

In the case of a racing game that is top-down view, the general idea might be "make the car go around the track at an average speed that increases with level, such that it mistakenly hits the edges of the track from time to time, less often at higher levels." To implement this idea you could place a bunch of invisible (ghost = 100) sprites along the track that act as "waypoints" such that the car simply tries to connect the dots (more waypoints near the corners, just a couple on the straightaway.)  Each waypoint could invisibly move around its ideal (perfectly centered) position at a distance that decreases with level. If you want the computer car to occasionally bump into the player, you could make the waypoints ahead of the computer car move toward the player's car and adjust the speed slower or faster, depending.

Last edited by chalkmarrow (2008-02-05 13:29:22)

Offline

 

#6 2008-02-05 13:39:53

freddie-few
Scratcher
Registered: 2008-02-04
Posts: 6

Re: Computer Player

Thanks for your help!

I've been playing around a bit and have come to another small problem. I used the racing game idea, but each time I play - the computer car just does exactly the same. I know there must be some way to make it random the way it drives around the circuit, but I just haven't worked it out. Cheers again.

Offline

 

#7 2008-02-05 15:18:44

EdnaC
Scratcher
Registered: 2007-08-28
Posts: 100+

Re: Computer Player

Read thru the second paragraph of Chalkmarrow's suggestion.  The idea is to use "random" (it's in the "numbers" blocks) when placing the waypoints.  Instead of having the waypoints go to fixed positions, you modify the position by adding a random number.

In Waypoint1

When (green flag or setup broadcast)
Go to x: 125 + Random (-10 to 10) Y: 90 + Random (-10 to 10)
Hide

This would give you a hidden sprite somewhere between

X 115 to 135 , Y 80 to 100

The range that random picks a number from can be tailored, say if at that waypoint, going less than 125 could make the car hit a wall, you could use "Random (0 to 20)".

-MrEd

Offline

 

#8 2008-02-05 15:32:59

chalkmarrow
Scratcher
Registered: 2007-05-18
Posts: 100+

Re: Computer Player

I posted an example here: http://scratch.mit.edu/projects/chalkmarrow/93807
Feel free to use it if you think it might help.

Offline

 

#9 2008-02-10 08:59:51

freddie-few
Scratcher
Registered: 2008-02-04
Posts: 6

Re: Computer Player

Thanks for your help, I will try it out. Cheers again.

Offline

 

#10 2008-02-10 13:05:52

freddie-few
Scratcher
Registered: 2008-02-04
Posts: 6

Re: Computer Player

Sorry to bother you again, and I'm actually sure that I could possibly work this out myself, but I would just like it if you guys think it's a good idea and that it'd work. If any of you have ever played Mario Kart, where they have objects like "red shells", which you pick up in a box and fire at the racer in front of you. It would then hit that racer, or sprite, and would send him spinning around.

To do this, I was thinking that maybe if there are boxes (sprites) that you can drive through and pick up a "red shell", and then when you fire it, it would move like the A.I kart, hitting the waypoints, and then when it touches the A.I kart, it would spin it around.

Does that make sense? And would it work?

Offline

 

#11 2008-02-10 22:14:45

chalkmarrow
Scratcher
Registered: 2007-05-18
Posts: 100+

Re: Computer Player

Freddie-few: That would definitely work.  Good idea. You could keep the red shell hidden until the player touches the box, then you could make it go to the location of the player until he shoots, then you could make it act like the AI until it makes a full circuit. The trick is making sure that the shell knows which waypoint to go to first. I haven't thought about how to do that, but I'm sure you could figure it out...

Offline

 

#12 2008-02-11 12:30:20

freddie-few
Scratcher
Registered: 2008-02-04
Posts: 6

Re: Computer Player

I'll give it a try. Thanks.

Offline

 

#13 2008-02-11 14:01:26

freddie-few
Scratcher
Registered: 2008-02-04
Posts: 6

Re: Computer Player

Ok - I've been trying for that last hour or two on the exact same thing. I can create the sprite, hide it and then show it when the computer kart touches the box, but then it just stays in the same place. I'll keep trying but just wondering if anyone has any useful tips. It should be quite easy to make the controls for the player kart.

Much appreciated.

Offline

 

#14 2008-02-12 11:14:48

chalkmarrow
Scratcher
Registered: 2007-05-18
Posts: 100+

Re: Computer Player

Are  you telling the sprite to go to the location of the kart after the kart touches the box? Maybe you could post the project as a test and I could look at it. One way to do it is define a variable called "mode" for the shell (a variable only the shell can see), and just assume mode 1 corresponds to an invisible shell that doesn't move, mode 2 corresponds to the shell following the car, and mode 3 corresponds to the shell moving around the track as a projectile. You can then have one script that simply changes the mode variable whenever the right thing happens (it will always go mode 1, 2, 3, 1, 2, 3, . . .), and another script that tells the shell what to do based on the mode variable. Since the shell has to move around the track, it really is effectively an AI kart itself -- just one that looks like a shell and is often invisible.

Last edited by chalkmarrow (2008-02-12 11:16:06)

Offline

 

Board footer