doki Posted March 15, 2015 Share Posted March 15, 2015 Hi guys, I've been searching everywhere for this one, I know it will be a popular code. I'm making my first game and simply want an enemy to pace left and right at the top of the screen. I'm new to batari, but whenever I've used Spectrum Basic, I've literally changed the graphic once it reaches the appropriate edge of the screen, ie, leftfacing becomes rightfacing, and somewhere I'll put in 'if leftfacing then x=x-1' etc. I tried to do the same in batari with 'if REFP1=8 then player1x=player1x-1' and changing REFP1 numbers when it reaches a point near the screen edge but this isn't working, neither is doing stuff like assigning a direction value to read whether the character's facing left (if dir=8...) or right (if dir=0...) and going 'if dir=0 then REFP1=0: player1x=player1x+1. I'm sure there'll be something obvious and simpler than what I'm doing but I think I'm close to finishing my first game and this is driving me crazy! Thanks for any help anyone can offer. This seems like a great community! Quote Link to comment Share on other sites More sharing options...
+atari2600land Posted March 15, 2015 Share Posted March 15, 2015 I don't think you can use if statements with the reflecting values. Have you "dim"med the dir, like give it one of the 26 variables? for example, "dim dir=a" Quote Link to comment Share on other sites More sharing options...
Papa Posted March 15, 2015 Share Posted March 15, 2015 (edited) One way is to have an identical second background (or just a section) that has REF in the beginning. All the commands could be the same sans sprite and background information. Once you draw the playfield it stays that way until you change it. This goes for the sprites, too. Just have the command go to that instance when the player1 left side (or right side, based on where it starts) has met it's limit. startup player1x=120:player1y=40 player0x=20:player0y=40 goto characterrightoleft characterrighttoleft payer1x=player1x-1 playfield: *playfield player0sprite: *sprite player1sprite: *sprite end drawscreen if player1x=20 then goto characterlefttoright goto characterrighttoleft characterlefttoright REFP1=8 player1x=player1x+1 end drawscreen if player1x=120 then goto characterrighttoleft goto characterlefttoright ..this is an easy way to get the player1 sprite to rove from right to left. Character represents the badguy and there is no control code in this (you add it), but it does display the player1 sprite on the right side and then when they reach the left side they reflect and then start to go back to the right. Edited March 15, 2015 by Papa Quote Link to comment Share on other sites More sharing options...
doki Posted March 15, 2015 Author Share Posted March 15, 2015 (edited) Thanks, Mr NEC! "Dims" are new to me (that's why I ke[t getting the black screen) but I got it to work with this code, I put it under player1's sprite if it helps anyone... if player1x>120 then dir=8 if dir=8 then player1x=player1x-1 else player1x=player1x+1 if player1x<20 then dir=0 if dir=0 then REFP1=0 if dir=8 then REFP1=8 PS I realise I could tidy this up a bit as I have two 'if dir=8's, but it's a start. Yeah, I checked with Random Terrain's Basic page and REFP's are write-only. (Edited code to reach safety zones, 20 and 120) And thanks, Papa. Your way looks way more sophisticated, I could definitely do with more 'subs' in my work! Edited March 15, 2015 by doki Quote Link to comment Share on other sites More sharing options...
ZackAttack Posted March 15, 2015 Share Posted March 15, 2015 Due to the way that negative numbers are stored you can just have a single variable to hold the sprites velocity and then simply assign the velocity to the REF register as well. I think you'll find this code is concise and easy to read. It also allows you to control the speed of the sprite which I've demonstrated in the attached bas file. Of course you could do a lot more with the speed such as increasing enemy speed when the player is inline with it. ;******** DONE ONCE ON START/RESTART ********* ; Use dim to give 'a' a friendly name. Any available var can be substituted in place of 'a' dim HorizontalVelocity = a ;Initialize the velocity. Postive means moving right & negative is moving left HorizontalVelocity = 1 ;******** DONE EACH FRAME ********* ;Move the sprite player0x = player0x + HorizontalVelocity ;Change velocity if player0x > 150 then HorizontalVelocity = -1 if player0x < 10 then HorizontalVelocity = 1 ;Update sprite's reflection REFP0 = HorizontalVelocity default.bas Quote Link to comment Share on other sites More sharing options...
Papa Posted March 15, 2015 Share Posted March 15, 2015 (edited) This is my code actually working, as certain names I used are weird.. I would say that ZackAttacks way is really great, too, and I would recommend using DIM stuff, as it is really fun. set kernel_options player1colors playercolors pfcolors set tv ntscbeginning player1x=133:player1y=70 player0x=20:player0y=70end goto GoleftControls if joy0left then player0x=player0x-1:gosub Mirror if joy0right then player0x=player0x+1 if player0x>133 then player0x=133 if player0x<20 then player0x=20 returnMirror REFP0=8 returnGoleft player1x=player1x-1 COLUBK=$AE scorecolor=$AE pfcolors: $BC $BC $BC $BC $BA $F2 $F6 $B8 $CA $CA $CC $CCend playfield: ................................ ......................XXX...XXX. .....................XXXXX.XXXXX .....................XXXXX.XXXXX ......................XXX...XXX. .......................X.....X.. .......................X.....X.. XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXend player0color: $1C $1C $1E $1E $1E $1E $1E $1E $1E $1Eend player0: %01010101 %11111111 %11111111 %11100011 %11011101 %11111111 %11011101 %11101011 %01111110 %00111100end player1color: $46 $44 $44 $44 $44 $44 $44 $42end player1: %00111100 %01111110 %10111101 %11000011 %11111111 %10110111 %01111110 %00111100end gosub Controlsend drawscreen if player1x=20 then goto Goright goto Goleft Goright REFP1=8 player1x=player1x+1 gosub Controlsend drawscreen if player1x=133 then goto Goleft goto Goright RovingEnemy.bin *I threw a cute little gosub in there for controls and boxing the player in so it wasn't repeated.. ..and I just replaced the badguy sprite so it could actually show reflect..DOY! Forgot my coffee this afternoon.. ..and now the player0 sprite reflects as well!! Edited March 15, 2015 by Papa Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.