Beispielprogramme in C/Joystick Abfrage

Aus C64-Wiki
Zur Navigation springenZur Suche springen

C-Version des Beispielprogramms im Artikel Joystick

/*
  Compiler: cc65
*/

#include <c64.h>
#include <conio.h>
#include <string.h>

const unsigned char sprite_data[] =
{
  240, 0, 0,
  224, 0, 0,
  144, 0, 0,
  8, 0, 0,
  4, 0, 0,
  2, 0, 0,
  1, 0, 0,
  0, 0, 0,
  0, 0, 0,
  0, 0, 0,
  0, 0, 0,
  0, 0, 0,
  0, 0, 0,
  0, 0, 0,
  0, 0, 0,
  0, 0, 0,
  0, 0, 0,
  0, 0, 0,
  0, 0, 0,
  0, 0, 0,
  0, 0, 0
};

enum Joystick
{ 
  oben = 1,
  unten = 2,
  links = 4,
  rechts = 8,
  feuer = 16
};

void Sprite_anlegen(void)
{
  memcpy((unsigned char *)(13 * 64), sprite_data, 63);
  *(unsigned char*) 2040 = 13;
  VIC.spr_ena = 1;
  VIC.spr0_color = 7;
  VIC.bgcolor0 = 0;
  VIC.spr_exp_x = 0;
}

void main(void)
{

  unsigned char x, y, speed = 2;

  x = y = 150;
  CIA1.ddra = 224;

  Sprite_anlegen();
  
  clrscr();
  textcolor(COLOR_YELLOW);

  while ((CIA1.pra & feuer) != 0)
  {
    if ((CIA1.pra & oben) == 0) y -= speed;
    if ((CIA1.pra & unten) == 0) y += speed;
    if ((CIA1.pra & links) == 0) x -= speed;
    if ((CIA1.pra & rechts) == 0) x += speed;

    if (x > 251) x = 24;
    if (x < 24) x = 250;

    if (y < 50) y = 250;
    if (y > 250) y = 50;

    gotoxy(0, 0);
    cprintf("X-Pos: %3d Y-Pos: %3d", x, y);
    VIC.spr0_x = x;
    VIC.spr0_y = y;
  }
  
  CIA1.ddra = 255;
}