/* Michael Donohue Cosc 072 Mahe final project: horse race race around track with sounds, etc. */ import java.applet.*; import java.awt.*; import java.awt.event.*; public class horse extends Applet implements KeyListener { TextField directions; Canvas track; int r, b, rx, ry, bx, by; int prx, pry, pbx, pby; AudioClip off, red, blue, finr, finb; public void init() { r = 0; b = 0; rx = 330; prx = 310; ry = 70; pry = 70; bx = 330; pbx = 310; by = 110; pby = 110; off = getAudioClip( getDocumentBase(), "off.au" ); blue = getAudioClip( getDocumentBase(), "blue.au" ); red = getAudioClip( getDocumentBase(), "red.au" ); finb = getAudioClip( getDocumentBase(), "finb.au" ); finr = getAudioClip( getDocumentBase(), "finr.au" ); directions = new TextField( "Directions: Red Horse hit an Arrow Key repeatedly, Blue Horse hit a Letter Key else repeatedly!!" ); directions.setEditable( false ); setLayout( new BorderLayout() ); add( directions, BorderLayout.SOUTH ); addKeyListener( this ); requestFocus(); } public void paint( Graphics g ) { g.setColor( Color.black ); g.fillRoundRect( 50, 50, 600, 400, 100, 100 ); g.setColor( Color.green ); g.fillRoundRect( 150, 150, 400, 200, 50, 50 ); g.setColor( Color.white ); g.fillRect( 349, 50, 3, 100 ); } public void update( Graphics g ) { g.setColor( Color.black ); g.fillOval( prx, pry, 20, 20 ); g.fillOval( pbx, pby, 20, 20 ); g.setColor( Color.red ); g.fillOval( rx, ry, 20, 20 ); g.setColor( Color.blue ); g.fillOval( bx, by, 20, 20 ); g.setColor( Color.white ); g.fillRect( 349, 50, 3, 100 ); } public void keyReleased( KeyEvent e ) { if ( e.isActionKey() ) { r++; prx = rx; pry = ry; if ( r <= 12 ) { rx += 20; repaint(); } if ( r == 13 ) { rx += 40; ry += 60; repaint(); } if ( r > 13 && r <= 25 ) { ry += 20; repaint(); } if ( r == 26 ) { rx -= 60; ry += 40; repaint(); } if ( r > 26 && r <= 48 ) { rx -= 20; repaint(); } if ( r == 49 ) { rx -= 40; ry -= 60; repaint(); } if ( r > 49 && r <= 61 ) { ry -= 20; repaint(); } if ( r == 62 ) { rx += 60; ry -= 40; repaint(); } if ( r > 62 && r <= 73 ) { rx += 20; repaint(); } } if ( e.isActionKey() == false ) { b++; pbx = bx; pby = by; if ( b <= 12 ) { bx += 20; repaint(); } if ( b > 12 && b <= 25 ) { by += 20; repaint(); } if ( b > 25 && b <= 48 ) { bx -= 20; repaint(); } if ( b > 48 && b <= 61 ) { by -= 20; repaint(); } if ( b > 61 && b <= 73 ) { bx += 20; repaint(); } } if ( e.isActionKey() || e.isActionKey() == false ) { if ( b + r == 1 ) off.play(); if ( b == 35 && r < 35 ) blue.play(); if ( r == 35 && b < 35 ) red.play(); if ( r == 73 && b < 73 ) { finr.play(); directions.setText( "Red Wins!!" ); } if ( b == 73 && r < 73 ) { finb.play(); directions.setText( "Blue Wins!!" ); } } } public void keyPressed( KeyEvent e ) { } public void keyTyped( KeyEvent e ) { } }