Guest User

Untitled

a guest
Jan 18th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.17 KB | None | 0 0
  1. package tuto;
  2.  
  3. import java.io.FileNotFoundException;
  4.  
  5. import org.newdawn.slick.*;
  6.  
  7. public class Main extends BasicGame {
  8.    
  9.     float SPEED = 0.4f;
  10.     float GRAVITY = 0.5f;
  11.     int a = 0;
  12.     int stepGame = 0;
  13.     Carte carte0 = new Carte();
  14.     Perso square = new Perso(0,0,50,50);
  15.    
  16.     public Main(String title) {
  17.         super("Mario");
  18.         // TODO Auto-generated constructor stub
  19.     }
  20.    
  21.     public static void main(String[] args) throws SlickException{
  22.         AppGameContainer app = new AppGameContainer(new Main("Game"));
  23.         app.setDisplayMode(800, 600, false);
  24.         app.getInput();
  25.         app.start();
  26.        
  27.        
  28.     }
  29.  
  30.     @Override
  31.     public void render(GameContainer gc, Graphics g) throws SlickException {
  32.        
  33.                
  34.                 if (carte0.map[0][11]!=1){
  35.                
  36.                 try {
  37.                     carte0.readMap();
  38.                 } catch (FileNotFoundException e) {
  39.                     // TODO Auto-generated catch block
  40.                     e.printStackTrace();
  41.                 }
  42.             }
  43.             int tempa,tempb;
  44.             for (tempa=0;tempa<16;tempa++){
  45.                 for (tempb=0;tempb<12;tempb++){
  46.                     if (carte0.map[tempa][tempb]==1){
  47.                         g.setColor(new Color(255, 255, 255));  
  48.                         g.fillRect(tempa*50, tempb*50, 50, 50);
  49.                     }
  50.                     if (carte0.map[tempa][tempb]==0){
  51.                         g.setColor(new Color(128, 128, 128));  
  52.                         g.fillRect(tempa*50, tempb*50, 50, 50);
  53.                     }
  54.                    
  55.                 }
  56.         }
  57.            
  58.  
  59.             g.setColor(new Color(121, 147, 176));      
  60.             g.fillRect(square.x, square.y, square.width, square.height);
  61.             // TODO Auto-generated method stub
  62.             g.setColor(new Color(200, 200, 200));
  63.             g.drawString("y="+square.y, 500, 0);
  64.             g.drawString("x="+square.x, 500, 50);
  65.             g.drawString("a="+a, 500, 100);
  66.         }
  67.        
  68.  
  69.     @Override
  70.     public void init(GameContainer arg0) throws SlickException {
  71.         // TODO Auto-generated method stub
  72.        
  73.     }
  74.  
  75.     @Override
  76.     public void update(GameContainer gc, int arg1) throws SlickException {
  77.  
  78.             Input input = gc.getInput();       
  79.             if(input.isKeyDown(Input.KEY_D)){
  80.                 if (collide()==false){
  81.                     square.x = square.x+SPEED; 
  82.                 }
  83.                 if (collide()==true){
  84.                     square.x = square.x-SPEED; 
  85.                 }
  86.                
  87.             }
  88.             if(input.isKeyDown(Input.KEY_Q)){
  89.                 if (collide()==false){
  90.                     square.x = square.x-SPEED; 
  91.                 }
  92.                 if (collide()==true){
  93.                     square.x = square.x+SPEED; 
  94.                 }
  95.                
  96.             }
  97.             if(square.y+square.height<600){
  98.                 if (collide()==false){
  99.                     square.y = square.y+GRAVITY;
  100.                 }
  101.                 if (collide()==true){
  102.                     square.y = square.y-GRAVITY;
  103.                 }
  104.                
  105.             }
  106.        
  107.        
  108.             if(input.isKeyDown(Input.KEY_Z) && a==0 ){
  109.                 a = 1;
  110.             }
  111.        
  112.             if (a == 400){a=0;}
  113.        
  114.             if (a != 0 && input.isKeyDown(Input.KEY_Z)){
  115.                 square.y = square.y - GRAVITY*2;
  116.                 a++;
  117.             }
  118.            
  119.        
  120.            
  121.            
  122.            
  123.            
  124.         }
  125.    
  126.    
  127.    
  128.    
  129.     public boolean collide(){
  130.        
  131.         int tempa,tempb;
  132.         for (tempa=0;tempa<16;tempa++){
  133.             for (tempb=0;tempb<12;tempb++){
  134.                 if (carte0.map[tempa][tempb]==1){
  135.                     int tempE = 1;
  136.                     int xCUBE = tempa*50;
  137.                     int yCUBE = tempb*50;
  138.                     if((xCUBE >= square.x + square.width)      // trop à droite
  139.                             || (xCUBE + 50 <= square.x) // trop à gauche
  140.                             || (yCUBE >= square.y + square.height) // trop en bas
  141.                             || (yCUBE + 50 <= square.y)){
  142.                         return false;
  143.                     }
  144.                
  145.                      
  146.                      
  147.                 }
  148.                
  149.             }
  150.         }
  151.        
  152.         return true;   
  153.     }
  154.    
  155.  
  156. }
Add Comment
Please, Sign In to add comment