PacMan 2D

Blitz3D Forums/Blitz3D Beginners Area/PacMan 2D

David819(Posted 2005) [#1]
Hi, I decided i'll learn 2d so that i'll be better at programing in the end. i've started a PacMan game and am just having a small problem, i have the game so you see everything from the top, and i'm trying to get the character to rotate at 90 degrees to a certain direction but i can get it to work does anyone know how to do this with the rotateimage command so that when i press up pacman will turn to face up and so on. thanks.


Nice_But_Dim(Posted 2005) [#2]
Hi Goober
hmm use AnimImage() create your pac with 4 directions
up,down,left,right. graphics wise that is.
Flip your image might work,instead of rotate not sure. Blitz Basic 2D guys in the forum might be better.

Hope it helps probebly not. HeHe
regards


David819(Posted 2005) [#3]
Thanks that should do the trick.
thanks again.


RiverRatt(Posted 2005) [#4]
Creating 4 images is a good way to do it so you have 4 frames of an animation, but you can do it in code using a precalculated array. Oh and 4 frames would not look that great, it would be better to have around 16 frames.
I find it easier to do and array than hand drawing 16 of the same character. You can find a great tutorial on this at blitzcoder in the tutorials section under precalculated arrays.


David819(Posted 2005) [#5]
Thanks


Ross C(Posted 2005) [#6]
Yeah, don't use rotateimage() command in real time during the game as it's pretty slow.


David819(Posted 2005) [#7]
Ok, I've got the image to work properly and got the soring working but dont now how to add the maze + collision and place the food in the appropriate places without to much coding. anny idea how to go about this? thanks.


n8r2k(Posted 2005) [#8]
Post your code and I can help you out more.


David819(Posted 2005) [#9]
Ok Here is my code:

Graphics 640,480,0,2
SetBuffer BackBuffer()
AppTitle "PacMan V0.0 "

Global SUR, Food, tempscore
SX#=0
SY#=0

Load_Surround()
Create_Player(90,90,3,0)
Load_HUD()

For t=1 To 35
Create_food(20+(t*16),90)
Next

While Not KeyHit(1)
Cls

Update_Food()
Draw_Surround()
Update_Player()
Draw_Player()
Draw_Hud()
Color 255,0,0
Flip
Wend
End

Type Player
Field Img
Field HUD
Field X#,Y#
Field Lives%
Field Score%
Field Frame%
End Type

;Function to create player
Function Create_Player(X#,Y#,Lives%=3,Score%=0)
P.Player = New Player
P\Img=LoadAnimImage("PacMan.Png",32,32,0,4)
MidHandle P\Img
P\X#=X#
P\Y#=Y#
P\Lives%=Lives%
P\Score%=Score%
P\Frame%=0
End Function

;Function to update player
Function Update_Player()
For P.Player = Each Player

If P\X#<=40 Then P\X#=40
If P\Y#<=90 Then P\Y#=90
If P\X#=>600 Then P\X#=600
If P\Y#=>440 Then P\Y#=440

Next
End Function

;Function to draw player
Function Draw_Player()
For P.Player = Each Player

If KeyHit(208) Then
P\Frame%=2 ;Up
EndIf

If KeyHit(200) Then
P\Frame%=3 ;Down
Sx#=0
Sy#=Sy#+1
EndIf

If KeyHit(203) Then
P\Frame%=1 ;Left
Sx#=Sx#-1
Sy#=0
EndIf

If KeyHit(205) Then
P\Frame%=0 ;Right
Sx#=Sx#+1
Sy#=0
EndIf

If P\Frame%=2 Then Sy#=Sy#+1:Sx#=0
If P\Frame%=3 Then Sy#=Sy#-1:Sx#=0
If P\Frame%=1 Then Sy#=0:Sx#=Sx#-1
If P\Frame%=0 Then Sy#=0:Sx#=Sx#+1

P\X#=P\X#+SX#
P\Y#=P\Y#+SY#

DrawImage P\Img,P\X#,P\Y#,P\Frame%
Next
End Function

;Function to load HUD
Function Load_HUD()
For P.Player = Each Player
P\Hud=LoadImage("HUD.png")
Next
End Function

;Function to draw HUD
Function Draw_Hud()
For P.Player=Each Player
DrawImage P\HUD,0,0
Color 1,1,1
Text 55,18,P\Lives%
Text 90,18,P\Score%
Next
End Function

;Functions for surrounding border
Function Load_Surround()
SUR=LoadImage("Dec.png")
End Function

Function Draw_Surround()
DrawImage SUR,0,50
End Function

;food
Type Food
Field Img
Field X#,Y#
End Type

Function Create_Food(X#,Y#)
F.Food = New Food
F\Img = LoadImage("Food.png")
F\X# = X#
F\Y# = Y#
End Function

Function Update_Food()
For F.Food = Each Food
	For P.Player = Each Player
DrawImage F\Img,F\X#,F\Y#

If RectsOverlap(P\X#-15,P\Y#-15,32,32,F\X#,F\Y#,7,7)
tempscore=tempscore+10
P\Score%=P\Score%+10
Delete F.Food
EndIf

If Tempscore=100 Then
P\Lives%=P\Lives%+1
tempscore=0
EndIf

	Next
Next
End Function



n8r2k(Posted 2005) [#10]
I will have to look at this code and see what I can do. You could try ImagesCollide() for collision. The only problem would be that you would have to do something like this:
If ImagesCollide(PacManImg,PacManX,PacManY,PacManFrame,MazeImg,MazeX,MazeY,0)
	PacManMoving = 0
Else 
	PacManMoving = 1
EndIf 


Then you wold have to make the guy move only when the pacmanmoving variable is true. You would be better off searching for more help while I help you because I'm not the greatest programmer. I will probably confuse myself.


David819(Posted 2005) [#11]
ok I'll keep that in mind, but at the moment what is mainly concerning me is how to place the maze around and such, like use of tiles and so on. thanks


n8r2k(Posted 2005) [#12]
You mean the little dots? I will try to think of a way to do that.


David819(Posted 2005) [#13]
Thanks.


n8r2k(Posted 2005) [#14]
Probably the easiest way to do this would be to create an array of dots, then get rid of the dots you don't need. This way you can change the layout of the maze for different levals. Of course there still may be a better way. How do you post code like that anyway goober?


WolRon(Posted 2005) [#15]
What are the forum codes?


n8r2k(Posted 2005) [#16]

TY WolRon




David819(Posted 2005) [#17]
hey n8r2k, How you getting on with the code?


n8r2k(Posted 2005) [#18]
I still haven't found a better way than to do the array and delete uneeded dots. Sorry I can't help you more yet.


Rook Zimbabwe(Posted 2005) [#19]
Thats the only way I did it with am othello type game... changing the colors of the coins and checking to see if there was a coin etc.
-RZ


David819(Posted 2005) [#20]
ok, thanks anyhow.


n8r2k(Posted 2005) [#21]
If you send me an email at n8r2k@... with a zip file containing source code, pictures and all data files I might have an idea. It's kind of hard to explain in words but I will try if it works.


n8r2k(Posted 2005) [#22]
Are you going to send me your source code and other files?


David819(Posted 2005) [#23]
Hi, sorry i haven't replied for a while not been online much, yea , i will send you the files.


n8r2k(Posted 2005) [#24]
k


n8r2k(Posted 2005) [#25]
I got your e-mail. I will help you as soon as I can. What I am thinking is creating a seperate data file for each leval that tells what maze to use and which dots to kill. But I am not sure that will work. Who knows?


David819(Posted 2005) [#26]
Ok thanks, hope to hear from you soon.


n8r2k(Posted 2005) [#27]
I had a really great idea last night. I will try it out and post it as soon as i can


n8r2k(Posted 2005) [#28]
Please Read the Entire Message, don't skim through. I am sending you the folder with the updated version by me. Basically what i did was make a maze and put the places i wanted dots to be with a color of 0,0,0 and everything else at 4,4,4 so blitz could recognize a collision. Then i told it that if there was a collision beteween the dot and the maze, it skipped drawing that dot. I just now realized that since you are using types, i could just have made it delete the dot. But of course theres me. Still don't like using types. But that way it can be easier to reset everything just free the vars. Not to brag or anything. Nice job so far by the way. And I changed the distance between the dots so the player can't pick up more than one dot at a time. I am sorry if the code is a little messy because I did this pretty quick and besides, it looks like you could fix it up pretty quick. Oh and I added rows instead of just columns of dots. Lemme know if your confuse and just post the code your confused about and I can switch it to psuedicode for you pretty quick I hope.


David819(Posted 2005) [#29]
Thanks, i recieved your email, Looks good from the compiled version, haven't looked at the code yet though, will do soon. Thanks for the help with part, should help me get further. thanks again. I'll Post again when i've looked through the coding.


n8r2k(Posted 2005) [#30]
You can map collisions with the maze and the player using my maze two. You can also probably change the color of the collision part to 1,1,1.


David819(Posted 2005) [#31]
Ok, I've looked over the coding and done a small test, though the code is good, it fails for this game, it just doesn't work with maps properly, a tile system would work better. Any idea how to do this.


n8r2k(Posted 2005) [#32]
Well, you could make a tile map using only unfilled white squares with blacklines, allowing only 1 dot per tile, then fill in the squares that you don't want dots to be in, then fill the tiles that have dots with black so that it is masked out and no collisions are found.


n8r2k(Posted 2005) [#33]
When you finish your game, send me a copy with the source code if possible. Id like to see how you do everything because its already a pretty nice code as it is


David819(Posted 2005) [#34]
Ok will do, but still working with the dots and mazes at the moment. but will send it to you when i'm finished.


_PJ_(Posted 2005) [#35]
From my old spectrum days, programming a pac man maze involved a simple data structure (could be done with an array) - Quick example:

Data 1,1,1,1,1,1,1,1,1
Data 1,2,0,1,0,0,0,2,1
Data 1,1,0,0,0,1,0,1,1
Data 1,1,1,1,0,0,0,0,1
Data 1,0,1,0,0,1,1,0,1
Data 1,0,1,0,1,1,0,0,1
Data 1,0,0,0,1,0,0,1,1
Data 1,1,1,0,0,0,1,1,1
Data 1,2,0,0,1,0,0,2,1
Data 1,1,1,1,1,1,1,1,1

Where:
1 = Wall
0 = Dot (Space)
2 = Power Pill


Although now such data banks can be loaded in from files and created much simpler using spreadsheet software etc.


David819(Posted 2005) [#36]
That is the sort of thing i need to do, using arrays but not sure how to.


BlackJumper(Posted 2005) [#37]
... or you could make a bitmap image and use ReadPixel to see where walls, dots, etc. start off (and maybe even use WritePixel to update the game, so that you could pause/save and return to it later ??)

e.g.


N.B. If you zoom in you will see that the dots are yellow where the power pills would be.

An even more useful scheme would use different coloured dots for the different types of corners, so that you could use a nice rounded tile going in the correct direction.

green = [
purple = ]
brown = L
silver = ¬
...
etc.

You could probably code up a maze editor easily using tiling examples in the codearcs and then write out the dots to a bitmap easily...


uletus(Posted 2005) [#38]
...