-
Notifications
You must be signed in to change notification settings - Fork 322
edited readme #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,99 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
package space; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import javax.swing.*; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import java.awt.*; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import java.awt.event.ActionEvent; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import java.awt.event.ActionListener; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import java.awt.event.KeyAdapter; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import java.awt.event.KeyEvent; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import java.util.ArrayList; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import java.util.Random; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public class FunnyCatchGame extends JPanel implements ActionListener { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
private Timer timer; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
private ArrayList<Item> items; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
private int playerX = 250; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
private final int playerWidth = 70; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
private final int playerHeight = 20; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
private int score = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public FunnyCatchGame() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
timer = new Timer(20, this); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
items = new ArrayList<>(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
setFocusable(true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
addKeyListener(new KeyAdapter() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@Override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public void keyPressed(KeyEvent e) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (e.getKeyCode() == KeyEvent.VK_LEFT) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
playerX -= 10; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
playerX += 10; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
playerX = Math.max(0, Math.min(playerX, 500 - playerWidth)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
timer.start(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
spawnItems(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+20
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Improve game constants and movement handling
+ private static final int REFRESH_RATE_MS = 20;
+ private static final int PLAYER_SPEED = 10;
+
public FunnyCatchGame() {
- timer = new Timer(20, this);
+ timer = new Timer(REFRESH_RATE_MS, this);
items = new ArrayList<>();
setFocusable(true);
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
- playerX -= 10;
+ playerX -= PLAYER_SPEED;
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
- playerX += 10;
+ playerX += PLAYER_SPEED;
}
- playerX = Math.max(0, Math.min(playerX, 500 - playerWidth));
+ playerX = Math.max(0, Math.min(playerX, WINDOW_WIDTH - PLAYER_WIDTH));
}
});
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
private void spawnItems() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Timer itemTimer = new Timer(1000, e -> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Random rand = new Random(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
int x = rand.nextInt(850); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
String itemType = rand.nextBoolean() ? "🦆" : "🌮"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
items.add(new Item(x, 0, itemType)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
itemTimer.start(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+39
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix spawn mechanics and add resource management
+ private static final int SPAWN_INTERVAL_MS = 1000;
+ private Timer itemTimer;
+
private void spawnItems() {
- Timer itemTimer = new Timer(1000, e -> {
+ itemTimer = new Timer(SPAWN_INTERVAL_MS, e -> {
Random rand = new Random();
- int x = rand.nextInt(850);
+ int x = rand.nextInt(WINDOW_WIDTH - 20); // Subtract emoji width
String itemType = rand.nextBoolean() ? "🦆" : "🌮";
items.add(new Item(x, 0, itemType));
});
itemTimer.start();
}
+
+ public void stopGame() {
+ timer.stop();
+ itemTimer.stop();
+ }
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@Override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
protected void paintComponent(Graphics g) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
super.paintComponent(g); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
g.setColor(Color.GREEN); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
g.fillRect(playerX, 450, playerWidth, playerHeight); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
g.setColor(Color.RED); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for (Item item : items) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
g.drawString(item.type, item.x, item.y); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
g.setFont(new Font("Arial", Font.BOLD, 20)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
g.drawString("Score: " + score, 10, 20); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@Override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public void actionPerformed(ActionEvent e) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for (int i = 0; i < items.size(); i++) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Item item = items.get(i); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
item.y += 5; // Fall speed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (item.y > 500) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
items.remove(i); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
i--; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} else if (item.y >= 450 && item.x >= playerX && item.x <= playerX + playerWidth) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Catching the item | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
score += item.type.equals("🦆") ? 1 : 2; // Duck = 1 point, Taco = 2 points | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
items.remove(i); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
i--; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
repaint(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+62
to
+78
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Improve game loop and collision detection safety
+ private static final int FALL_SPEED = 5;
+
@Override
public void actionPerformed(ActionEvent e) {
- for (int i = 0; i < items.size(); i++) {
- Item item = items.get(i);
- item.y += 5; // Fall speed
- if (item.y > 500) {
- items.remove(i);
- i--;
- } else if (item.y >= 450 && item.x >= playerX && item.x <= playerX + playerWidth) {
+ items.removeIf(item -> {
+ item.y += FALL_SPEED;
+ if (item.y > WINDOW_HEIGHT) {
+ return true;
+ }
+ if (item.y >= WINDOW_HEIGHT - PLAYER_HEIGHT &&
+ item.x >= playerX &&
+ item.x <= playerX + PLAYER_WIDTH) {
// Catching the item
- score += item.type.equals("🦆") ? 1 : 2; // Duck = 1 point, Taco = 2 points
- items.remove(i);
- i--;
+ score += item.type.equals("🦆") ? 1 : 2;
+ return true;
}
- }
+ return false;
+ });
repaint();
}
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public static void main(String[] args) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
JFrame frame = new JFrame("Funny Catch Game"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
FunnyCatchGame game = new FunnyCatchGame(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
frame.add(game); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
frame.setSize(500, 500); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
frame.setVisible(true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+80
to
+87
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Improve window initialization
public static void main(String[] args) {
JFrame frame = new JFrame("Funny Catch Game");
FunnyCatchGame game = new FunnyCatchGame();
frame.add(game);
- frame.setSize(500, 500);
+ frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
+ frame.setMinimumSize(new Dimension(WINDOW_WIDTH, WINDOW_HEIGHT));
+ frame.setLocationRelativeTo(null); // Center on screen
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class Item { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
int x, y; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
String type; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Item(int x, int y, String type) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
this.x = x; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
this.y = y; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
this.type = type; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+89
to
+98
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Improve Item class encapsulation and documentation The Item class needs better encapsulation and documentation. - class Item {
- int x, y;
- String type;
+ /**
+ * Represents a falling item in the game.
+ */
+ private static class Item {
+ private int x, y;
+ private final String type;
Item(int x, int y, String type) {
this.x = x;
this.y = y;
+ if (type == null || (!type.equals("🦆") && !type.equals("🌮"))) {
+ throw new IllegalArgumentException("Invalid item type");
+ }
this.type = type;
}
+
+ public int getX() { return x; }
+ public int getY() { return y; }
+ public String getType() { return type; }
} 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Improve field encapsulation and constant declarations
Apply these improvements:
📝 Committable suggestion