[comp.sources.x] v08i066: xinvaders -- space invaders for X, Part05/05

jgoldman@parc.xerox.com (Jonny Goldman) (08/04/90)

Submitted-by: Jonny Goldman <jgoldman@parc.xerox.com>
Posting-number: Volume 8, Issue 66
Archive-name: xinvaders/part05

XXtIntervalId id;
X{
X  int i, x, y, newx, newy;
X  Shot vshot;
X
X  if (closure != (Opaque) MoveVshots) return;
X  if (!paused) {
X    if (numvshots > 0)
X      vshottimerid = XtAddTimeOut(vshotwait, MoveVshots, (Opaque) MoveVshots);
X    else
X      vshottimerid = NULL;
X    for (i=0 ; i<numvshots ; i++) {
X      vshot = vshots + i;
X      newy = vshot->y + 2;
X      x = vshot->x;
X      y = vshot->y;
X      if (y>gameheight ||
X	  VshotHitsShot(x, y) ||
X	  ShotHitsBase(x,y) ||
X	  ShotHitsBuilding(x,y)) {
X	DestroyVshot(i);
X	i--;			/* Ensures we don't skip moving a shot. */
X      } else {
X	PaintVshot(vshot, backgc);
X	tick = tick ? 0 : 1;
X	vshot->y = newy;
X	PaintVshot(vshot, vshotgc);
X	tick = tick ? 0 : 1;
X      }
X    }
X    tick = tick ? 0 : 1;
X  }
X}
X
X
Xvoid AddShot(x, y)
Xint x, y;
X{
X    Shot shot;
X    if (numshots >= maxshots) return;
X    shot = shots + numshots;
X    numshots++;
X    shot->x = x;
X    shot->y = y-SHOTSIZE;
X    PaintShot(shot, shotgc);
X    if (shottimerid == NULL)
X        shottimerid = XtAddTimeOut(shotwait, MoveShots, (Opaque) MoveShots);
X}
X
Xvoid AddVshot(x, y)
Xint x, y;
X{
X    Shot shot;
X    if (numvshots >= maxvshots) return;
X    shot = vshots + numvshots;
X    numvshots++;
X    shot->x = x;
X    shot->y = y;
X    PaintVshot(shot, vshotgc);
X    if (vshottimerid == NULL)
X      vshottimerid = XtAddTimeOut(shotwait, MoveVshots, (Opaque) MoveVshots);
X}
X
X
X
Xint ReadVshotImages()
X{
X  unsigned int width, height;
X  int x_hot, y_hot;
X  char *data, filename[30];
X  int i, status;
X
X  for (i = 0; i < 2; i++) {
X    sprintf(filename, "sperm%s.bit", (i ? "b" : "a"));
X    status = XmuReadBitmapDataFromFile (filename,
X					&width, &height, &data,
X					&x_hot, &y_hot);
X
X    if (status != BitmapSuccess) return status;
X
X    vshot_image[i] = XCreateImage(dpy,
X				  DefaultVisual(dpy, DefaultScreen(dpy)),
X				  1,
X				  XYBitmap,
X				  0,
X				  data,
X				  width, height,
X				  8, 0);
X    vshot_image[i]->bitmap_bit_order = LSBFirst;
X    vshot_image[i]->byte_order = LSBFirst;
X  }
X  return BitmapSuccess;
X}
X
XInitShot()
X{
X    shottimerid = NULL;
X    numshots = 0;
X    vshottimerid = NULL;
X    numvshots = 0;
X    if( ReadVshotImages() != BitmapSuccess) {
X      fprintf(stderr, "Error reading vshot images.\n");
X      exit(20);
X    }
X}
X
Xvoid AddLine(fromx, fromy, tox, toy, gc)
Xint fromx, fromy, tox, toy;
XGC gc;
X{
X    XDrawLine(dpy, gamewindow, gc, fromx, fromy, tox, toy);
X}
END_OF_FILE
if test 4984 -ne `wc -c <'shot.c'`; then
    echo shar: \"'shot.c'\" unpacked with wrong size!
fi
# end of 'shot.c'
fi
if test -f 'score.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'score.c'\"
else
echo shar: Extracting \"'score.c'\" \(1418 characters\)
sed "s/^X//" >'score.c' <<'END_OF_FILE'
X/* 
XCopyright notice:
X
XThis is mine.  I'm only letting you use it.  Period.  Feel free to rip off
Xany of the code you see fit, but have the courtesy to give me credit.
XOtherwise great hairy beasties will rip your eyes out and eat your flesh
Xwhen you least expect it.
X
XJonny Goldman <jgoldman@parc.xerox.com>
X
XTue Jul 17 1990
X*/
X
X/* score.c -- Print the score. */
X
X#include "vaders.h"
X
XPaintScore()
X{
X  char scorestring[7];
X  XDrawString(dpy, gamewindow, backgc, gamewidth, 10, "Score", 5);
X  sprintf(scorestring, "%6d", lastscore);
X  XDrawString(dpy, gamewindow, backgc, gamewidth, 20, scorestring, 6);
X  lastscore = score;
X  sprintf(scorestring, "%6d", lastscore);
X  XDrawString(dpy, gamewindow, scoregc, gamewidth, 10, "Score", 5);
X  XDrawString(dpy, gamewindow, scoregc, gamewidth, 20, scorestring, 6);
X  if (nextbonus && score >= nextbonus) {
X    basesleft++;
X    ShowBase(basesleft-1, basegc);
X    bases = basesleft;
X    nextbonus = 0;
X  }
X  sprintf(scorestring, "%6d", hiscore);
X  XDrawString(dpy, gamewindow, backgc, gamewidth, 30, " High", 5);
X  XDrawString(dpy, gamewindow, backgc, gamewidth, 40, scorestring, 6);
X  if (score > hiscore) hiscore = score;
X  sprintf(scorestring, "%6d", hiscore);
X  XDrawString(dpy, gamewindow, scoregc, gamewidth, 30, " High", 5);
X  XDrawString(dpy, gamewindow, scoregc, gamewidth, 40, scorestring, 6);
X}
X
XInitScore()
X{
X    score = 0;
X    basesleft = 3;
X    nextbonus = 1500;
X}
END_OF_FILE
if test 1418 -ne `wc -c <'score.c'`; then
    echo shar: \"'score.c'\" unpacked with wrong size!
fi
# end of 'score.c'
fi
if test -f 'xinvaders.ma' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'xinvaders.ma'\"
else
echo shar: Extracting \"'xinvaders.ma'\" \(3921 characters\)
sed "s/^X//" >'xinvaders.ma' <<'END_OF_FILE'
X.\" Man page for xinvaders, by Jonny Goldman.
X.TH XInvaders 1 "Jul 17 1990"
X.SH NAME
X\fIXInvaders\fR \- Shoot-em-up them nasty little bugs.
X.SH SYNOPSIS
X.B xinvaders
X[-all_those_groovy_xtoolkit_switches]
X.SH DESCRIPTION
X.I xinvaders
Xis an implementation of the old Atari Space Invaders game, on top of the
XX11 window system.
X
X.SH PLAYING XINVADERS
XThe game will start with all the invaders drawn.  Press 'p' to play, and
Xyou're off.
X
XThe controls are:
X
X.PP
X.TP 8
X.B z
XFire a shot.  You will only be able to fire one shot at a time, so be careful.
X.PP
X.TP 8
X.B ,
XMove left.  Pressing this key will start you moving to the left, releasing it
Xwill make you stop.
X.PP
X.TP 8
X.B .
XMove right.  Pressing this key will start you moving to the right, releasing it
Xwill make you stop.
X.PP
X.TP 8
X.B p
XPause or Play.  Pressing this will either pause the game, or play it.
XRemember, you must press it to start the game (either at the very
Xbeginning, or between new games).
X.PP
X.TP 8
X.B q
XQuit the game.  Do this and you will end the game.  No ifs, ands or buts
Xabout it.
X
X.PP
XThe mouse controls are active, and rather obvious:
X
X.PP
X.TP 8
X.B Left
XMove left, as above
X.PP
X.TP 8
X.B Middle
XFire, as above
X.PP
X.TP 8
X.B Right
XMove right.  ditto.
X
X
X.SH SCORING
X
XThe lowest monsters are worth 10 points, the next are worth 20, and the
Xhighest are worth 30.  Space ships are worth anything from 50-300 points
X(in 50 point increments, of course).
X
X
X
X.SH CUSTOMIZING COLORS
X
XAs with all standard X applications,
X.I xinvaders
Xmay be customized through entries in the resource manager.   This game also
Xworks on monochrome screens, but the colors don't mean anything (surprise!).
X.PP
X.TP 8
X.B BaseColor
XThe color for the Base (you).  Default is white.
X.PP
X.TP 8
X.B  BuildingColor
XThe color for the buildings that shield you.  Default is yellow.
X.PP
X.TP 8
X.B Vader1Color
XThe color for the highest invaders.  Default is blue.
X.PP
X.TP 8
X.B Vader2Color
XThe color for the middle invaders.  Default is orange.
X.PP
X.TP 8
X.B Vader3Color
XThe color for the lowest invaders.  Default is red.
X.PP
X.TP 8
X.B SpacerColor
XThe color for the Space Ship.  Default is gray.
X.PP
X.TP 8
X.B ShotColor
XThe color for your shots.  Default is green.
X.PP
X.TP 8
X.B VshotColor
XThe color for their shots.  Default is pink.
X.PP
X.TP 8
X.B ScoreColor
XThe color for the score.  Default is cyan.
X
X.PP
XHere's an example of some of the resources you can set (this would be in
Xyour .Xdefaults file):
X
X Vaders*defaultfore:            black
X Vaders*defaultback:            white
X Vaders*Vader1Color:            blue
X Vaders*Vader2Color:            green
X Vaders*Vader3Color:            red
X Vaders*BaseColor:              cyan
X Vaders*BuildingColor:          yellow
X Vaders*SpacerColor:            gray
X Vaders*ShotColor:              lavender
X Vaders*VshotColor:             orange
X Vaders*BaseWait:               10
X Vaders*VaderWait:              300
X Vaders*SpacerWait:             50
X Vaders*ShotWait:               10
X Vaders*VshotWait:              30
X
X
X
X.SH NOTES
X
XThere are lots of resources.  You can change all the timings involved, and
Xeven the shapes of the objects in the game (they are loaded at run-time
Xfrom the .bit files).  Have fun with it.
X
XAlthough you can resize the gamewindow, I don't recommend it.
X
X
X.SH BUGS
X
XThe buildings aren't as good as the arcade, but hey, whatdayawant for free?
XYou also must be in the same directory as the executable for the images to
Xload properly.  It also doesn't handle auto-repeat very well, so you should
Xprobably turn it off.
X
X.SH COPYRIGHT
X
XThis is mine.  I'm only letting you use it.  Period.  Feel free to rip off
Xany of the code you see fit, but have the courtesy to give me credit.
XOtherwise great hairy beasties will rip your eyes out and eat your flesh
Xwhen you least expect it.
X
X.SH CREDITS
X
XI'd like to give a lot of credit to Terry Weissman for his roids games.  It
Xgave me the guidance on how to do something like this.
END_OF_FILE
if test 3921 -ne `wc -c <'xinvaders.ma'`; then
    echo shar: \"'xinvaders.ma'\" unpacked with wrong size!
fi
# end of 'xinvaders.ma'
fi
if test -f 'Imakefile' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'Imakefile'\"
else
echo shar: Extracting \"'Imakefile'\" \(255 characters\)
sed "s/^X//" >'Imakefile' <<'END_OF_FILE'
XLOCAL_LIBRARIES = $(XAWLIB) $(XTOOLLIB) $(XMULIB) $(XLIB)
X
XOBJS = spacers.o base.o widget.o main.o vaders.o shot.o score.o
X
XSRCS = spacers.c base.c widget.c main.c vaders.c shot.c score.c
X
XINCLUDES = -I$(TOP) -I$(TOP)/X11
X
XComplexProgramTarget(xinvaders)
END_OF_FILE
if test 255 -ne `wc -c <'Imakefile'`; then
    echo shar: \"'Imakefile'\" unpacked with wrong size!
fi
# end of 'Imakefile'
fi
if test -f 'Makefile' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'Makefile'\"
else
echo shar: Extracting \"'Makefile'\" \(698 characters\)
sed "s/^X//" >'Makefile' <<'END_OF_FILE'
XCFLAGS		= -g -I/import/X11R4/include -L/import/X11R4/lib
X
XCC		= cc
X
XLIBS = -lXaw -lXt -lXmu -lX11
X
XBITS = 	base.bit	sperma.bit	vader1b.bit	vader3a.bit \
X	explode.bit	spermb.bit	vader2a.bit	vader3b.bit \
X	spacer.bit	vader1a.bit	vader2b.bit
X
XINCS = patchlevel.h vaders.h
X
XOBJS = spacers.o base.o widget.o main.o vaders.o shot.o score.o
X
XSRCS = spacers.c base.c widget.c main.c vaders.c shot.c score.c
X
XMAN = xinvaders.ma
X
XALL = README $(BITS) $(INCS) $(SRCS) $(MAN) Imakefile Makefile
X
XDISTRIB = xinvaders.sh
X
XSPLIT  = xinvaders.split.
X
Xxinvaders:	$(OBJS)
X	$(CC) -o xinvaders $(OBJS) $(LIBS)
X
Xclean:
X	rm xinvaders *.o *~
X
Xdistrib:: ${DISTRIB}
X
X${DISTRIB}: $(ALL)
X	shar $(ALL) | split -500 - ${SPLIT}
END_OF_FILE
if test 698 -ne `wc -c <'Makefile'`; then
    echo shar: \"'Makefile'\" unpacked with wrong size!
fi
# end of 'Makefile'
fi
echo shar: End of shell archive.
exit 0

dan
----------------------------------------------------
O'Reilly && Associates   argv@sun.com / argv@ora.com
Opinions expressed reflect those of the author only.