Welcome to Dylan's Programming Homework Website! Pokemon code in chart below!
import java.util.Scanner;
import java.util.Random;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.Color;
public class SideScroller extends JPanel {
Player p;
Shape ground;
int timeInterval = 10;
int timePerSec = Math.round(1000 / timeInterval);
public static void main(String[] args){
SideScroller side = new SideScroller();
side.setLayout(new BorderLayout());
side.setBackground(Color.BLUE);
JFrame window = new JFrame("Window");
window.setContentPane(side);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(640, 480);
window.setVisible(true);
side.setFocusable(true);
side.requestFocusInWindow();
if(side.hasFocus() == true){
System.out.println("Has focus");
}
else{
System.out.println("Didn't get focus");
}
}
public SideScroller(){
p = new Player(100, 300);
ground = new Rect(0, 400, 640, 10, Color.RED);
addKeyListener(new keyListener());
addMouseListener(new mouseListener());
Timer timer = new Timer();
timer.schedule(new Update(), 0, timeInterval);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.fillRect(p.getX(), p.getY(), 10, 20);
g.fillRect(((Rect)ground).getX(), ((Rect)ground).getY(), ((Rect)ground).getWidth(), ((Rect)ground).getHeight());
}
class Update extends TimerTask{
int time;
public void run(){
if(p.isJumping() == true){
time++;
p.calculateNewPosY(time, 7.0, timePerSec);
repaint();
}
else{
time = 0;
}
p.getIsTouching(ground);
}
}
class keyListener implements KeyListener{
public void keyPressed(KeyEvent e){
System.out.println("Key Pressed");
int key = e.getKeyCode();
if(key == KeyEvent.VK_SPACE){
if(p.isJumping() == false){
p.jump();
System.out.println("Jumped");
}
}
}
public void keyReleased(KeyEvent e){
}
public void keyTyped(KeyEvent e){
}
}
class mouseListener implements MouseListener{
public void mouseClicked(MouseEvent e){
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public void mousePressed(MouseEvent e){
}
public void mouseReleased(MouseEvent e){
}
}
}
class Player extends Object{
double velocity;
boolean isJumping;
Shape shape;
int orgX;
int orgY;
double scale = 30;
public Player(int x, int y){
this.x = x;
this.y = y;
orgX = x;
orgY = y;
isJumping = false;
shape = (Rect) new Rect(x, y, 10, 20, Color.RED);
}
public void calculateNewPosY(double time, double initialVelocity, double tPS){
double g = -4.9 * scale;
double t = time / tPS;
double vI = initialVelocity * scale;
double newY = g * (t*t) + vI * (t);
if(t == 0){
orgY = this.y;
}
/*
int y1 = y;
int y2 = y - (int)Math.round(newY);
if(y2 - y1 > 100){
if(g * t + vI < 0){
y += 100;
}
else{
y -= 100;
}
}
else{
this.y = (int)(orgY - Math.round(newY));
}
*/
this.y = (int)(orgY - Math.floor(newY));
System.out.println(y + " " + t);
}
public void calculateNewPosX(double time, double initialVelocity, double tPS){
}
public void jump(){
setIsJumping(true);
}
public boolean isJumping(){
return isJumping;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public double getVelocity(){
return velocity;
}
public Shape getShape(){
return shape;
}
public double getIsTouching(Shape s){
if(s.getType().equals("Rect")){
for(int i = 0; i < 4; i++){
double oSlope;
double pSlope;
Rect o = (Rect)s;
Rect p = (Rect)getShape();
if(o.getPointX(i + 1) - o.getPointX(i) != 0){
oSlope = (o.getPointY(i + 1) - o.getPointY(i)) / (o.getPointX(i + 1) - o.getPointX(i));
}
else{
oSlope = .000000000000000000001;
}
if(p.getPointX(i + 1) - p.getPointX(i) != 0){
pSlope = (p.getPointY(i + 1) - p.getPointY(i)) / (p.getPointX(i + 1) - p.getPointX(i));
}
else{
pSlope = .000000000000000000001;
}
if(oSlope != pSlope){
int oY = o.getPointY(i);
int oX = o.getPointX(i);
int oB = (int)Math.round(oY / (oX * oSlope));
int pB = (int)Math.round(y / (x * pSlope));
int connectPoint = (int)Math.round((oB - pB) / (pSlope - oSlope));
System.out.println(connectPoint);
if(connectPoint > this.getX() && connectPoint < this.getX() + p.getWidth()){
}
}
}
}
return 1;
}
public void setX(int x){
this.x = x;
}
public void setY(int y){
this.y = y;
}
public void setVelocity(double velocity){
this.velocity = velocity;
}
public void setIsJumping(boolean isJumping){
this.isJumping = isJumping;
}
}
class Shape{
String type;
int x;
int y;
Color color;
public Shape(){
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public Color getColor(){
return color;
}
public String getType(){
return type;
}
public void setX(int x){
this.x = x;
}
public void setY(int y){
this.y = y;
}
public void setColor(Color color){
this.color = color;
}
}
class Rect extends Shape{
int width;
int height;
int[] pointX;
int[] pointY;
public Rect(int x, int y, int width, int height, Color color){
this.x = x;
this.y = y;
this.color = color;
this.type = "Rect";
this.width = width;
this.height = height;
pointX = new int[4];
pointY = new int[4];
for(int i = 0; i < 2; i++){
pointX[i] = i * this.width + x;
pointY[i] = y;
}
for(int i = 0; i < 2; i++){
pointX[i] = Math.abs(i - 1) * this.width + x;
pointY[i] = this.height + y;
}
}
public int getWidth(){
return width;
}
public int getHeight(){
return height;
}
public int getPointX(int pointNum){
if(pointNum == 4){
return pointX[0];
}
else{
return pointX[pointNum];
}
}
public int getPointY(int pointNum){
if(pointNum == 4){
return pointY[0];
}
else{
return pointY[pointNum];
}
}
}
class Cirlce extends Shape{
int radius;
public Cirlce(int x, int y, int radius, Color color){
this.x = x;
this.y = y;
this.color = color;
this.type = "Circle";
}
}