resources_for_tetris
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| resources_for_tetris [2022/05/09 11:17] – created frchris | resources_for_tetris [2022/05/23 16:43] (current) – frchris | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| **Tetris resources** | **Tetris resources** | ||
| + | |||
| The basic falling item, with a Key Listener: | The basic falling item, with a Key Listener: | ||
| + | <code java> | ||
| + | import java.awt.Color; | ||
| + | import java.awt.Font; | ||
| + | import java.awt.Graphics; | ||
| + | import java.awt.event.ActionEvent; | ||
| + | import java.awt.event.ActionListener; | ||
| + | import java.awt.event.KeyEvent; | ||
| + | import java.awt.event.KeyListener; | ||
| + | import javax.swing.JFrame; | ||
| + | import javax.swing.JPanel; | ||
| + | import javax.swing.Timer; | ||
| + | |||
| + | public class Tetris extends JPanel implements KeyListener, | ||
| + | { | ||
| + | public static int WIDTH=800; | ||
| + | public static int HEIGHT=600; | ||
| + | private Font titleFont, regularFont; | ||
| + | private int x,y; | ||
| + | private Timer timer1, timer2; | ||
| + | |||
| + | |||
| + | public Tetris() | ||
| + | { | ||
| + | |||
| + | // | ||
| + | titleFont = new Font(" | ||
| + | regularFont = new Font(" | ||
| + | y=0; | ||
| + | x=200; | ||
| + | timer1 = new Timer(50, this); //1000=1 seconds | ||
| + | timer2 = new Timer(2000, this); //1000=1 seconds | ||
| + | timer1.start(); | ||
| + | |||
| + | } | ||
| + | public static void main(String[] args) { | ||
| + | Tetris app= new Tetris(); | ||
| + | JFrame window = new JFrame(" | ||
| + | window.setSize(WIDTH, | ||
| + | window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
| + | window.getContentPane().add(app); | ||
| + | window.addKeyListener(app); | ||
| + | // | ||
| + | window.setVisible(true); | ||
| + | |||
| + | } | ||
| + | public void paintComponent(Graphics g){ | ||
| + | super.paintComponent(g); | ||
| + | g.setColor(Color.WHITE); | ||
| + | g.fillRect(0, | ||
| + | g.setColor(Color.BLUE); | ||
| + | g.setFont(titleFont); | ||
| + | g.drawString(" | ||
| + | g.setColor(Color.BLACK); | ||
| + | g.setFont(regularFont); | ||
| + | g.drawString(" | ||
| + | g.fillRect(x, | ||
| + | |||
| + | } | ||
| + | |||
| + | |||
| + | // These 3 methods need to be declares to implement the KeyListener Interface | ||
| + | @Override | ||
| + | public void keyTyped(KeyEvent e) {} | ||
| + | |||
| + | @Override | ||
| + | public void keyPressed(KeyEvent e) { | ||
| + | int code=e.getKeyCode(); | ||
| + | | ||
| + | if (code ==39) // right | ||
| + | x=(x+1)%HEIGHT; | ||
| + | else if (code == 37 && x>0) //left | ||
| + | x--; | ||
| + | repaint(); | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public void keyReleased(KeyEvent e) {} | ||
| + | @Override | ||
| + | public void actionPerformed(ActionEvent e) { | ||
| + | |||
| + | if (e.getSource()==timer1){ | ||
| + | y=(y+1)%WIDTH; | ||
| + | } | ||
| + | | ||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| + | You may wish to make a Shape class which has an ArrayList< | ||
| + | |||
| + | |||
| + | < | ||
| + | |||
| + | import java.awt.Color; | ||
| + | import java.awt.Graphics; | ||
| + | import java.awt.Rectangle; | ||
| + | import java.util.ArrayList; | ||
| + | public class Shape | ||
| + | { | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | { | ||
| + | this.x=x; | ||
| + | this.y=y; | ||
| + | int n = xPoints.length; | ||
| + | color = Color.RED; | ||
| + | boxes = new ArrayList< | ||
| + | } | ||
| + | | ||
| + | { | ||
| + | |||
| + | for (Rectangle r:boxes) { | ||
| + | g.setColor(color); | ||
| + | g.fillRect(r.x, | ||
| + | g.setColor(Color.BLACK); | ||
| + | g.drawRect(r.x, | ||
| + | |||
| + | } | ||
| + | |||
| + | |||
| + | } | ||
| + | } | ||
| + | |||
| + | </ | ||
resources_for_tetris.1652109442.txt.gz · Last modified: 2022/05/09 11:17 by frchris