├── Preview └── track1.png ├── README.md ├── LICENSE └── Track.java /Preview/track1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AdiChat/Highway/master/Preview/track1.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Highway 2 | A Java game to drive your truck against other 2 trucks throughout the highway and compete to stay on the top of the leaderboard

3 | 4 | Author : Aditya Chatterjee
5 | :e-mail: me : aditianhacker@gmail.com
6 | 7 | :movie_camera: : [here](https://www.youtube.com/watch?v=AQzlGbrLvDw)
8 | 9 | ![alt text](Preview/track1.png "I am leading the leaderboard but competitors are a tough match")

10 | Feel free to suggest improvements 11 | 12 | ## Extension 13 | 14 | [Endless](https://github.com/Gaboso/Highway) by [Gaboso](https://github.com/Gaboso) 15 | 16 | 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Aditya Chatterjee 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /Track.java: -------------------------------------------------------------------------------- 1 | /* 2 | The MIT License (MIT) 3 | 4 | Copyright (c) 2015 Aditya Chatterjee 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. 23 | */ 24 | 25 | import java.awt.Color; 26 | import static java.awt.Color.gray; 27 | import static java.awt.Color.yellow; 28 | import java.awt.Font; 29 | import java.awt.Graphics;import java.awt.Image; 30 | import java.awt.Rectangle;import java.awt.event.KeyAdapter; 31 | import java.awt.event.KeyEvent;import javax.swing.JDialog; 32 | import javax.swing.JFrame; 33 | import javax.swing.JOptionPane; 34 | public class Track extends JFrame implements Runnable 35 | { 36 | int x,y;int xDirection,yDirection,rectx,recty;int bx,by; 37 | Font font=new Font("ARIAL",Font.BOLD|Font.ITALIC,20);int t1=0,score=0;double pp=0.0;double pp1=0.0; 38 | Rectangle MyShip=new Rectangle(x,y,20,50); 39 | int controlx=1,controly=1;int yy=1; 40 | Rectangle boundary=new Rectangle(5,380,500,20); 41 | Rectangle road=new Rectangle(65,0,285,1000); 42 | Rectangle boundary2=new Rectangle(45,20,20,500); 43 | Rectangle boundary3=new Rectangle(350,20,20,500);int c=1; 44 | Rectangle c1=new Rectangle(100,380,20,30); 45 | Rectangle c2=new Rectangle(300,350,30,10); 46 | Rectangle illu11=new Rectangle(45,90,20,20);Rectangle illu21=new Rectangle(350,90,20,20); 47 | Rectangle illu12=new Rectangle(45,200,20,20);Rectangle illu22=new Rectangle(350,200,20,20); 48 | Rectangle illu13=new Rectangle(45,300,20,20);Rectangle illu23=new Rectangle(350,300,20,20);int sd=0,s1=-30,s2=0; 49 | int w=0; 50 | private Graphics dbg;private Image dbImage; 51 | public void move() 52 | { 53 | cv=2,cd=2; 54 | if(w%13==0)cv=5; 55 | else if(w%17==0)cv=2; 56 | if(w%11==0)cd=5; 57 | else if(w%17==0)cd=2; 58 | else if(w%100==0)cd=15; 59 | MyShip.x+=xDirection;MyShip.y+=yDirection;c1.y+=cd;c2.y+=cv; 60 | sd+=yDirection;s1+=cd;s2+=cv;w++; 61 | if(yDirection==-3) 62 | { 63 | illu11.y-=yDirection;illu12.y-=yDirection;illu13.y-=yDirection; 64 | illu21.y-=yDirection;illu22.y-=yDirection;illu23.y-=yDirection; 65 | } 66 | if(MyShip.x<=50) 67 | MyShip.x=50; 68 | if(MyShip.x>=350) 69 | MyShip.x=350; 70 | if(MyShip.y<=150) 71 | MyShip.y=150; 72 | if(MyShip.y>380) 73 | MyShip.y=380; 74 | if(c1.x<=50) 75 | c1.x=50; 76 | if(c1.x>=350) 77 | c1.x=350; 78 | if(c1.y<=140) 79 | c1.y=140; 80 | if(c1.y>380) 81 | c1.y=380; 82 | if(c2.x<=50) 83 | c2.x=50; 84 | if(c2.x>=350) 85 | c2.x=350; 86 | if(c2.y<=155) 87 | c2.y=155; 88 | if(c2.y>380) 89 | c2.y=380; 90 | if(illu11.y>=350) 91 | illu11.y=50; 92 | if(illu12.y>=350) 93 | illu12.y=50; 94 | if(illu13.y>=350) 95 | illu13.y=50; 96 | if(illu21.y>=350) 97 | illu21.y=50; 98 | if(illu22.y>=350) 99 | illu22.y=50; 100 | if(illu23.y>=350) 101 | illu23.y=50; 102 | } 103 | public void run() 104 | { 105 | try{ 106 | while(true) 107 | { 108 | move();Thread.sleep(20); 109 | } 110 | } 111 | catch(Exception e) 112 | { 113 | System.err.println(e.getMessage()); 114 | } 115 | } 116 | public void setXDirection(int xdir) 117 | { 118 | xDirection=xdir; 119 | } 120 | public void setYDirection(int ydir) 121 | { 122 | yDirection=ydir; 123 | } 124 | public class AL extends KeyAdapter 125 | { 126 | public void keyPressed(KeyEvent e) 127 | { 128 | int keyCode=e.getKeyCode(); 129 | if(keyCode==e.VK_LEFT) 130 | setXDirection(-3); 131 | if(keyCode==e.VK_RIGHT) 132 | setXDirection(+3); 133 | if(keyCode==e.VK_UP) 134 | setYDirection(-3); 135 | if(keyCode==e.VK_DOWN) 136 | setYDirection(+3); 137 | } 138 | public void keyReleased(KeyEvent e) 139 | { 140 | int keyCode=e.getKeyCode(); 141 | if(keyCode==e.VK_LEFT) 142 | setXDirection(0); 143 | if(keyCode==e.VK_RIGHT) 144 | setXDirection(0); 145 | if(keyCode==e.VK_UP) 146 | setYDirection(0); 147 | if(keyCode==e.VK_DOWN) 148 | setYDirection(0); 149 | } 150 | } 151 | public Track() 152 | { 153 | setBackground(Color.GREEN); 154 | addKeyListener(new AL()); 155 | setTitle("SPEED UP"); 156 | setSize(500,400); 157 | setResizable(false); 158 | setVisible(true); 159 | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 160 | MyShip.x=200;MyShip.y=350;yy=1; 161 | } 162 | public void paint(Graphics g) 163 | { 164 | dbImage=createImage(getWidth(),getHeight()); 165 | dbg=dbImage.getGraphics(); 166 | paintComponent(dbg); 167 | g.drawImage(dbImage,0,0,this); 168 | } 169 | public void paintComponent(Graphics g) 170 | { 171 | g.setColor(yellow); 172 | g.fillRect(boundary2.x,boundary2.y,boundary2.width,boundary2.height); 173 | g.fillRect(boundary3.x,boundary3.y,boundary3.width,boundary3.height); 174 | g.setColor(gray); 175 | g.fillRect(road.x, road.y, road.width, road.height); 176 | g.setColor(Color.white); 177 | g.fillRect(illu11.x,illu11.y,illu11.width,illu11.height); 178 | g.fillRect(illu11.x+170,illu11.y,5,25); 179 | g.fillRect(illu12.x,illu12.y,illu12.width,illu12.height); 180 | g.fillRect(illu12.x+170,illu12.y,5,25); 181 | g.fillRect(illu13.x,illu13.y,illu13.width,illu13.height); 182 | g.fillRect(illu13.x+170,illu13.y,5,25); 183 | g.fillRect(illu21.x,illu21.y,illu21.width,illu21.height); 184 | g.fillRect(illu22.x,illu22.y,illu22.width,illu22.height); 185 | g.fillRect(illu23.x,illu23.y,illu23.width,illu23.height); 186 | g.setColor(Color.red); 187 | g.fillRect(MyShip.x,MyShip.y,20,50); 188 | g.setColor(Color.black); 189 | g.fillRect(MyShip.x+20,MyShip.y+5,5,5); 190 | g.fillRect(MyShip.x-5,MyShip.y+5,5,5); 191 | g.fillRect(MyShip.x+20,MyShip.y+40,5,5); 192 | g.fillRect(MyShip.x-5,MyShip.y+40,5,5); 193 | g.setColor(Color.yellow); 194 | g.fillRect(c1.x,MyShip.y-s1-sd,20,50); 195 | g.setColor(Color.black); 196 | g.fillRect(c1.x+20,MyShip.y-s1-sd+5,5,5); 197 | g.fillRect(c1.x-5,MyShip.y-s1-sd+5,5,5); 198 | g.fillRect(c1.x+20,MyShip.y-s1-sd+40,5,5); 199 | g.fillRect(c1.x-5,MyShip.y-s1-sd+40,5,5); 200 | g.setColor(Color.orange); 201 | g.fillRect(c2.x,MyShip.y-s2-sd,20,50); 202 | g.setColor(Color.black); 203 | g.fillRect(c2.x+20,MyShip.y-s2-sd+5,5,5); 204 | g.fillRect(c2.x-5,MyShip.y-s2-sd+5,5,5); 205 | g.fillRect(c2.x+20,MyShip.y-s2-sd+40,5,5); 206 | g.fillRect(c2.x-5,MyShip.y-s2-sd+40,5,5); 207 | g.drawString("Car - Dist. travelled", 380, 170); 208 | String r1="Me "+(-sd); 209 | g.drawString(r1, 380, 190); 210 | r1="C1 "+(s1); 211 | g.drawString(r1, 380, 210); 212 | r1="C2 "+(s2); 213 | g.drawString(r1, 380, 230);r1="Rank"; 214 | g.drawString(r1, 380, 250); 215 | int m=(int)Math.max(s1,Math.max(s2,-sd));String p; 216 | if(s1-m==0)p="C1"; 217 | else if(s2-m==0)p="C2"; 218 | else p="Me"; 219 | r1="1."+p;g.drawString(r1, 380, 270); 220 | int m1=(int)Math.min(s1,Math.min(s2,-sd)); 221 | if(s1-m1==0)p="C1"; 222 | else if(s2-m1==0)p="C2"; 223 | else p="Me"; 224 | r1="3."+p;g.drawString(r1, 380, 310); 225 | m=s1+s2-sd-m-m1; 226 | if(s1-m==0)p="C1"; 227 | else if(s2-m==0)p="C2"; 228 | else p="Me"; 229 | r1="2."+p;g.drawString(r1, 380, 290); 230 | repaint(); 231 | } 232 | public static void main(String[] args) 233 | { 234 | String message="BE READY TO FLY YOUR TRUCK"; 235 | JOptionPane pane = new JOptionPane(message); 236 | JDialog dialog = pane.createDialog(new JFrame(), "Dilaog"); 237 | dialog.show(); 238 | Track su=new Track(); 239 | Thread t1=new Thread(su); 240 | t1.start(); 241 | } 242 | } 243 | --------------------------------------------------------------------------------