Guest User

Untitled

a guest
May 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.79 KB | None | 0 0
  1. public class Gioca implements Runnable
  2. {
  3. private int vite;
  4. private int recuperi;
  5.  
  6. public Gioca()
  7. {
  8. vite=3;
  9. recuperi=0;
  10. }
  11.  
  12.  
  13. public void gioca()
  14. {
  15. Thread t=new Thread(new Gioco(vite));
  16.  
  17. try
  18. {
  19. t.start();
  20. t.join();
  21. }
  22. catch (Exception ex) {}
  23. System.out.println("Fine");
  24. }
  25.  
  26. @Override
  27. public void run()
  28. {
  29. gioca();
  30. }
  31. }
  32.  
  33.  
  34.  
  35.  
  36.  
  37. public class Gioco extends Canvas implements ActionListener, KeyListener, Runnable
  38. {
  39. private int direzione;
  40. private Timer timer;
  41. private JFrame f;
  42. private int vite;
  43. private int velocità;
  44. private int spazio;
  45. private Personaggio p;
  46. private int pos;
  47. private LinkedList<Ostacolo> o;
  48. private Random r;
  49. private int po;
  50.  
  51. private Image imm1=new ImageIcon(this.getClass().getResource("images/sfondo.jpg")).getImage();
  52. private Image imm2=new ImageIcon(this.getClass().getResource("images/cuore.png")).getImage();
  53.  
  54. public Gioco(int vite)
  55. {
  56. r=new Random();
  57.  
  58. try
  59. {
  60. File file=new File("images/punteggio.txt");
  61. Scanner scanner=new Scanner(file);
  62. spazio=scanner.nextInt();
  63. }
  64. catch (Exception e) {}
  65. direzione=3;
  66. this.vite=vite;
  67. o=new LinkedList();
  68. for(int i=0; i<20; i++)
  69. o.add(new Ostacolo(Math.abs(400*i)+1000));
  70. p=new Personaggio();
  71. this.velocità=2;
  72. timer=new Timer(10, this);
  73. f=new JFrame("Gioco");
  74. f.setSize(1000, 700);
  75. f.setResizable(false);
  76. f.setLocation(200,200);
  77. f.add(this);
  78. f.addKeyListener(this);
  79. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  80.  
  81. }
  82.  
  83. public void actionPerformed(ActionEvent ae)
  84. {
  85. if(direzione==2)
  86. {
  87. velocità-=2;
  88. if(velocità<2)
  89. velocità=2;
  90. }
  91.  
  92. if(direzione==1)
  93. p.setY(5);
  94.  
  95. if(direzione==0)
  96. p.setY(-5);
  97.  
  98. spazio+=velocità;
  99.  
  100. if(spazio%1000<10)
  101. velocità++;
  102.  
  103. pos=(pos+velocità)%4500;
  104. po=-pos;
  105.  
  106. for(int i=0; i<20; i++)
  107. {
  108. o.get(i).muovi(velocità);
  109. if(o.get(i).getX()<-100)
  110. {
  111. o.remove(i);
  112. o.add(new Ostacolo(i*400));
  113. }
  114. }
  115.  
  116. verificaCollisioni();
  117. repaint();
  118.  
  119. }
  120.  
  121. public void verificaCollisioni()
  122. {
  123. for(int i=0; i<20; i++)
  124. {
  125. if(o.get(i).getX()>300 && o.get(i).getX()<350)
  126. {
  127. int r[]=o.get(i).getDimensioni();
  128. if(r[0]<p.getY() && r[1]>p.getY())
  129. {
  130.  
  131. }
  132. else
  133. fine();
  134. }
  135. }
  136. }
  137.  
  138. private void fine()
  139. {
  140. try
  141. {
  142. Thread.sleep(3000);
  143. }
  144. catch(Exception e){}
  145.  
  146. timer.stop();
  147.  
  148. try
  149. {
  150. File file=new File("images/punteggio.txt");
  151. file.createNewFile();
  152. FileOutputStream f=new FileOutputStream(file);
  153. f.flush();
  154. String sPunteggio=String.valueOf(spazio);
  155. byte[] scrivi=sPunteggio.getBytes();
  156. f.write(scrivi);
  157. }
  158. catch(Exception e){}
  159.  
  160. f.dispose();
  161.  
  162. }
  163.  
  164. @Override
  165. public void keyPressed(KeyEvent ke)
  166. {
  167. int c=ke.getKeyCode();
  168.  
  169. if(c == 40)
  170. direzione=1;
  171. if(c == 38)
  172. direzione=0;
  173.  
  174. if(c==32)
  175. direzione=2;
  176. }
  177.  
  178. public void paint(Graphics g)
  179. {
  180. Image workspace=createImage(getWidth(),getHeight());
  181. Graphics2D buffer=(Graphics2D) workspace.getGraphics();
  182.  
  183. buffer.drawImage(imm1, po, 0, this);
  184.  
  185. buffer.setColor(new Color(242, 54, 33));
  186. buffer.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));
  187. buffer.drawString(""+(spazio/100), 10, 20);
  188.  
  189.  
  190. buffer.drawImage(imm2, 940, 4, this);
  191. buffer.setColor(new Color(13, 226, 13));
  192. buffer.drawString(""+vite, 920, 20);
  193.  
  194. buffer.drawImage(p.getImage(), 300, p.getY(), this);
  195.  
  196. for(int i=0; i<20; i++)
  197. {
  198. Ostacolo tmp=o.get(i);
  199. buffer.drawImage(tmp.getImage(), tmp.getX(),tmp.getY(), this);
  200. }
  201.  
  202. Graphics2D g2=(Graphics2D)g;
  203. g2.drawImage(workspace, 0, 0, this);
  204. buffer.dispose();
  205. }
  206.  
  207. public void update(Graphics g)
  208. {
  209. paint(g);
  210. }
  211.  
  212. public void keyReleased(KeyEvent ke) {direzione=3;}
  213. public void keyTyped(KeyEvent ke) {}
  214.  
  215. @Override
  216. public void run()
  217. {
  218. f.setVisible(true);
  219. timer.start();
  220.  
  221. }
  222.  
  223. }
  224.  
  225. public class Campana implements Runnable{
  226. private String suono;
  227. private int volte;
  228. public Campana(String suono,int volte)
  229. {
  230. this.suono =suono;
  231. this.volte=volte;
  232. }
  233. public void run()
  234. {
  235. for(int i=0;i<volte;i++) {
  236. System.out.println((i+1)+" "+suono);
  237.  
  238. }
  239. }
  240. }
  241.  
  242. public class Suona {
  243.  
  244. public static void main(String args[]){
  245.  
  246. Thread campana1=new Thread(new Campana("din", 5));
  247. Thread campana2=new Thread(new Campana("don", 5));
  248. Thread campana3=new Thread(new Campana("dan", 5));
  249. try {
  250. campana1.start();
  251. campana1.join();
  252. campana2.start();
  253. campana2.join();
  254. campana3.start();
  255. campana3.join();
  256.  
  257.  
  258.  
  259.  
  260. } catch (InterruptedException ex) {
  261. Logger.getLogger(Suona.class.getName()).log(Level.SEVERE, null, ex);
  262. }
  263.  
  264.  
  265. }
  266.  
  267. }
  268.  
  269. f.setVisible(true);
  270. timer.start();
Add Comment
Please, Sign In to add comment