Advertisement
Guest User

Untitled

a guest
Dec 13th, 2014
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.19 KB | None | 0 0
  1. import greenfoot.*;
  2.  
  3. import java.awt.*;
  4. import javax.swing.*;
  5. import java.awt.image.*;
  6.  
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import java.util.Collections;
  10.  
  11.  
  12. public class RaY extends World
  13. {
  14.     int[] obbX = {900,900,900,900,900,900,900,900,900,900,900,900,900,900,900,900,900,900,900,900,900,900,900,900,900,900,900,900,900,900};
  15.     double[] obbY = {0,10,20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200,210,220,230,240,250,260,270,280,290};
  16.     int x,y,color=0,score=0,bestScore=0,health=3,offY,alpha=128,accel;
  17.     double slant;
  18.     double speed = .7,offX=0,count=-1;
  19.     boolean scored=false;
  20.     String me;
  21.     GreenfootImage screen = new GreenfootImage(500,300);
  22.    
  23.     List<UserInfo> highscores;
  24.     List<UserInfo> nearbyscores;
  25.     final static int scoresToDisplay = 10;
  26.     Color flash;
  27.    
  28.     boolean scoreScreen = false;
  29.  
  30.     public RaY() {
  31.         super(500,300, 1);
  32.         for(int f=8;f<20;f++){
  33.             obbX[f] = Greenfoot.getRandomNumber(80)-10;
  34.         }
  35.         screen = getBackground();
  36.     }
  37.    
  38.     public void act()
  39.     {
  40.         screen.setColor(new Color(color,0,0,alpha));
  41.         screen.fillRect(0,0,500,300);
  42.        
  43.         //change color
  44.         flashColors();
  45.  
  46.         if(checkDisplayScores()){
  47.             return;
  48.         }
  49.  
  50.         drawLines();
  51.        
  52.         screen.setColor(flash);
  53.         screen.drawString(" "+score,0,12);  
  54.  
  55.         doEnemyActions();
  56.        
  57.         doPlayerActions();
  58.     }    
  59.    
  60.     public void flashColors(){
  61.         //increase our counter as speed increases
  62.         count = count+speed;
  63.         if(count>15){   //if we have counted high enough
  64.             flash = new Color(Greenfoot.getRandomNumber(200)+50,Greenfoot.getRandomNumber(200)+50,Greenfoot.getRandomNumber(200)+50);
  65.             if(scored)flash = new Color(Greenfoot.getRandomNumber(100),Greenfoot.getRandomNumber(100),Greenfoot.getRandomNumber(100));
  66.             count=0;
  67.         }        
  68.     }
  69.  
  70.     public boolean checkDisplayScores(){
  71.         if(health<=0  &&  !scored){
  72.             UserInfo myInfo = UserInfo.getMyInfo();
  73.             if(myInfo != null){
  74.                 bestScore = myInfo.getScore();
  75.                 if(score > bestScore){
  76.                     myInfo.setScore(score);
  77.                 }
  78.                 for(int i=0; i<UserInfo.NUM_INTS; i++){
  79.                     myInfo.setInt(i, 0xAABBCCDD);
  80.                 }
  81.                 for(int i=0; i<UserInfo.NUM_STRINGS; i++){
  82.                     myInfo.setString(i, "TESTSTRING");
  83.                 }
  84.                 myInfo.store();
  85.             }
  86.            
  87.             scored=true;
  88.             highscores = UserInfo.getTop(10);
  89.             nearbyscores = UserInfo.getNearby(10);
  90.         }
  91.        
  92.         if(scored){
  93.             screen.setColor(flash);
  94.             screen.drawString("Final Score:  " + score, 5, 15);
  95.             screen.drawString("Previous Best: " + bestScore, 5, 30);
  96.             screen.drawString("Top Scores", 120, 60);
  97.  
  98.             for(int i=0; i<highscores.size(); i++){
  99.                 UserInfo u = highscores.get(i);
  100.                 screen.drawString(u.getRank() + ". " + u.getUserName() + ": " + u.getScore(), 100, i*20 + 80);
  101.                
  102.                 if(nearbyscores != null){
  103.                     screen.drawString("Similar Scores", 320, 60);
  104.                     u = nearbyscores.get(i);
  105.                     screen.drawString(u.getRank() + ". " + u.getUserName() + ": " + u.getScore(), 300, i*20 + 80);
  106.                 }
  107.             }
  108.             return true;
  109.         }
  110.         return false;
  111.     }
  112.    
  113.     public void drawLines(){
  114.         //draw top lines
  115.         screen.setColor(new Color(70,70,70));
  116.         x = 250+(int)(270*Math.atan2(40-offX,0));
  117.         y = (int)(5000/Math.sqrt((40-offX)*(40-offX)));
  118.         screen.drawLine(250,150,x,150-(int)(y/2)+(int)(.002*(250-x)*slant));
  119.         x = 250+(int)(270*Math.atan2(-40-offX,0));
  120.         y = (int)(5000/Math.sqrt((-40-offX)*(-40-offX)));
  121.         screen.drawLine(250,150,x,150-(int)(y/2)+(int)(.002*(250-x)*slant));
  122.        
  123.         //draw lines on the field
  124.         for(int f=-40;f<=40;f=f+5){
  125.             x = 250+(int)(270*Math.atan2(f-offX,0));
  126.             y = (int)(5000/Math.sqrt((f-offX)*(f-offX)));
  127.             screen.drawLine(250,150,x,150+(int)(y/2)+(int)(.002*(250-x)*slant));
  128.         }
  129.     }
  130.    
  131.     public void doEnemyActions(){
  132.         for(double k=0; k<1; k+=0.05){
  133.             for(int f=0;f<30;f++){
  134.                 x = 250+(int)(270*Math.atan2(obbX[f]-offX,obbY[f]));
  135.                 y = (int)(400/Math.sqrt((obbX[f]-offX)*(obbX[f]-offX)  + obbY[f]*obbY[f]));
  136.                 offY = 150+(int)(270*Math.atan2(4,obbY[f]));
  137.                
  138.                 screen.drawOval(x-(int)(y/2),offY-(int)(y/2)+(int)(.002*(250-(x-(int)(y/2)))*slant),y,y);
  139.                 obbY[f] = obbY[f] - speed*0.05;
  140.                 if(obbY[f]<9 && obbY[f]>6 && Math.abs(obbX[f] - offX)<1){   //we're hit!
  141.                     color = 254;
  142.                     obbY[f] = 290;
  143.                     obbX[f] = Greenfoot.getRandomNumber(80)-40;  
  144.                     health--;
  145.                 }
  146.                 if(obbY[f]<5){
  147.                     obbY[f] = 290;
  148.                     obbX[f] = Greenfoot.getRandomNumber(80)-40;      
  149.                 }
  150.             }        
  151.         }
  152.     }
  153.    
  154.     public void doPlayerActions(){
  155.         if(Greenfoot.isKeyDown("left"))
  156.             accel = accel - 5;
  157.         if(Greenfoot.isKeyDown("right"))
  158.             accel = accel+5;
  159.         slant += (accel-slant)/5.0;
  160.         if(offX+accel/100.0 > 39 || offX+accel/100.0<-39){
  161.             accel = -accel;
  162.         }
  163.         offX = offX + accel/100.0;
  164.         if(accel>0){
  165.             accel = accel - 1;
  166.         }
  167.         if(accel<0){
  168.             accel = accel + 1;
  169.         }
  170.         speed = speed+.0005;
  171.         if(color>0){
  172.             color = color - 2;
  173.         }
  174.         score++;    //increase the score
  175.        
  176.         //draw the player
  177.         screen.drawRect(240,290,20,10);
  178.        
  179.         if(Greenfoot.isKeyDown("t") && alpha!=0)
  180.             alpha--;
  181.         if(Greenfoot.isKeyDown("y") && alpha!=255)
  182.             alpha++;        
  183.     }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement