November 28, 2020, 11:51:15 AM

Author Topic: [bb] Sprites2Go - C64 Sprite - BB3d (bb+?!) by Dan [ 1+ years ago ]  (Read 823 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Sprites2Go - C64 Sprite - BB3d (bb+?!)
Author : Dan
Posted : 1+ years ago

Description : With this addon, you can have the Sprites drawn in your code, or have the Sprites, like they were used on the Commodore 64 machines,in the Data code.

Collision detection is not implemented, youll have to write one for your game, see below.

Usage:

First youll need to add collors to the Palette.

Call AddPal (Red,Green,Blue) with the desired values, in the range of 0 to 255 ($0-$ff), multiple times.
The first addpal entry will always be transparent, and is drawn with the space key. (" ")
You can add up to 64 colors.

Then you can use either c64ds() or c64datspr() functions to draw your sprites.

The standard commodore 64 sprite settings are used here, which is 24x21.

C64ds (dat$,newspr):

to draw 1 sprite you need to use this function 21 times.  

Colors used are defined as chars from left to right: " "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

To draw an transparent pixel, you should use " ", Color number 1 would then be 0, Color #2 would be 1, color #11 = a and so on.

here is a small example:

C64ds ("   1");1
c64ds ("  11");2..
c64ds (" 222");20
c64ds ("fFfF");21 <- f and F are different colors ! (if defined by the AddPal() function)

Dat$ shall contain, (if not changed) up to 24 chars. To save bytes, transparent pixels to the right side, can be left out:

E.g.: c64ds ("  111122223333     ") can be written as c64ds ("  111122223333")
edit c64ds ("  111122223333     ") above has 8x" " after the 3333 !

and if the whole line contains only transparent pixels, then it can be written as c64ds (" ")

The newspr flag can be used to start a new Sprite drawing, (the old sprite will, then, be saved to a larger bitmap).
You do not need to use the newspr flag, if your sprites have 21 lines each.
At the 22th line, the sprite will be automaticaly stored to a larger bitmap!
C64datspr(dat,newsprite,color1,multicolor=2,color3) :

dat can be a number between 0 and 255, which represents, in the binary format, the pixels which will be drawn. (see C64 sprite data format : <a href="https://www.c64-wiki.com/index.php/Sprite" target="_blank"> here [/url])

If not changed, youll need to provide 3 Bytes of data for each Sprite line, 21 lines (which are standard setting)

newsprite can be used to start a new sprite, but can be left to 0, if you provide the 21 lines of data (which will automaticaly start the new sprite)

C64 sprites, in hires mode have only 2 colors. so for hires sprites there is only transparent and the color1 (1-63) - from your palete.

If your sprite data is in Multicolor format, which is Lowres, you have to set the multicolor and color3 flag to the number from your palette.
This will draw in the lowres format.

There is no error checking at all, so basicaly you can switch between lowres and hires colors (which is not possibile on the Real c64.
 
The only limitation is, if you draw a sprite, you can have only C64datspr or c64ds lines for one sprite, using them after eachother will start a new sprite.

c64_finalize()

Needs to be called,without parameter , after the last sprite is drawn ((after the last c64datspr or c64ds) to complete the copying process to the larger bitmap)

c64drawsprite (nr,x,y)

This will draw an image of the sprite to the Current Buffer.
If you need to do Collision check, then youll need to do something like this for each sprite on the screen:

Code: [Select]
;pseudo code, this is not tested
spr1=CreateImage (c64ispr_width,c64ispr_height)
spr2=CreateImage (c64ispr_width,c64ispr_height)
...
setbuffer imageBuffer(spr1)
c64drawsprite(nr)

setbuffer imageBuffer(spr2)
c64drawsprite(nr)

if ImagesCollide (spr1,x,y,0,spr2,x,y,0)
   ;collision
EndIf

The last function, which can be used in your program, to access the colors of the palette is:

c64color(nr)  (which is same as Color $xx,$xx,$xx)
And a Demo, which says more than 1000 words:

Code: [Select]
;Screen (320,350,1)
Graphics 320,250,32,6
Graphics 320,250,32,7 ;Make the Resizeable window

Include "c64sprite_inc.bb"
SeedRnd MilliSecs()

temp=Rand(0,1) ; Each start use a randomly choosen Palette :

;here are 2 WinVice modified palettes:
 
If temp=1
AddPal ($00,$00,$00) ; transparent " "
AddPal ($01,$00,$00) ; Black 0
AddPal ($FF,$FF,$FF) ; White 1
AddPal ($88,$20,$00) ; Red 2
AddPal ($68,$d0,$a8) ; Cyan 3
AddPal ($a8,$38,$a0) ; Purple 4
AddPal ($50,$b8,$18) ; Green 5
AddPal ($18,$10,$90) ; Blue 6
AddPal ($f0,$e8,$58) ; Yellow 7
AddPal ($a0,$48,$00) ; Orange 8
AddPal ($47,$2b,$1b) ; Brown 9
AddPal ($c8,$78,$70) ; Light Red a
AddPal ($48,$48,$48) ; Dark Gray b
AddPal ($80,$80,$80) ; Medium Gray c
AddPal ($98,$ff,$98) ; Light Green d
AddPal ($50,$90,$d0) ; Light Blue e
AddPal ($b8,$b8,$b8) ; Light Gray f
Else
Restore paldat
For x=1 To 17
Read r,g,b
addpal(r,g,b)
Next
EndIf

.paldat
Data $0,$0,$0, $00,$00,$01 ,$FF,$FF,$FF ,$68,$37,$2b ,$70,$a4,$b2 ,$6f,$3d,$86 ,$58,$8d,$43 ,$35,$28,$79 ,$b8,$c7,$6f ,$6f,$4f,$25 ,$43,$39,$00 ,$9a,$67,$59 ,$44,$44,$44 ,$6c,$6c,$6c ,$9a,$d2,$84 ,$6c,$5e,$b5 ,$95,$95,$95


;To display the Template correctly, use Fixed width font, like "Courier New","Lucida Console" or "Terminal"
; this is 24x21 template
;X=     123456789012345678901234  v-- y
;c64ds("                        ",1);01 ;Template ! use overwrite mode to draw inside ""
;C64DS("                        ") ;02 ;,1 Starts a new sprite drawing
;c64ds("                        ") ;03 ;if the whole line is transparent, you can use " " instead
;c64ds("                        ") ;04
;c64ds("                        ") ;05
;c64ds("                        ") ;06
;c64ds("                        ") ;07
;c64ds("                        ") ;08
;c64ds("                        ") ;09
;c64ds("                        ") ;10
;c64ds("                        ") ;11
;c64ds("                        ") ;12
;c64ds("                        ") ;13
;c64ds("                        ") ;14
;c64ds("                        ") ;15
;c64ds("                        ") ;16
;c64ds("                        ") ;17
;c64ds("                        ") ;18
;c64ds("                        ") ;19
;c64ds("                        ") ;20
;c64ds("                        ") ;21

.again
For z=1 To 1 ;Increase this to 250 or 500 to test multiple sprite pages (will be 250*10 sprites in this case!)
temp=(temp+1) Mod 10 ;make the program responsive, while reading data (for large for z= loop !)
If temp=1 Then Delay 2

;Theese are test sprites, with added pixels, to see if they were drawn correctly
c64ds ("  ffffffffffffffffffff  ");1
c64ds ("                        ");2
c64ds ("   111111111111111111   ");3
c64ds ("  1                  1  ");4
c64ds (" 1                    1 ");5
c64ds (" 1   2222      2222   1 ");6
c64ds (" 1  2    2    222222  1 ");7
c64ds (" 1  2 77 2    222222  1 ");8
c64ds (" 1  2    2    222222  1 ");9
c64ds (" 1   2222      2222   1 ");10
c64ds (" 1                    1 ");11
c64ds (" 1                    1 ");12
c64ds (" 1                    1 ");13
c64ds (" 1                    1 ");14
c64ds (" 1  34            43  1 ");15
c64ds (" 1   34444444444443   1 ");16
c64ds (" 1    333333333333    1 ");17
c64ds (" 1                    1 ");18
c64ds ("  1                  1  ");19
c64ds ("   111111111111111111   ");20
c64ds ("                        ");21

c64ds ("3                      3") ;1
c64ds (" ") ;2
c64ds ("   111111111111111111   ") ;3
c64ds ("  1                  1  ") ;4
c64ds (" 1                    1 ") ;5
c64ds (" 1   2222      2222   1 ") ;6
c64ds (" 1  2    2    2    2  1 ") ;7
c64ds (" 1  2 77 2    2 77 2  1 ") ;8
c64ds (" 1  2    2    2    2  1 ") ;9
c64ds (" 1   2222      2222   1 ") ;10
c64ds (" 1                    1 ") ;11
c64ds (" 1                    1 ") ;12
c64ds (" 1                    1 ") ;13
c64ds (" 1                    1 ") ;14
c64ds (" 1  34            43  1 ") ;15
c64ds (" 1   34444444444443   1 ") ;16
c64ds (" 1    333333333333    1 ") ;17
c64ds (" 1                    1 ") ;18
c64ds ("  1                  1  ") ;19
c64ds ("   111111111111111111   ") ;20
c64ds ("3                      3") ;21

c64ds ("         3333333        ");1
c64ds ("       33333333333      ");2
c64ds ("      3333333333333     ");3
c64ds ("      33333   33333     ");4
c64ds ("     33333 333   333    ");5
c64ds ("     33333 333 33333    ");6
c64ds ("4    33333 333   333   5");7
c64ds ("4     33333   33333    6");8
c64ds ("      3333333333333    7");9
c64ds ("      3333333333333     ");10
c64ds ("      3 333333333 3     ");11
c64ds ("       3 3333333 3      ");12
c64ds ("       3  33333  3      ");13
c64ds ("        3  333  3       ");14
c64ds ("        3  333  3       ");15
c64ds ("         3  3  3        ");16
c64ds ("         3  3  3        ");17
c64ds ("          33333         ");18
c64ds ("          33333         ");19
c64ds ("          33333         ");20
c64ds ("           333          ");21

Restore spr

For x=1 To 63 ;Multicolor sprite data
Read dat
c64datspr(dat,0,2,5,8)
Next

For y=1 To 3 ;There are 3 sprite animations to read
   ;testc=Rand(2,c64ipalcount) ;Random color
testc=2 ;Fixed color (here 2 - white)

For x=1 To 63 ;Read 1 sprite data
Read dat
c64datspr(dat,x<2,testc)    ;x<2 here makes 1, if x is less than 2, or 0, if x is greater number
Next
Next ;y

c64ds ("111111111111111111111111");1
c64ds ("222221222222222222222222");2
c64ds ("333333233333333333333333");3
c64ds ("444444434444444444444444");4
c64ds ("555555554555555555555555");5
c64ds ("666666666566666666666666");6
c64ds ("777777777767777777777777");7
c64ds ("888888888887888888888888");8
c64ds ("999999999999899999999999");9
c64ds ("aaaaaaaaaaaaa9aaaaaaaaaa");10
c64ds ("abbbbbbbbbbbbbabbbbbbbba");11
c64ds ("accccccccccccccbccccccca");12
c64ds ("ddddddddddddddddcddddddd");13
c64ds ("eeeeeeeeeeeeeeeeedeeeeee");14
c64ds ("ffffffffffffffffffefffff");15
c64ds ("gggggggggggggggggggfgggg");16 - Unused palette entries are drawn as transparent pixels !
c64ds ("hhhhhhhhhhhhhhhhhhhhghhh");17
c64ds ("iiiiiiiiiiiiiiiiiiiiihii");18
c64ds ("jjjjjjjjjjjjjjjjjjjjjjij");19
c64ds ("kkkkkkkkkkkkkkkkkkkkkkkj");20
c64ds ("llllllllllllllllllllllll");21

c64ds ("cc       bbbbbbb      cd");1
c64ds ("cc     bbbbbbbbbbb    cd");2
c64ds ("cc    bbbbbbbbbbbbb   cd");3
c64ds ("cc    bbbbb   bbbbb   cd");4
c64ds ("cc   bbbbb bbb  bbbb  cd");5
c64ds ("cc   bbbbb bbb bbbbb  cd");6
c64ds ("cc   bbbbb bbb  bbbb  cd");7
c64ds ("cc    bbbbb   bbbbb   cd");8
c64ds ("cc    bbbbbbbbbbbbb   cd");9
c64ds ("cc    bbbbbbbbbbbbb   cd");10
c64ds ("cc    b bbbbbbbbb b   cd");11
c64ds ("cc     b bbbbbbb b    cd");12
c64ds ("cc     b  bbbbb  b    cd");13
c64ds ("cc      b  bbb  b     cd");14
c64ds ("cc      b  bbb  b     cd");15
c64ds ("cc       b  b  b      cd");16
c64ds ("cc       b  b  b      cd");17
c64ds ("cc        bbbbb       cd");18
c64ds ("cc        bbbbb       cd");19
c64ds ("cc        bbbbb       cd");20
c64ds ("cc         bbb        cd");21

c64ds ("         ccccccc        ");1
c64ds ("       ccccccccccc      ");2
c64ds ("      ccccccccccccc     ");3
c64ds ("      cccc   cccccc     ");4
c64ds ("     cccc cccc  cccc    ");5
c64ds ("     cccc cccccccccc    ");6
c64ds ("4    cccc cccc  cccc   5");7
c64ds ("4     cccc   cccccc    6");8
c64ds ("      ccccccccccccc    7");9
c64ds ("      ccccccccccccc     ");10
c64ds ("      c ccccccccc c     ");11
c64ds ("       c ccccccc c      ");12
c64ds ("       c  ccccc  c      ");13
c64ds ("        c  ccc  c       ");14
c64ds ("        c  ccc  c       ");15
c64ds ("         c  c  c        ");16
c64ds ("         c  c  c        ");17
c64ds ("          ccccc         ");18
c64ds ("          ccccc         ");19
c64ds ("          ccccc         ");20
c64ds ("           ccc          ");21
Next

c64_finalize()

SetBuffer BackBuffer()
Cls
x=0
For tmp.c64sprmap = Each c64sprmap
x=x+1
If tmpimage >0
DrawBlock tmpimage,0,0
EndIf
Delay 10
Color $0,$ff,$ff
Text 0,30,"Bitmap number "+x
Text 0,45,"Press any key to show next"
Flip
WaitKey()
Cls
Next

FlushKeys()
x=0
Print "Bitmap Listing:"
For tmp.c64sprmap = Each c64sprmap
x=x+1
Print "Image number ="+x+" IMG Handle="+tmpimage
Next
y=0
Flip
WaitKey()

SetBuffer BackBuffer()
animtimer=MilliSecs()

Repeat

If dir=0
cfl#=(cfl#+0.1) : If cfl#=>50 Then dir=1
Else
cfl#=(cfl#-0.1) : If cfl#<=0 Then dir=0
EndIf
Cls
c=0
For py=0 To GraphicsHeight() Step 10
c=(c+1) Mod 2
For px=0 To (GraphicsWidth()*0.80) Step 10
c=(c+1) Mod 2
If c=1 Then Color $50-cfl#,$0,$0
If c=0 Then Color $0,$50+cfl#,$50+cfl#
Rect px,py,10,10,1
Next
Next
Color $ff,$ff,$0
Text 40,10,"Mice Party - ESCape it"

If MilliSecs()-animtimer>250
animtimer=MilliSecs()
y=(y+1) Mod 3
s=(s+1) Mod 2
t#=(t#+1.5) Mod 60
EndIf

x=(x+1) : If x>350 Then x=-120
c64drawsprite(4+y,x,80)
c64drawsprite(4+y,x+c64ispr_width    ,120+(t#))
c64drawsprite(4+y,x+(c64ispr_width*2),110-(t#))
c64drawsprite(4+y,x+(c64ispr_width*3),120+(t#))
c64drawsprite(4+y,x+(c64ispr_width*4),110-(t#))

c64drawsprite(0+s,10,10)

six#=(six#+0.5) Mod 360
siy#=(siy#-0.9) Mod 360
c64drawsprite(2,100+Cos(six#)*106,100+Sin(six#)*60)
c64drawsprite(8,50+Cos(siy#)*66,50+Sin(six#)*60)
c64drawsprite(9,150+Cos(six#)*66,50+Sin(siy#)*60)
c64drawsprite(7,150+Tan(six#)*66,50+Tan(siy#)*10)
Flip
Delay 1

Until KeyHit(1)

End

.spr
;this is the format which is used To draw one sprite: 21 * 3 bytes - here hexadecimal numbers
;1 multicolored sprite
;c64imaxdrawx=3 in each row of the Sprite. 3*8=  c64ispr_width=24
Data $AA,$55,$FF
Data $AA,$55,$FF
Data $AA,$55,$FF
Data $82,$41,$C3
Data $82,$41,$C3
Data $82,$41,$C3
Data $82,$41,$C3
Data $82,$41,$C3
Data $82,$41,$C3
Data $82,$41,$C3
Data $82,$41,$C3
Data $82,$41,$C3
Data $82,$41,$C3
Data $82,$41,$C3
Data $82,$41,$C3
Data $82,$41,$C3
Data $82,$41,$C3
Data $82,$41,$C3
Data $AA,$55,$FF
Data $AA,$55,$FF
Data $AA,$55,$FF

;3 images of
;dancing mouse from <a href="http://www.zimmers.net/cbmpics/cbm/c64/c64prg.txt" target="_blank">http://www.zimmers.net/cbmpics/cbm/c64/c64prg.txt</a>  (here with correct data statemens !)
Data 30,0,120,63,0,252,127,129,254,127,129,254,127,189,254,127,255,254,63,255,252,31,187,248,3,187,192,1,255,128,3,189,192,1,231,128,1
Data 255,0,31,255,0,0,124,0,0,254,0,1,199,32,3,131,224,7,1,192,1,192,0,3,192,0
Data 30,0,120,63,0,252,127,129,254,127,129,254,127,189,254,127,255,254,63,255,252,31,221,248,3,221,192,1,255,128,3,255,192,1,195
Data 128,1,231,3,31,255,255,0,124,0,0,254,0,1,199,0,7,1,128,7,0,204,1,128,124,7,128,56,30,0,120,63,0,252,127,129,254,127,129,254,127,189
Data 254,127,255,254,63,255,252,31,221,248,3,221,192,1,255,134,3,189,204,1,199,152,1,255,48,1,255,224,1,252,0,3,254,0
Data 7,14,0,204,14,0,248,56,0,112,112,0,0,60,0


p.s. what needs to be checked, is, IF the Drawn bitmap of 50x50 images (50*24pixel width,50*21pixel height = 1200x1050 is possibile on every machine.)
     provided you want to use that many images ;)
and the Include file, which shall be saved as c64sprite_inc.bb : [/i]

Code :
Code: BlitzBasic
  1. ;======================================================================================
  2. ; Project: Sprites 2 Go - C64 sprite drawing method, expandable
  3. ;               Save this as c64sprite_inc.bb
  4. ; Version: 1.0
  5. ; Author: Dan
  6. ; Email: -.-
  7. ; Copyright: PD
  8. ; Description:    Draw Sprites in your Code, or use the C64 sprite data format
  9. ;                   This makes your program Independant on external bitmap
  10. ;                   Usage:
  11. ;                   AddPal(r,g,b) ;Adds color to the Palette, Call this multiple times to add More colors
  12. ;                             1st palette entry will always be transparent (make sure that if you set this to 0,0,0 your black collor shall be 0,0,1
  13. ;                   C64DS (" 1 1 2 3") Draws to the sprite (1 line here). Youll need to draw them manualy, (see at the bottom for the template)
  14. ;
  15. ;                   C64datspr(dat,newsprite,color1,multicolor=2,color3).  Adds C64 sprite data, 1 byte = 8 pixel to the sprite)
  16. ;                             (c64ds and c64datspr functions cannot be mixed/used together to draw to a same spite !, each other saves the current sprite and starts a new one
  17. ;
  18. ;                   c64_finalize() - needs to be called after your drawing is done or you may miss the last image or the whole sprite bitmap
  19. ;
  20. ;                   c64drawsprite (nr,x,y) same as DrawImage, but this one uses your sprite drawings, starting with nr 0, at x,y coordinates
  21. ;                             This function is drawing the Sprites to the current buffer, so if you want to have collison detection with ImagesCollide or ImagesOverlap commands,
  22. ;                             youll need to make image holders for the sprites, set the buffer to their image buffer
  23. ;                             and call this function, with only image number to be copied to the holder (x=0,y=0 are set to default).Then Draw your sprites to the screen and do the collision check.
  24. ;
  25. ;                   aditionaly, you can use c64color(nr) function to set the foreground collor, from the defined palette
  26. ;======================================================================================
  27. ;how this works:
  28. ;You draw a small picture:
  29. ; ####
  30. ; #  #     save it into a bigger one:
  31. ; #### ---> ##################                                                ##################
  32. ;           #  #             #                                                #  #  #  #  #  # #
  33. ;           ####             #      --------------copy------------->          ##################
  34. ;           #                #                                                #  #  #  #  #  # #
  35. ;           ##################                                                ##################
  36. ;                              when the big picture is filled,make a copy,save it into a bitmap,
  37. ;                              and reset the big picture.Repeat the process.
  38.  
  39.  
  40. ;Dont change the lines below, unless you know what you are doing
  41. Const AddPaletteorder$=" 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ;64 colors (63+transparent)
  42.  
  43. Dim c64pal(Len(AddPaletteorder$)+1,2)
  44.  
  45. ;Used to hold the Sprite frames in one or more bitmaps
  46. Type c64sprmap
  47.     Field image
  48. End Type
  49.  
  50. Type c64data
  51.         Field palcount,imgdrawcount
  52.         Field dx,dy,maxdrawx
  53.         Field spr_width,spr_height
  54.         Field c64image,maxsprites
  55.         Field imgcount,NrX,NrY,MaxNrX,MaxNrY
  56.         Field DrawTo,picture[1]
  57.         Field collection,recursion
  58.         Field drawn
  59. End Type
  60.  
  61. Global c64i.c64data = New c64data
  62. Global c64_image.c64sprmap = New c64sprmap
  63. ;Dont change the lines above, unless you know what you are doing
  64.  
  65.  
  66. ;If you have a need to adapt the code to a bigger/smaller size, the next 5 variables should be changed and set up
  67. c64imaxdrawx=3                                  ;c64datspr() - 3 bytes * 8bits = 24 pixel, make sure your data statement provide this many bytes * c64ispr_height
  68.  
  69. ;used for C64datSpr() and C64ds()   - needed for both and need to be adjusted manualy
  70. c64ispr_width=24                                        ;width of the drawn image, for c64 is 3*8=24 !!!        - Correct this manually, if you use your own width/height format
  71. c64ispr_height=21                                       ;height of the drawn image,c64 image standard is 21 !!! - Correct this manually, if you use your own width/height format
  72.  
  73. ;How many Sprite images shall one bitmap contain:
  74. c64iMaxNrX=50                                   ;50 images * 24 pixels = 1200 pixels width    ! depends on spr_width   - for C64storeimg()
  75. c64iMaxNrY=50                                   ;50 images * 21 pixels = 1050 pixels height   ! depends on spr_height   - for C64storeimg()
  76.  
  77. ;#####################################
  78. ;Dont change the values below :
  79. c64ipalcount=0                                  ;count added palette colors
  80. c64iimgdrawcount=-1                             ;c64ds() to count lines drawn, depends on spr_Height setting !
  81. c64idx=1                                                ;c64datspr() - holds the current x drawing position - DONT CHANGE THIS
  82. c64idy=0                                                ;c64datspr() - holds the current y drawing position - DONT CHANGE THIS
  83. c64iimgcount=0                                  ;c64storeImg() - how many images have been stored       - DONT CHANGE THIS
  84. c64idrawn=0                                     ;Check if something was drawn                             DONT CHANGE This
  85. c64iNrX=0                                               ;Holder of the current image on the X axis      - DONT CHANGE THIS
  86. c64iNrY=1                                               ;Holder of the current image on the y axis      - DONT CHANGE THIS
  87. c64iDrawTo=0                                    ;Draw to image buffer holder                            - DONT CHANGE THIS
  88. c64icollection=0                                ;How many image pages are created, (when maxnrx + maxnry) are reached
  89. c64ic64image=CreateImage(c64ispr_width,c64ispr_height)                          ;imagage to draw sprite !
  90.  
  91. ;Dont Change the values above this line.
  92. ;#######################################
  93.  
  94. Function c64drawsprite (nr,x=0,y=0)
  95. ;sprite Nr begins with 0 !
  96.        
  97.         page=(nr)/(c64iMaxNrX*c64iMaxNrY)
  98.         xp=((nr) Mod c64iMaxNrX)*c64ispr_width
  99.         yp=(((nr-(page*(c64iMaxNrX*c64iMaxNrY)))/c64iMaxNrY))*c64ispr_height
  100.         z=0
  101.         For tmp.c64sprmap = Each c64sprmap
  102.                 If z=page
  103.                         DrawImageRect tmpimage,x,y,xp,yp,c64ispr_width,c64ispr_height
  104.                 EndIf
  105.                 z=z+1
  106.         Next
  107.        
  108. End Function
  109.  
  110. Function c64datspr(dat,newspr=0,col1=2,col2=-1,col3=-1)
  111. ;Usage: if col2 is set to a collor (=>0), then it will be a multi collored sprite !
  112. ;Use newspr to force drawing of the next sprite, (this can be left out, if your data has enough bytes to draw 1 whole sprite)
  113. ;
  114. ;draws 8 pixels of a sprite in a line, using commodore 64 sprite data format
  115. ;1 byte equals 8 pixel, where each bit represents 1 pixel
  116. ;
  117. ;Spr width = c64imaxdrawx * 8
  118. ;spr height= c64ispr_height
  119. ;bytes needed for drawing = c64imaxdrawx * c64ispr_height
  120.        
  121.         SetBuffer ImageBuffer(c64ic64image)
  122.        
  123.         If newspr=1
  124.             If c64idrawn>0 Then C64storeimg() : SetBuffer ImageBuffer(c64ic64image)
  125.         ClsColor c64pal(0,0),c64pal(0,1),c64pal(0,2)
  126.                 Cls
  127.         Else
  128.             If c64iimgdrawcount>0 Or c64idy>c64ispr_height-1                            ;Used to save the sprite from the c64ds function !
  129.                         C64storeimg()
  130.                         SetBuffer ImageBuffer(c64ic64image)
  131.                         ClsColor c64pal(0,0),c64pal(0,1),c64pal(0,2)
  132.                         Cls
  133.                 EndIf
  134.         EndIf  
  135.        
  136.         If col2<0                               ;Draw Mono (hires) sprites
  137.                 For x=1 To 8
  138.                         Select BitState(dat,x)
  139.                                 Case 0
  140.                                         C64color(0)
  141.                                 Case 1
  142.                                         C64color(col1)
  143.                         End Select
  144.                         Plot (c64idx*8)-x,c64idy
  145.                 Next
  146.                
  147.         Else                                    ;Draw Multicolored (lowres) sprite
  148.                 For x=1 To 8 Step 2
  149.                         temp=(BitState(dat,x+1)*2)+BitState(dat,x)
  150.                         Select temp
  151.                                 Case 0
  152.                                         C64color (0)
  153.                                 Case 1
  154.                                         C64color (col1)
  155.                                 Case 2
  156.                                         C64color (col2)
  157.                                 Case 3
  158.                                         C64color (col3)
  159.                         End Select
  160.                        
  161.                         Plot (c64idx*8)-x,c64idy
  162.                         Plot ((c64idx*8)-x)-1,c64idy
  163.                 Next
  164.                
  165.         EndIf
  166.         c64idrawn=1
  167.         c64idx=c64idx+1
  168.         If c64idx>c64imaxdrawx
  169.                 c64idx=1
  170.                 c64idy=c64idy+1
  171.         EndIf
  172.        
  173.         SetBuffer BackBuffer()
  174. End Function
  175.  
  176. ;################################################################
  177.  
  178. Function c64ds (dat$,newspr=0)
  179.        
  180.         SetBuffer ImageBuffer(c64ic64image)
  181.         If newspr=1             ;Check if new
  182.             If c64idrawn>0 Then C64storeimg() : SetBuffer ImageBuffer(c64ic64image) ;save to bigger pic if something was drawn
  183.         ClsColor c64pal(0,0),c64pal(0,1),c64pal(0,2)  ;set the background collor (just in case)
  184.                 Cls
  185.         Else
  186.                 If (c64iimgdrawcount<(c64ispr_height-1)) And c64idy=0   ;is the end of the spite reached ? and is c64datspr used
  187.                         c64iimgdrawcount=c64iimgdrawcount+1                                     ;if no, add a new line
  188.                 Else
  189.         ;Save the image to a background image and start a new sprite
  190.                         C64storeimg() : SetBuffer ImageBuffer(c64ic64image)     ;else save it first before drawing new sprite
  191.                         ClsColor c64pal(0,0),c64pal(0,1),c64pal(0,2)
  192.                         Cls
  193.                 EndIf
  194.         EndIf
  195.        
  196.         If Len(dat$)>c64ispr_width              ;is the dat$ longer than the Sprite width ?
  197.                 tmpx=c64ispr_width                              ;then set the tmpx to the length of the sprite
  198.         Else
  199.                 tmpx=Len(dat$)                                  ;set it to the width of the sprite (or length of the line (dat$))
  200.         EndIf
  201.        
  202.         For x=0 To tmpx                         ;Drawing is done here
  203.                 C64color (Instr(AddPaletteorder$,Mid$(dat$,x+1,1))-1)           ;Choose the color from the char
  204.                 Plot x,c64iimgdrawcount                                                                 ;Plot the pixel
  205.         Next
  206.         c64idrawn=1
  207.         SetBuffer BackBuffer()
  208. End Function
  209.  
  210. Function AddPal(r,g,b)
  211. ;r red value, g= green value, b= blue value
  212.         If (r=>0 And r<=$ff) And (g=>0 And g<=$ff) And (b=>0 And b<=$ff)
  213.         ;D'OH  ;)
  214.         Else                    ;invalid collors, set the default to $0
  215.                 r=0
  216.                 g=0
  217.                 b=0
  218.         EndIf
  219.         If c64ipalcount=<Len(AddPaletteorder$)-1
  220.                 c64pal(c64ipalcount,0)=r
  221.                 c64pal(c64ipalcount,1)=g
  222.                 c64pal(c64ipalcount,2)=b
  223.                 c64ipalcount=c64ipalcount+1
  224.         EndIf
  225. End Function
  226.  
  227. Function C64color(number)
  228.         Color c64pal(number,0),c64pal(number,1),c64pal(number,2)
  229. End Function
  230.  
  231. Function C64storeimg(final=0)
  232. ;       DebugLog c64iDrawTo +" "+ c64iNrX+"/" +c64iNrY+" -- " + " "+c64iimgcount
  233.        
  234.         c64iNrX=c64iNrX+1
  235.         c64imaxsprites=c64imaxsprites+1
  236.        
  237.     If c64iNrX>c64iMaxNrX
  238. ;               DebugLog "reached 50 img"
  239.                 c64iNrX=1
  240.                 c64iNrY=c64iNrY+1
  241.         EndIf
  242.        
  243.         If c64iimgcount>c64iMaxNrX-1
  244.                 iw=50*c64ispr_width
  245.         Else
  246.                 iw=c64iNrX*c64ispr_width
  247.         EndIf
  248.         c64ipicture[c64iDrawTo] = CreateImage(iw,c64iNrY*c64ispr_height)
  249.        
  250.         If c64iimgcount >0
  251.                 CopyRect 0,0,ImageWidth(c64ipicture[(c64iDrawTo+1) Mod 2]),ImageHeight(c64ipicture[(c64iDrawTo+1) Mod 2]),0,0,ImageBuffer(c64ipicture[(c64iDrawTo+1) Mod 2]),ImageBuffer(c64ipicture[c64iDrawTo])
  252.                 FreeImage c64ipicture[(c64iDrawTo+1) Mod 2]
  253.         EndIf
  254.        
  255.         SetBuffer ImageBuffer(c64ipicture[c64iDrawTo])
  256.         DrawBlock c64ic64image,(c64iNrX-1)*c64ispr_width,(c64iNrY-1)*c64ispr_height
  257.        
  258.         If (c64iNrY=c64iMaxNrY) And c64iNrX=c64iMaxNrX
  259.                 c64_finalize(1)
  260. ;               DebugLog "finalize 1"
  261.         ;set up to a new image
  262.         Else
  263.                 If final=0
  264.                         c64iDrawTo=(c64iDrawTo + 1) Mod 2
  265.                         c64iimgcount=c64iimgcount+1
  266.                 Else
  267.                         c64i
  268. ecursion=0
  269.                 EndIf
  270.         EndIf
  271.        
  272.         SetBuffer BackBuffer()
  273.         c64idrawn=0
  274.         c64iimgdrawcount=0                                              ;Reset C64ds + C64datspr function drawings
  275.         c64idy=0
  276.         c64idx=1
  277.         ClsColor $0,$0,$0
  278. End Function
  279.  
  280. Function c64_finalize(nr=0)
  281.        
  282.         If nr=0
  283.                 If (c64idy>0 Or c64iimgdrawcount>0)
  284.                         C64storeimg(1)
  285.                         If c64i
  286. ecursion>0
  287.                             c64i
  288. ecursion=0
  289.                                 Goto getout
  290.                         Else
  291.                                 c64_imageimage=CopyImage(c64ipicture[c64iDrawTo])
  292.                                 MaskImage c64_imageimage,c64pal(0,0),c64pal(0,1),c64pal(0,2)
  293.                                 c64icollection=c64icollection+1
  294.                                 FreeImage c64ipicture[c64iDrawTo]
  295.                                 c64iDrawTo=0
  296.                                 c64iNrX=0
  297.                                 c64iNrY=1
  298.                                 c64iimgdrawcount=0                                              ;Reset C64ds + C64datspr function drawings
  299.                                 c64idy=0
  300.                                 c64idx=1
  301.                                 c64iimgcount=0
  302.                         EndIf
  303.                 EndIf
  304.         Else
  305.                 c64_imageimage=CopyImage(c64ipicture[c64iDrawTo])
  306.                 MaskImage c64_imageimage,c64pal(0,0),c64pal(0,1),c64pal(0,2)
  307.                 c64icollection=c64icollection+1
  308.                 c64_image.c64sprmap = New c64sprmap
  309.                 FreeImage c64ipicture[c64iDrawTo]
  310.                 c64iDrawTo=0
  311.                 c64iNrX=0
  312.                 c64iNrY=1
  313.                 c64iimgdrawcount=0                                              ;Reset C64ds + C64datspr function drawings
  314.                 c64idy=0
  315.                 c64idx=1
  316.                 c64iimgcount=0
  317.                 c64i
  318. ecursion=c64i
  319. ecursion+1
  320.                 Return
  321.         EndIf
  322.        
  323.         .getout
  324.         For tmp.c64sprmap = Each c64sprmap   ; clear unused !
  325.                 If tmpimage=0
  326.                         Delete tmp
  327.                 EndIf
  328.         Next
  329.        
  330. End Function
  331.  
  332. Function BitState(a,b)
  333. ;a = variable
  334. ;b = bit number to Check
  335.         a=Mid(Bin$(a),Len(Bin$(a))-(b-1),1)
  336.         Select a
  337.                 Case "0"
  338.                         Return False
  339.                 Case "1"
  340.                         Return True
  341.         End Select
  342. End Function
  343.  
  344. ;To display the Template correctly, use Fixed width font, like "Courier New","Lucida Console" or "Terminal"
  345. ; this is 24x21 template
  346. ;X=     123456789012345678901234          v-- y
  347. ;c64ds("                        ",1);01                 ;Template ! use overwrite mode to draw inside ""
  348. ;C64DS("                        ")      ;02                     ;,1 Starts a new sprite drawing
  349. ;c64ds("                        ")      ;03                     ;if the whole line is transparent, you can use " " instead
  350. ;c64ds("                        ")      ;04
  351. ;c64ds("                        ")      ;05
  352. ;c64ds("                        ")      ;06
  353. ;c64ds("                        ")      ;07
  354. ;c64ds("                        ")      ;08
  355. ;c64ds("                        ")      ;09
  356. ;c64ds("                        ")      ;10
  357. ;c64ds("                        ")      ;11
  358. ;c64ds("                        ")      ;12
  359. ;c64ds("                        ")      ;13
  360. ;c64ds("                        ")      ;14
  361. ;c64ds("                        ")      ;15
  362. ;c64ds("                        ")      ;16
  363. ;c64ds("                        ")      ;17
  364. ;c64ds("                        ")      ;18
  365. ;c64ds("                        ")      ;19
  366. ;c64ds("                        ")      ;20
  367. ;c64ds("                        ")      ;21
  368.  
  369. ;Following are standard c64 Palette colors, you can change your own
  370. ;AddPal ($00,$00,$00) ; transparent     " "
  371. ;AddPal ($01,$00,$00) ; Black           0
  372. ;AddPal ($FF,$FF,$FF) ; White           1
  373. ;AddPal ($88,$20,$00) ; Red                     2
  374. ;AddPal ($68,$d0,$a8) ; Cyan            3
  375. ;AddPal ($a8,$38,$a0) ; Purple          4
  376. ;AddPal ($50,$b8,$18) ; Green           5
  377. ;AddPal ($18,$10,$90) ; Blue            6
  378. ;AddPal ($f0,$e8,$58) ; Yellow          7
  379. ;AddPal ($a0,$48,$00) ; Orange          8
  380. ;AddPal ($47,$2b,$1b) ; Brown           9
  381. ;AddPal ($c8,$78,$70) ; Light Red       a
  382. ;AddPal ($48,$48,$48) ; Dark Gray       b
  383. ;AddPal ($80,$80,$80) ; Medium Gray     c
  384. ;AddPal ($98,$ff,$98) ; Light Green     d
  385. ;AddPal ($50,$90,$d0) ; Light Blue      e
  386. ;AddPal ($b8,$b8,$b8) ; Light Gray      f


Comments :


Hotshot2005(Posted 1+ years ago)

 Brilliant :-)


Dan(Posted 1+ years ago)

 Update: Fixed a bug with Multicolored sprites.


Dan(Posted 1+ years ago)

 here is a small game, using the above functions.It is, aswell, a demostration on how to use ImageCollide to detect collision.you will need following additions to the userlib files:(theese are,mostly, used for the Screen() function.
Code: [Select]
; User32.decls
.lib "user32.dll"
api_GetSystemMetrics% (nIndex%) : "GetSystemMetrics"
api_GetActiveWindow%():"GetActiveWindow"
api_GetDC% (hwnd%) : "GetDC"
api_GetDesktopWindow% () : "GetDesktopWindow"
api_MoveWindow% (hwnd%, x%, y%, nWidth%, nHeight%, bRepaint%) : "MoveWindow"
;
; GDI32.decls
.lib "gdi32.dll"
api_GetDeviceCaps% (hdc%, nIndex%) : "GetDeviceCaps"
if you dont want to use Screen function, then you can remove/comment it out, and make following changes to the game:
Code: [Select]
If mode=0 ;F1
 ;Screen (320,240,1)
 Graphics 320,240,0,6
 Graphics 320,240,0,7
EndIf

If mode=1 Then Graphics 640,480,16,0 ;F2
If mode=2 Then Graphics 640,480,32,0 ;F3

If mode=3 ;F4
  ;Screen(320,240,2)
   Graphics 640,480,32,6
   Graphics 320,240,32,7
EndIf

If mode=4 ;F5
  ;Screen(320,240,-1)
   Graphics 1024,960,32,6
   Graphics 320,240,32,7
End If

SW=GraphicsWidth()
SH=GraphicsHeight()

If mode=5 Then ;F6
    ;SW=api_GetSystemMetrics(0)
;SH=api_GetSystemMetrics(1)
    ;bits=api_GetDeviceCaps(api_GetDC( api_GetDesktopWindow()),12)
SW=1680
SH=1050
bits=32
Graphics SW,SH,bits,0
EndIf
The Game is called Asteroids. The sprites are from the C64 version of this game. I cant recall where i have got this gameor who the programmer is (as i have changed the text on my copy, but do not have any other copies of it)It could be typed from some old Magazine, like Happy Computer, or C64er or others ...The sourcecode of this game is a bit strange as well, as it uses print statements with c64 codes through the whole program !There are no data statements, just 1 or 2 poke at the beginning.For blitzBasic source you'll need to copy the c64sprite_inc.bb to the folder, where you have saved this game, or change the full path from:
Code: [Select]
Include "c64sprite_inc.bb"
(or copy and paste the the functions,overwriting the include text, works, aswell)and here is the Sourcecode:
Code: [Select]
Graphics 640,480,0,6
Graphics 640,480,0,7

SetBuffer BackBuffer()
introtime=MilliSecs()
Global TxtY=-2

mode=4
intro_time=15000               ;15000milisecons = 15 sec
Repeat
Cls
Color $ff,$ff,$ff
TextY (-2,"             Asteroids !")
TextY (1,"Your spaceship is ready to harvest some nearby Asteroids.")
TextY (0,"The Yellow collored contains alot of Gold,")
TextY (0,"but they are rarer than the Silber Asteroids, which are indestructible.")
TextY (1,"Gameplay:")
TextY (0," Arrowkeys move the Ship left and right")
TextY (0," Strg Fires the laser.")
TextY (1,"Ingame Keys: F1/F2 changes the game version")
TextY (0,"   Version 2 - Gray Asteroids need 3 hits")
Color $ff,$50,$50
TextY (2,"Please choose a game resolution:")
Color $ff,$ff,$ff
TextY (1,"F1 - 320,240 windowed mode (original)")
TextY (0,"F2 - 640,480 16bit -Fullscreen - not scaled")
TextY (0,"F3 - 640,480 32bit -Fullscreen - not scaled")
TextY (0,"F4 - Windowed - double size ")
TextY (0,"F5 - Windowed - fullscreen")
TextY (0,"F6 - Fullscreen - Desktop Resolution")
TextY (0,"If the game is too slow, try switching between 16 and 32bits")
TextY (3,"Original Game is from C64")
TextY (0,"Source/Author: Unknown to me. From year 1990/1991")
TextY (0,"Using original sprites.")
TextY (5,"The Game is autostarting in :"+Floor((intro_time-(MilliSecs()-Introtime))*0.001)+" seconds")
        TextY (0,"Default Resolution F"+(mode+1))
If KeyDown(59) Then mode=0 : Exit
If KeyDown(60) Then mode=1 : Exit
If KeyDown(61) Then mode=2 : Exit
If KeyDown(62) Then mode=3 : Exit
If KeyDown(63) Then mode=4 : Exit
If KeyDown(64) Then mode=5 : Exit

Flip (1)
Until MilliSecs()-introtime=>intro_time

If mode=0 ;F1
 Screen (320,240,1)
 ;Graphics 320,240,0,6
 ;Graphics 320,240,0,7
EndIf

If mode=1 Then Graphics 640,480,16,0 ;F2
If mode=2 Then Graphics 640,480,32,0 ;F3

If mode=3 ;F4
  Screen(320,240,2)
  ;Graphics 640,480,32,6
  ;Graphics 320,240,32,7
EndIf

If mode=4 ;F5
  Screen(320,240,-1)
  ;Graphics 1024,960,32,6
  ;Graphics 320,240,32,7
End If

SW=GraphicsWidth()
SH=GraphicsHeight()

If mode=5 Then ;F6
    SW=api_GetSystemMetrics(0)
SH=api_GetSystemMetrics(1)
    bits=api_GetDeviceCaps(api_GetDC( api_GetDesktopWindow()),12)
;SW=1680
;SH=1050
;bits=32
Graphics SW,SH,bits,0
EndIf

SeedRnd MilliSecs()

Include "c64sprite_inc.bb"

Font=LoadFont("Courier",10,False,False,False)
SetFont font

Restore paldat
For x=1 To 17
Read r,g,b
addpal(r,g,b)
Next

.paldat
;Data $0,$0,$0, $00,$00,$01 ,$FF,$FF,$FF ,$68,$37,$2b ,$70,$a4,$b2 ,$6f,$3d,$86 ,$58,$8d,$43 ,$35,$28,$79 ,$b8,$c7,$6f ,$6f,$4f,$25 ,$43,$39,$00 ,$9a,$67,$59 ,$44,$44,$44 ,$6c,$6c,$6c ,$9a,$d2,$84 ,$6c,$5e,$b5 ,$95,$95,$95 ;16colors
Data $00,$00,$00, $01,$00,$00, $FF,$FF,$FF, $88,$20,$00, $68,$d0,$a8, $a8,$38,$a0, $50,$b8,$18, $18,$10,$90, $f0,$e8,$58, $a0,$48,$00, $47,$2b,$1b, $c8,$78,$70, $48,$48,$48, $80,$80,$80, $98,$ff,$98, $50,$90,$d0, $b8,$b8,$b8 ;16colors
Restore spr

For y=1 To 3 ;There are 3 sprites to read
If y=2 And yr=0 Then Restore spr
For x=1 To 63 ;Read 1 sprite data
Read dat
If y=1 Then c64datspr(dat,0,$c,$d,2) ;Asteroid - Gray
If y=2 Then c64datspr(dat,0,10,9,8) ;Asteroid - Yellow
If y=3 Then c64datspr(dat,0,$c,$d,2) ;Ship
Next
Next ;y

c64_finalize() ;No more sprites, its save them to a bitmap !

Global scratch%=CreateImage(SW,SH)                ; create a scratch image -for DrawSizeImage

Global Score=0,HiScore1=0,HiScore2=0
Global asteroidcount=6          ;Maximum asteroids on screen
Global SpawnDelay=450           ;Spawn a new Asteroid after this delay (it limits the ammount of active asteroids !)
Global YellowDelay=1000         ;wait this long, until yellow asteroid is spawned
Global gametyp=0
Global AsteroidTimer=MilliSecs()
Global asteroidactive=0
Global YellowTimer=MilliSecs()
Global Framer=CreateTimer (75)
Global version=1

Type ship
Field x#
Field y#
Field shot
Field bulletX#
Field bulletY#
End Type

Type asteroid
Field x#
Field y#
Field speed#
Field typ
Field hp
End Type

player.ship = New ship

;Set sprite data for ImagesCollide

spr_ship=CreateImage(24,21)
SetBuffer ImageBuffer(spr_ship)
c64drawsprite(2)

spr_asteroid=CreateImage(24,21)
SetBuffer ImageBuffer(spr_asteroid)
c64drawsprite(0)

spr_laser=CreateImage(2,10)
SetBuffer ImageBuffer(spr_laser)
Color $50,$Ff,$ff
Rect 0,0,2,10,1
Color $ff,$ff,$ff

Global img_backbuffer=CreateImage(320,240)

 GW=(GraphicsWidth()/2)-160
 GH=(GraphicsHeight()/2)-160

;Main Loop starts here
Repeat
SetBuffer ImageBuffer(img_backbuffer)
Gosub drawscreen ;Redraw the screen
Gosub spawnasteroid
    Gosub game

;Draw the Gamescreen to the Backbuffer
SetBuffer BackBuffer()
Cls

If mode=1 Or mode=2 Or mode=5 Or mode=6
   
 DrawSizeImage(img_backbuffer,0,0,SW,SH)
  Color $ff,$ff,$ff
  ;Rect GW-1,GH-1 ,321,241,0
Else
DrawImage img_backbuffer,0,0
EndIf

WaitTimer(Framer)
Flip (0)
Until KeyDown(1)

FreeTimer Framer
FreeImage scratch%
FreeImage img_backbuffer
FreeImage Spr_Ship
FreeImage Spr_
End

.game
If gametyp=0 Or gametyp=3 Or gametyp=4
If KeyDown(59) Then version=1
If KeyDown(60) Then version=2
EndIf

Select gametyp
Case 0 ;Press fire to start
   Text 20,100,"Press button to start"
                    Text 50,160,"F1/F2 - V1/V2"
   If (KeyDown(157) Or KeyDown(29))
gametyp=1
FlushKeys()
EndIf
Case 1 ;User has pressed fire, clear starfield
   If asteroidactive=0
gametyp=2
Gosub initialize
EndIf
Case 2 ;Starfield is clear, start the game
If KeyDown(205) And playerx<197 Then playerx=playerx+1
If KeyDown(203) And playerx>0  Then Playerx=playerx-1

If (KeyDown(157) Or KeyDown(29)) And playershot=0
playershot=1
playerulletx#=playerx#+10
playerullety#=playery#
EndIf
c64drawsprite(2,playerx#,playery#)
Case 3 ;Game Over
If KeyDown(157) Or KeyDown(29)
   ;Still holding the keys ?
Else
   gametyp=4
EndIf
Case 4
If (KeyDown(157) Or KeyDown(29))
gametyp=1
EndIf
End Select
Return

.spawnasteroid

If (MilliSecs()-AsteroidTimer>SpawnDelay) And gametyp<>1
    AsteroidTimer=MilliSecs()
If MilliSecs()-YellowTimer>YellowDelay ;delay for the yellow asteroid
If yellowactive=0
spawnyellow=1
EndIf
EndIf

If asteroidactive<asteroidcount ;Spawn Gray asteroids
asteroid.asteroid = New asteroid
asteroidx#=Rand(1,199)
asteroidy#=-21
asteroidhp=3
If spawnyellow=0
asteroid yp=0
Else
asteroid yp=1
yellowactive=1
spawnyellow=0
EndIf

asteroidactive=asteroidactive+1
asteroidspeed#=Rnd(0.8,2)
EndIf
EndIf

Return

.initialize

playerx=98
playery=215

Score=0
FlushKeys()
AsteroidTimer=MilliSecs()
YellowTimer=MilliSecs()
yellowactive=0
spawnyellow=0

Return

.drawscreen;is where all the action is !
Cls
C64color(2)
;Line 220,0,220,240
;Text 1,1,MilliSecs()-AsteroidTimer
;Text 1,14,asteroidactive
Color $3,$3,$3
Rect 220,0,100,240,1
Color $ff,$ff,$ff
Text 230,40,"Asteroids!"
If version=2
Text 250,55,"V 2.0"
Text 220,110,"HiScore:"+addzero(HiScore2,4)
Else
Text 250,55,"V 1.0"
Text 220,110,"HiScore:"+addzero(HiScore1,4)
EndIf

Text 222,90,"Score:"+addzero(Score,4)
Text 230,152,"Written by"
Text 230,174,"Dan / 2016"

If playershot=1 Then
playerullety#=playerullety#-2.5
DrawImage spr_laser,playerulletx#,playerullety#,0
If playerullety#=-15
playerulletx#=-10
playershot=0
EndIf
EndIf

For tmp.asteroid = Each asteroid
tmpy#=tmpy#+tmpspeed#
c64drawsprite (tmp yp,tmpx#,tmpy#)

If ImagesCollide(spr_ship,playerx#,playery#,0,spr_asteroid,tmpx#,tmpy#,0) And gametyp=2
;Player Asteroid collision
gametyp=3
EndIf

If ImagesCollide(spr_laser,playerulletx#,playerullety#,0,spr_asteroid,tmpx#,tmpy#,0)
;Collision detection - bullet + asteroid
playerulletx#=-20
playershot=0

If tmp yp=1
Delete tmp
If version=1
Score=Score+1
If Score>HiScore1 Then HiScore1=Score
Else
Score=Score+10
If Score>HiScore2 Then HiScore2=Score
EndIf
asteroidactive=asteroidactive-1
spawnyellow=0
yellowactive=0
YellowTimer=MilliSecs()
Goto ext
EndIf

If version=2
tmphp=tmphp-1
If tmphp<=0
Score=Score+4
asteroidactive=asteroidactive-1
Delete tmp
Goto ext
EndIf
EndIf
EndIf

If tmpy#>244
;If Asteroid is offscreen:
If tmp yp=1
spawnyellow=0
yellowactive=0
YellowTimer=MilliSecs()
EndIf
Delete tmp
asteroidactive=asteroidactive-1
EndIf
.ext
Next

If gametyp=3 Or gametyp=4
Text 55,100,"Game Over"
Text 20,130,"Press button to Start"
        Text 50,160,"F1/F2 - V1/V2"
EndIf

Return

.spr
;asteroid
Data $00,$FF,$00,$07,$F5,$40,$1F,$E9,$D0,$AF,$AE,$50,$3E,$EB,$94,$7E,$BA,$94,$BF,$99,$A4
Data $FA,$AA,$95,$EA,$BA,$65,$FB,$9A,$95,$FD,$AE,$A5,$EA,$B9,$95,$FA,$AA,$65,$FB,$99,$A5
Data $BD,$AA,$94,$7E,$E6,$54,$3A,$6A,$94,$2F,$A5,$50,$1E,$95,$50,$07,$D5,$40,$01,$55,$00
;ship
Data $00,$20,$00,$00,$70,$00,$00,$E4,$00,$00,$E4,$00,$01,$E4,$00,$02,$E5,$00,$03,$A9,$00
Data $03,$A9,$00,$07,$A9,$00,$1F,$A9,$40,$7E,$BA,$50,$BA,$C6,$94,$EB,$11,$A4,$ED,$20,$64
Data $EC,$74,$64,$EC,$20,$64,$EB,$11,$A4,$FA,$46,$94,$3E,$AA,$50,$05,$55,$40,$03,$4D,$00

Function addzero$(var, showcount)
;shorter code by bobysait
If showcount=1
Return Right$(var,1)
Else
Return String("-", Sgn(var)<0)+Replace(RSet(Abs(var), (showcount-(Sgn(var)<0)))," ","0");
EndIf
End Function

Function TextY (num,txt$)
;Num = How many columns to skip
;Replaces Print with Text
;Every Function call displays the text
;1 line under the old one, like calling
;multiple print statements after eachother.
;
; Set num to -2 to make the text go on top (like locate 0,0)
; Use the Global TxtY=-2 outside this function
;
;Global TxtY=-2       ;Make TxtY global variable for displaying help text
If num=-2
TxtY=0
Else
TxtY=TxtY+(FontHeight()*(num+1))
EndIf
Text 0,TxtY,txt$
End Function

Function DrawSizeImage(image,x%,y%,w%,h%)
;Global scratch%=CreateImage(SW,SH)                ; create a scratch image
     Local ih%=ImageHeight(image)
     Local iw%=ImageWidth(image)

     Local sw%=Abs(w)
     Local sh%=Abs(h)
     
     Local xr#=(Float(iw)/Float(sw))
     Local yr#=(Float(ih)/Float(sh))
     
     fromimg=ImageBuffer(image)
     toimg=ImageBuffer(scratch)
     
     Local vf=-1+((h>0)*2)
     
     Local fw=(w<0)*w
     Local fh=(h<0)*h
     
     If w>=0
          For ix=0 To sw
               CopyRect ix*xr,0,1,ih,ix,0,fromimg,toimg
          Next
     Else
          For ix=0 To sw
               CopyRect ix*xr,0,1,ih,sw-ix,0,fromimg,toimg
          Next
     EndIf

     For iy=0 To sh
          CopyRect 0,iy*yr,sw,1,x+fw,y+(iy*vf),toimg
     Next
End Function

Function Screen(x,y,full=0)
;fill <0 = Fulldesktop, borderless window
;full =0 = Fulldesktop
;full =1 = original x,y
;full >1-5 = x*full,y*full size
;full >5 = 0
DeskX=api_GetSystemMetrics(0)
DeskY=api_GetSystemMetrics(1)
If x>DeskX Then x=DeskX
    If x<64 Then x=64
If y>DeskY Then y=DeskY
    If y<64 Then y=64
    bits=api_GetDeviceCaps(api_GetDC( api_GetDesktopWindow()),12)
Graphics x,y,bits,6
Graphics x,y,bits,7
If full<=0 Or full>5
If full<0 Then api_SetWindowLong(api_GetActiveWindow(), -16, $10000000)
api_MoveWindow(api_GetActiveWindow(),0,0,DeskX,DeskY,True)
    EndIf
If full>1 And full<=5 Then api_MoveWindow(api_GetActiveWindow(),0,0,x*full,y*full,True)
End Function
p.s. f1/f2 while game start/over screen changes the version (typ) of the game [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal