📄 gravity.java
字号:
hilightNearestLine(); repaint(); } // In edit mode, add a new Friend (enemy) to the world, sitting // on the line that is closest to the cursor. void addFriend() { if (lastLine == -1) return; planet.addFriend( lastLine ); repaint(); } // If the cursor moves to or past the edge of the screen, // automatically slide the virtual screen around to keep the cursor // towards the middle. public void autoslide() { Point sp = screenCoords( new Point( (int)x, (int)y) ); int tx = sp.x - size().width/4; if (tx>0) { tx = sp.x - (size().width*3)/4; if (tx<0) tx = 0; } int ttx=tx; int ty = sp.y - size().height/4; int tty=ty; if (ty>0) { ty = sp.y - (size().height*3)/4; if (ty<0) ty = 0; } translate( -tx, -ty ); } // Check to see if things have hit each other. public void intersections() { boolean wereHit = intersectBullets(); if (shield) { wereHit = false; playEffect( theyFireClip ); } //wereHit = false; if (!exploded && (ship.intersectFigure( planet ) || wereHit)) { playEffect( iSplodeClip ); exploded = true; explodedTime = System.currentTimeMillis(); vx = vy = 0; turnLeft = turnRight = engine = false; } if (exploded && (System.currentTimeMillis()-explodedTime)>3000) { exploded = false; x = startx; y = starty; shipang = (Math.PI*3)/2; vx = vy = 0; } } // move the ship, move the bullets, remove really old // bullets, draw stuff, run the AI for the enemies, // check for hits, and automatically slide the virtual // screen. In short, do everything. public void frame( double delta ) { //System.out.println( "Delta: " + delta ); rotation( delta ); move( delta ); moveBullets( delta ); ageBullets( delta ); repaint(); planet.fireBulletsMaybe( delta*buls_per_sec, x, y ); intersections(); autoslide(); } // Rotate the ship. public void rotation( double delta ) { if (exploded) return; if (turnLeft) shipang -= delta * rot_vel; else if (turnRight) shipang += delta * rot_vel; } // Move the ship forward. public void move( double delta ) { if (exploded) return; if (engine) { vx += delta * engineAccel * Math.cos( shipang ); vy += delta * engineAccel * Math.sin( shipang ); } vy += delta * gravityAccel; x += delta * vx; y += delta * vy; } // Stop the ship hard. public void stopp() { vx = vy = 0; } // Fire a new bullet into the world, at (x,y), moving in // direction (vx,vy). void addBullet( double x, double y, double vx, double vy ) { if (n_buls >= max_buls) return; bulx[n_buls] = bullx[n_buls] = x; buly[n_buls] = bully[n_buls] = y; bulvx[n_buls] = vx; bulvy[n_buls] = vy; bulage[n_buls] = 0; n_buls++; } // for interface TakesABullet. public void take( double x, double y, double vx, double vy ) { addBullet( x, y, vx, vy ); } // The player fires a bullet. void addShipBullet() { if (exploded) return; double s = Math.sin( shipang ); double c = Math.cos( shipang ); addBullet( x+c*((Ship.size*4)/3), y+s*((Ship.size*4)/3), vx+bul_vel*c, vy+bul_vel*s ); } // Remove a bullet from the world. void removeBullet( int i ) { n_buls--; bulx[i] = bulx[n_buls]; buly[i] = buly[n_buls]; bulvx[i] = bulvx[n_buls]; bulvy[i] = bulvy[n_buls]; bullx[i] = bullx[n_buls]; bully[i] = bully[n_buls]; bulage[i] = bulage[n_buls]; } // Note the inevitable aging of the bullets. void ageBullets( double delta ) { for (int i=0; i<n_buls; ++i) bulage[i] += delta; for (int i=0; i<n_buls; ++i) { if (bulage[i] > max_bul_age) removeBullet( i ); } } // move each bullet ahead. void moveBullets( double delta ) { for (int i=0; i<n_buls; ++i) { bullx[i] = bulx[i]; bully[i] = buly[i]; bulx[i] += delta * bulvx[i]; buly[i] += delta * bulvy[i]; } } // Draw the bullets to a Graphics void renderBullets( Graphics g ) { for (int i=0; i<n_buls; ++i) { //g.drawRect( (int)bulx[i], (int)buly[i], 1, 1 ); g.drawLine( (int)bulx[i], (int)buly[i], (int)bullx[i], (int)bully[i] ); } } // Check for intersection between any bullet and the player. // Returns true if player is hit. boolean intersectBullets() { boolean wereHit = false; int bline[][] = new int[2][2]; for (int i=0; i<n_buls; ++i) { bline[0][0] = (int)bulx[i]; bline[0][1] = (int)buly[i]; bline[1][0] = (int)bullx[i]; bline[1][1] = (int)bully[i]; if (planet.intersectLines( bline )) removeBullet( i ); else planet.intersectFriends( bline ); if (ship.intersectLines( bline )) wereHit = true; } return wereHit; } // Turn edit mode on or off. // When on, turn the game heartbeat off (i.e. pause). void editmodeOnOff() { editmode = !editmode; if (editmode) heartbeat.suspend(); else heartbeat.resume(); repaint(); } // Load the world from the URL 'world'. public void loadWorldFromNet() { try { planet.read( new URL( getDocumentBase(), "world" ) ); } catch( MalformedURLException e ) { System.out.println( e ); } } public boolean keyDown( Event evt, int key ) { switch( key ) { case 'z': turnLeft = true; break; case 'x': turnRight = true; break; case '.': { engine = true; playEffect( thrustClip ); break; } case ',': { addShipBullet(); playEffect( iFireClip ); break; } case ' ': shield = true; break; case 'e': editmodeOnOff(); break; case '\t': stopp(); break; case 'd': if (editmode) removeNearestLine(); break; case 'a': if (editmode) addFriend(); break; case 'S': if (editmode) planet.write( "world" ); repaint(); break; case 'L': if (editmode) planet.read( "world" ); repaint(); break; case 'l': if (editmode) loadWorldFromNet(); repaint(); break; } return true; } public boolean keyUp( Event evt, int key ) { switch( key ) { case 'z': turnLeft = false; break; case 'x': turnRight = false; break; case '.': engine = false; break; case ' ': shield = false; break; } return true; } public boolean mouseUp(Event e, int x, int y) { if (playing) { stopSoundtrack(soundtrackClip); } else { playSoundtrack(soundtrackClip); } return true; } public boolean mouseDown( Event evt, int x, int y ) { if (drawing) { drawing = false; planet.addLine( currentline[0][0], currentline[0][1], currentline[1][0], currentline[1][1] ); } else { drawing = true; currentline[1][0] = currentline[0][0] = x; currentline[1][1] = currentline[0][1] = y; } repaint(); return true; } public boolean mouseDrag( Event evt, int x, int y ) { if (editmode) { mx = x; my = y; editStuff(); } if (drawing) { currentline[1][0] = x; currentline[1][1] = y; repaint(); } return true; } public boolean mouseMove( Event evt, int x, int y ) { if (editmode) { mx = x; my = y; editStuff(); } if (drawing) { currentline[1][0] = x; currentline[1][1] = y; repaint(); } return true; } public void start() { playSoundtrack(soundtrackClip); } public void stop() { stopSoundtrack(soundtrackClip); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -