Board Logo
« The Sprite Byte Series »

Welcome Guest. Please Login or Register.
Sep 25th, 2017, 7:06pm


Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ


« Previous Topic | Next Topic »
Pages: 1 2  Notify Send Topic Print
 sticky  Author  Topic: The Sprite Byte Series  (Read 5164 times)
Janet Terra
Global Moderator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM

Gender: Female
Posts: 3061
xx The Sprite Byte Series
« Thread started on: Mar 12th, 2005, 10:36pm »

Alice Watson's Sprite Byte series is a wonderful tutorial for learning about sprites. The articles are intended for Liberty BASIC, though, and sometimes contain commands nonsupported by Just BASIC. This series is too good to pass up for those of you wanting to learn how to control sprites.

If you've never created a sprite before, read

Sprite Byte: The Absolute Minimum! Newsletter Issue #132

This article starts from the ground up and is a must read if you're just starting out, or need a refresher course. The demo runs just as well from either the Liberty BASIC root directory or the Just BASIC root directory.

SPRITE BYTE: USER-CONTROLLED SPRITE Newsletter Issue #119

Replace the entire [mainLoop] block of code from
[mainLoop]
to
goto[mainLoop]

with

Code:
[mainLoop] 'the entire game runs from this loop
    #1, "When characterInput [checkKey]"
    #1, "Setfocus"
    Wait

[checkKey]  'the entire game runs from this loop
    key$ = Inkey$ 'Read key pressed

    Select Case Asc(Right$(key$, 1))
        Case _VK_LEFT, _VK_NUMPAD4
            Gosub [goLeft]
        Case _VK_UP, _VK_NUMPAD8
            Gosub [goUp]
        Case _VK_RIGHT, _VK_NUMPAD6
            Gosub [goRight]
        Case _VK_DOWN, _VK_NUMPAD5
            Gosub [goDown]
    End Select
Wait
 


and follow along.


In addition, you might also want to look through Alyce's site Alyce's Restaurant for other demos. There's a very nice demo entitled Sprite Move Demo that shows how to read a mouseclick, then move the sprite toward that mouseclick. Remove the
Code:
cursor crosshair
 

line first, but everything else runs great. This demo complements the newsletter series nicely. The demo uses sprites from the Just (or Liberty) BASIC sprite folder, so run it from your Just BASIC directory.

Check out all the other cool stuff at Alyce's Restaurant as well. Most of the LB v3.03 code should run in JB with very little modification.


« Last Edit: May 8th, 2005, 11:50pm by Janet Terra » User IP Logged

Janet Visit me at BASIC Samplings
Janet Terra
Global Moderator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM

Gender: Female
Posts: 3061
xx Re: The Sprite Byte Series
« Reply #1 on: Mar 13th, 2005, 08:57am »

SPRITE BYTE: SCROLLING BACKGROUND Newsletter Issue #120

Alyce's second installment demonstrates how to scroll the background for a 'moving' effect. The BACKGROUNDXY command isn't supported by Just BASIC.

This article does show how to load a bitmap and set it as the background. You can change a background by loading another bitmap and then setting the new as the background, but this method is not efficient enough to substitute for the BACKGROUNDXY command.
User IP Logged

Janet Visit me at BASIC Samplings
Janet Terra
Global Moderator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM

Gender: Female
Posts: 3061
xx Re: The Sprite Byte Series
« Reply #2 on: Mar 13th, 2005, 09:03am »

SPRITE BYTE: SPRITE SCALING Newsletter #121

The SPRITESCALE command allows for greater and lesser image displays of your sprite. This means only one sprite bitmap is required regardless of the size, or sizes, needed. In this demo, the Call kernel#32 "sleep" command is used. Substitute the sleep call with the Timer / Wait method to make the demo compatible with Just BASIC

Change the FOR NEXT group to
Code:
for i = 10 to 200
    #w.g "spritexy smiley 0 ";i
    #w.g "spritescale smiley ";i
    #w.g "drawsprites"
    timer 20, [resumeLoop]
    wait
    [resumeLoop]
    timer 0
next
 

Please note the change from
i = 1 to 200
to
i = 10 to 200

Reducing the spritescale to < 3 caused the program to crash on my computer in both Liberty BASIC and Just BASIC. This may be due to my monitor display settings, though.

User IP Logged

Janet Visit me at BASIC Samplings
Janet Terra
Global Moderator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM

Gender: Female
Posts: 3061
xx Re: The Sprite Byte Series
« Reply #3 on: Mar 13th, 2005, 09:12am »

Alyce continues the Sprite Byte series with showing how to move multiple sprites. The sprites associated with this and continuing demo are found in the newsletter files archive rather than the sprite folder of the Just BASIC root directory. Be sure to download the zip archive Newsletter #122 and all associated files.

SPRITE BYTE: SHOOTING Newsletter #122

Nonsupported Just BASIC Commands
CallDLL #kernel32 "sleep"
Backgroundxy
Spritetravelxy

Download the Newsletter Archive. There are 3 associated .bas files. You will only need 2:

shoot.bas and shoot1.bas

In the [moveship] block of code, remove the
Code:
#1 "background ";bgX;" ";bgY
 

command. You won't get the 'twinkling starry sky' effect. Instead you'll get a static background, not as pretty but functions quite well.

Change [mainLoop] from
Code:
[mainLoop]
    SCAN    'scan for user events
    gosub [moveship]
    'slight pause to slow action:
    calldll #kernel32, "Sleep",10 as long, re as void
    goto [mainLoop]
 


to

Code:
[mainLoop]
    gosub [moveship]
    'slight pause to slow action:
    Timer 10, [resumeMainLoop]
    Wait
    [resumeMainLoop]
    Timer 0
    goto [mainLoop]
 


shoot2.bas is the same demo as shoot1.bas, except that it uses the spritetravelxy command rather than checking the xy coordinates and then using IF THEN. It is only the block of code in [goShoot] that differs between shoot1.bas and shoot2.bas. Just BASIC doesn't support the spritetravelxy command, so, use the IF THEN method demonstrated in the shoot1.bas version.


User IP Logged

Janet Visit me at BASIC Samplings
Janet Terra
Global Moderator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM

Gender: Female
Posts: 3061
xx Re: The Sprite Byte Series
« Reply #4 on: Mar 13th, 2005, 1:12pm »


SPRITE BYTES: SHOOTING MULTIPLE MISSILES Newsletter #124

Nonsupported Just BASIC Commands
CallDLL #kernel32 "sleep"
Backgroundxy
Spritetravelxy

Download the Newsletter Archive. There is just one associated .bas file.

SPRITETRAVELXY is a Liberty BASIC v4.x command that is a specialized SPRITEMOVEXY command. It allows the programmer to predetermine what will happen if the sprite gets to a certain x,y coordinate. While SPRITETRAVELXY is not supported by Just BASIC, the SPRITEMOVEXY command is fully supported and can be used to accomplish the same event.

The Liberty BASIC way (from the Help File)
Code:
#w.g "spritetravelxy SpriteName 200 250 5 [landed]"
 

meaning, everytime a DRAWSPRITES command is issued, the sprite should travel 5 pixels closer to position 200, 250. When 200, 250 is arrived at, execute the code at [landed]

The Just BASIC way
Code:
#w.g, "spritemovexy SpriteName 5 5"
 

meaning, everytime a DRAWSPRITES command is issued, increase the x position by 5 pixels and increase the y position by 5 pixels. Then the code simply has to retrieve the x and y coordinates with the SPRITEXY? command and manually check to see if the sprite has arrived at the desired x, y position. This is accomplished with changing the [checkShoot] block of code from a separate event ending with WAIT to a GOSUB ending with RETURN. In the [mainLoop], right after updating the ship's x,y coordinates
Code:
gosub [moveship]
 

check to update the x,y coordinates of the 10 shots.
Code:
gosub [checkShoot]
 


Here are the changes to make in the shoot3.bas file included with the zip archives of Liberty BASIC Newsletter #124. Again, you'll need to remove the BACKGROUNDXY command.

Change
Code:
[mainLoop]
    SCAN    'scan for user events
    gosub [moveship]
    'slight pause to slow action:
    calldll #kernel32, "Sleep",10 as long, re as void
    goto [mainLoop]

 

to
Code:
[mainLoop]
    gosub [moveship]
    gosub [checkShoot]
    'slight pause to slow action:
    timer 10, [resumeLoop]
    wait
    [resumeLoop]
    timer 0
    goto [mainLoop]
 


Change
Code:
[goShoot]
    activeShot=activeShot+1
    if activeShot>10 then activeShot=1
    #1 "spritexy? ship x y"
    'start shot near center of ship
    shotX=x+15:shotY=maxY
    'set shot to travel to top of screen and
    'fire event when it gets there
    #1 "spritexy shot"+str$(activeShot)+" ";shotX;" ";shotY
    #1 "spritetravelxy shot"+str$(activeShot)+" ";shotX;"  0 5 [checkShoot]"
    playwave "laser.wav", async
    return
 

to
Code:
[goShoot]
    activeShot=activeShot+1
    if activeShot>10 then activeShot=1
    #1 "spritexy? ship x y"
    'start shot near center of ship
    shotX=x+15:shotY=maxY
    'set shot to travel to top of screen and
    'fire event when it gets there
    #1 "spritexy shot"+str$(activeShot)+" ";shotX;" ";shotY
    'spritemovexy commands changes x, y positions by nIncrements
    'with each drawsprites command
    #1 "spritemovexy shot"+str$(activeShot)+" 0 -15"
    playwave "laser.wav", async
    return
 


Change
Code:
[moveship]
    'put sprites in new location
    bgY=bgY-2   'scroll background
    #1 "backgroundxy ";bgX;" ";bgY
    #1 "spritexy ship ";shipX;" ";shipY
    #1 "drawsprites"    'update screen!
    RETURN
 

to
Code:
[moveship]
    'put sprites in new location
    #1 "spritexy ship ";shipX;" ";shipY
    #1 "drawsprites"    'update screen!
    RETURN
 


Any program written in Liberty BASIC v3.x will use the MOVESPRITEXY with a routine to check the x,y coordinates as SPRITETRAVELXY wasn't introduced until Liberty BASIC 4.0.


User IP Logged

Janet Visit me at BASIC Samplings
Janet Terra
Global Moderator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM

Gender: Female
Posts: 3061
xx Re: The Sprite Byte Series
« Reply #5 on: Mar 13th, 2005, 5:03pm »

SPRITE BYTE: COMPUTER CONTROLLED SPRITE Newsletter #125

Download Newsletter #125 with zip archive file to get the associated shoot4.bas and sprite bitmap files.

In this Sprite Byte installment, an enemy ship is introduced. The coding commands for this enemy ship are all supported by Just BASIC. If you are following along in the series, make sure you have made all the changes to shoot4.bas that were made in shoot3.bas


See Reply #4 to this thread and make those same 3 changes in [mainLoop], [goShoot] and [moveship].




User IP Logged

Janet Visit me at BASIC Samplings
Janet Terra
Global Moderator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM

Gender: Female
Posts: 3061
xx Re: The Sprite Byte Series
« Reply #6 on: Mar 13th, 2005, 5:09pm »

SPRITE BYTE: COLLISON DETECTION Newsletter #126

Once again, you'll need to download Newsletter #126 zip archive files to get the code and bitmap files.

There is no Spritetravelxy command in this demo, so just make the necessary changes to [mainLoop]

Code:
[mainLoop]
    gosub [moveship]
    gosub [moveEnemy]
    if activeShot then gosub [moveShot]
    gosub [detectCollisions]
    #1.g "drawsprites"    'update screen!
    if lives=0 then [quit]
    'slight pause to slow action:
    timer 10, [resumeLoop]
    wait
    [resumeLoop]
    timer 0
    goto [mainLoop]
 


and [moveship]
Code:
[moveship]
    'put sprites in new location
    #1.g "spritexy ship ";shipX;" ";shipY
    #1.g, "drawsprites" 'update screen!
    RETURN
 


This installment concludes Alyce's Asteroids game, but the series continues with more Sprite Commands.

User IP Logged

Janet Visit me at BASIC Samplings
Janet Terra
Global Moderator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM

Gender: Female
Posts: 3061
xx Re: The Sprite Byte Series
« Reply #7 on: Mar 13th, 2005, 10:10pm »

SPRITE BYTE: CHANGING THE SPRITE IMAGE Newsletter #128

Download the Newsletter #128 zip archive to get the required bitmap sprites.

The only modification spritecycle.bas requires is substituting the call sleep delay with a timer/wait delay. Change the [loop] code to

Code:
[loop]
    #main.g "drawsprites"
    timer 200, [resumeLoop]
    wait
    [resumeLoop]
    timer 0
    goto [loop]
 


The spriteimage.bas demo will run perfectly as is.
User IP Logged

Janet Visit me at BASIC Samplings
Janet Terra
Global Moderator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM

Gender: Female
Posts: 3061
xx Re: The Sprite Byte Series
« Reply #8 on: Mar 13th, 2005, 10:12pm »


SPRITE BYTE: MANUAL CYCLE AND SOUND Newsletter #129

Download the Newsletter #129 zip archive to get the required bitmap sprites.

Once again, the only modification required for the spritewav.bas demo is in the [loop] code where you will need to substitute the Call kernel#32 "sleep" delay with a timer/wait delay.

Code:
[loop]
    frame=frame+1   'advance frame of animation
    if frame>3 then frame=1
    #main.g "spriteimage guy club";frame
    #main.g "drawsprites"
    if frame=3 then
        playwave "hit.wav",async
    end if
    timer 200, [resumeLoop]
    wait
    [resumeLoop]
    timer 0
    goto [loop]
 


To learn more about the PLAYWAVE command, see Adding Sound with PLAYWAVE
User IP Logged

Janet Visit me at BASIC Samplings
Janet Terra
Global Moderator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM

Gender: Female
Posts: 3061
xx Re: The Sprite Byte Series
« Reply #9 on: Jul 15th, 2005, 07:37am »

This installment revisits the missile game. A status bar is becomes a realtime scoreboard. With a few modifications, this demo can become Just BASIC complaint.

SPRITE BYTE: ADDING A SCOREBOARD OR STATUS PANEL Newsletter #131

Nonsupported Just BASIC Commands
Stylebits
CallDLL #kernel32 "sleep"
Backgroundxy
Spritetravelxy


These are the Just BASIC modifications

Stylebits
The demo uses the Stylebits _WS_THICKFRAME to put a 3D like frame around the lower status panel. Stylebits aren't supported by Just BASIC, but a similar effect can be achieved drawing a horizontal line at the lower edge of the graphicbox. The graphicbox will need to be lengthened by 2 pixels and the statictext also lowered by 2 pixels to give room for this horizontal line.

Find the GUI setup in the code before opening the window. There are 3 changes to make before the window is opened.

#1 - Remove the Stylebits command completely
Code:
stylebits #1.s, _WS_THICKFRAME,0,0,0
 


#2 - Lower the statictext for the status bar (maxY+22) Change
Code:
statictext #1.s, space$( 50) + "Shots Fired: 0",0,maxY+20,WindowWidth-5,36
 


#3 - Lengthen the graphicbox by changing
Code:
graphicbox #1.g, 0,0,maxX,maxY+20
 

to
Code:
graphicbox #1.g, 0,0,maxX,maxY+22
 


Once the window is open, a thick horizontal line needs to be drawn. This line will become part of the captured sprite background. Since the graphicbox has been lengthened by 2 pixels, the getbmp area must also be lengthened by 2 pixels. The original block of code was
Code:
    'create a black background with stars:
    #1.g "down; fill black; color white"
    for i = 1 to 200
        x = int(rnd(0)*maxX)
        y = int(rnd(0)*maxY)
        #1.g "set ";x;" ";y
    next
    #1.g "getbmp bkg 0 0 ";maxX;" ";maxY

    'set the background
    #1.g "background bkg"
 

The JB block of code should be
Code:
    'create a black background with stars:
    #1.g "down; fill black; color white"
    for i = 1 to 200
        x = int(rnd(0)*maxX)
        y = int(rnd(0)*maxY)
        #1.g "set ";x;" ";y
    next
    #1.g, "color buttonface; backcolor buttonface"
    #1.g, "place 0 ";maxY + 18
    #1.g, "boxfilled ";maxX;" ";maxY +21
    #1.g "getbmp bkg 0 0 ";maxX;" ";maxY+22

    'set the background
    #1.g "background bkg"
 


CallDLL #kernel32
Replace the Sleep API call with a Timer. Remove the SCAN command, as WAIT will recognize user events. In the [mainLoop] block of code change
Code:
[mainLoop]
    SCAN    'scan for user events
    gosub [moveship]

    gameTime=time$("seconds")-startTime
    msg$=space$(50) + "Shots Fired: " ; shotsFired
    msg$=msg$ + space$(20) + "   Time Elapsed: ";gameTime
    #1.s msg$ 

    'slight pause to slow action:
    calldll #kernel32, "Sleep",10 as long, re as void
    goto [mainLoop]
 

to
Code:
[mainLoop]
    gosub [moveship]

    gameTime=time$("seconds")-startTime
    msg$=space$(50) + "Shots Fired: " ; shotsFired
    msg$=msg$ + space$(20) + "   Time Elapsed: ";gameTime
    #1.s msg$ 
    'slight pause to slow action:
    timer 10, [resume]
    wait
    [resume]
    timer 0
    goto [mainLoop]
 


Spritetravelxy
The spritetravelxy is similar to the spritemovexy command. In most cases, an If ... Then statement accompanies the spritemovexy command to see if something is happening each time the sprite moves. In this case, no action needs to take place, because a y value less than 0 does effectively remove the sprite from the screen and that's what is desired. Replace

Code:
#1.g "spritetravelxy shot"+str$(activeShot)+" ";shotX;"  0 5 [checkShoot]"
 

with
#1.g, "spritemovexy shot";str$(activeShot) + " 0 -5"
[/code]
and remove the [checkShoot] block of code completely as it's not needed.

Backgroundxy
In Liberty BASIC, the background can be scrolled by changing the x and/or y values of the backgroundxy command. Unfortunately, this is not possible with Just BASIC. There is no easy fix for this, so your background will remain static. That doesn't affect playability, though. In the [moveship] block of code, remove these two lines
Code:
    bgY=bgY-2   'scroll background
    #1.g "backgroundxy ";bgX;" ";bgY
 


Now you're ready to shoot missiles.

Be sure to read the article for detailed explanations of how the game is created and executed. These modifications will allow you to create the game in Just BASIC rather than Liberty BASIC, but do not and can not stand as a substitution for the actual content of the newsletter.


User IP Logged

Janet Visit me at BASIC Samplings
Janet Terra
Global Moderator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM

Gender: Female
Posts: 3061
xx Re: The Sprite Byte Series
« Reply #10 on: Jul 15th, 2005, 07:41am »

A Must-Read for any coder new to sprites is
SPRITE BYTES: THE ABSOLUTE MINIMUM

This gives you all the preliminary information you need to start adding sprites to your own programs. Run the code 'as is' in Just BASIC. No modifications needed. smiley

« Last Edit: Jul 15th, 2005, 07:42am by Janet Terra » User IP Logged

Janet Visit me at BASIC Samplings
Janet Terra
Global Moderator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM

Gender: Female
Posts: 3061
xx Re: The Sprite Byte Series
« Reply #11 on: Jul 15th, 2005, 07:50am »



SPRITE BYTES: CYCLING ANIMATION AND THE TIMER

Alyce continues her 'Back to Basics' with this article detailing how to animate your sprite cycling through different images. The demo already uses the timer and wait command rather than the CallDLL #kernel32 "Sleep" API call, so no changes are necessary. Just copy and paste into your JB editor and you're good to go! smiley
User IP Logged

Janet Visit me at BASIC Samplings
Welopez
Moderator
ImageImageImageImageImage


member is offline

Avatar

Never let your beliefs get in the way of learning.


PM

Gender: Male
Posts: 4407
xx Re: The Sprite Byte Series
« Reply #12 on: Jul 15th, 2005, 08:32am »

Wonderful adaptations and work-arounds, Janet! What more does a coder need to do really neat stuff with Sprites?

This entire thread should be required reading for all our game creators and animators! Thanks to both you and Alyce for the tips and tricks! grin
User IP Logged

JB 1.01
Win7 64bit, 4 GB RAM, Pentium 6200@2.13 GHz (laptop)
WinXP, 1 GB RAM, Intel N270@1.6 GHz (netbook)
Bill W.
Full Member
ImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 303
xx Re: The Sprite Byte Series
« Reply #13 on: Aug 5th, 2005, 05:37am »

Well, I always have wanted to make a game... So I will. Finally though, a guide to sprites in JB!
User IP Logged

Janet Terra
Global Moderator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM

Gender: Female
Posts: 3061
xx Re: The Sprite Byte Series
« Reply #14 on: Aug 6th, 2005, 11:54pm »

Glad you're finding these to be of use. smiley
The Liberty BASIC Newsletter #134 has Sprite Bytes: More Cycling. In this installment, Alyce tells how to load two or more sprites from the same bitmap to create cycled animation. The .bas file, spritebug.bas, is fully Just BASIC compatible. Be sure to download the zip archive to get all associated files.
User IP Logged

Janet Visit me at BASIC Samplings
Pages: 1 2  Notify Send Topic Print
« Previous Topic | Next Topic »

Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls