Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cs218087.CSRun;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- public class Keying extends JPanel{
- private static final long serialVersionUID = 1L;
- public Rectangle character;
- public Rectangle bottomBox;
- public int charW = 24;
- public int charH = 36;
- public int f3counter = 1;
- public boolean right = false;
- public boolean left = false;
- public boolean jumping = false;
- public boolean obstacleEndOfScreen = false;
- public int obstacleX = 565;
- public int levelCounter = 0;
- public int obstacleSpeed = 1;
- public boolean f3menu = false;
- public long jumpingTime = 90;
- public Point mouse;
- public Keying(Display f, Images i){
- character = new Rectangle(180, 180, charW, charH);
- bottomBox = new Rectangle(0, 350, 9000, 30);
- f.addKeyListener(new KeyAdapter(){
- public void keyPressed(KeyEvent e){
- if(e.getKeyCode() == KeyEvent.VK_D){
- right = true;
- }
- if(e.getKeyCode() == KeyEvent.VK_A){
- left = true;
- }
- if(e.getKeyCode() == KeyEvent.VK_M){
- }
- if(e.getKeyCode() == KeyEvent.VK_SPACE){
- jumping = true;
- new Thread(new thread()).start();
- }
- if(e.getKeyCode() == KeyEvent.VK_F3){
- f3counter++;
- if(f3counter % 2 == 0){
- f3menu = true;
- }else{
- f3menu = false;
- }
- }
- }
- public void keyReleased(KeyEvent e){
- if(e.getKeyCode() == KeyEvent.VK_D){
- right = false;
- }
- if(e.getKeyCode() == KeyEvent.VK_A){
- left = false;
- }
- }
- });
- f.addMouseListener(new MouseAdapter() {
- public void mouseClicked(MouseEvent e){
- mouse = new Point(e.getX(), e.getY() - 25);
- if(e.getButton() == MouseEvent.BUTTON1){
- character.x = mouse.x;
- character.y = mouse.y;
- }
- }
- });
- }
- public void paintComponent(Graphics g){
- if(Main.f.i.imagesLoaded == true){
- super.paintComponent(g);
- g.drawImage(Main.f.i.bg, 0, 0, 600, 400, null);
- Point pt1 = new Point(character.x, character.y + character.height);
- if(!bottomBox.contains(pt1) && !jumping){
- character.y++;
- }
- g.setColor(Color.WHITE);
- g.fillRect(character.x, character.y, character.width, character.height);
- g.fillRect(bottomBox.x, bottomBox.y, bottomBox.width, bottomBox.height);
- g.setColor(Color.PINK);
- g.fillRect(obstacleX, bottomBox.y - 60, 20, 60);
- if(right == true && character.x != Main.w - charW){
- character.x += 1;
- }
- if(left == true && character.x != 0){
- character.x -= 1;
- }
- if(jumping == true){
- character.y = (int) (character.y - 0.1);
- }
- if(f3menu == true){
- g.drawString("CSRun 1.0", 30, 40);
- g.drawString("Made by Cs218087", 30, 52);
- g.drawString("Level: " + levelCounter/10, 30, 64);
- }
- obstacleX = (int) (obstacleX - obstacleSpeed);
- if(obstacleX <= 0){
- obstacleX = 600;
- levelCounter++;
- }
- repaint();
- }
- }
- public class thread implements Runnable{
- @Override
- public void run() {
- try{
- Thread.sleep(jumpingTime);
- jumping = false;
- }catch (Exception e){
- e.printStackTrace();
- new Thread(this).start();
- System.exit(0);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement