-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPingWorld.java
More file actions
135 lines (113 loc) · 3.54 KB
/
PingWorld.java
File metadata and controls
135 lines (113 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import greenfoot.*;
/**
* The Ping World is where Balls and Paddles meet to play pong.
*
* @author Patrik Valentiny (PM), Jakov Klaric, Abdulkader Alomar, Ignas Klimas
* @version 1.2
*/
public class PingWorld extends World
{
private static final int WORLD_WIDTH = 500;
private static final int WORLD_HEIGHT = 700;
private int level = 1;
private Paddle playerPaddle = new Paddle(100,20);
private Paddle2 paddle2 = new Paddle2(100,15);
private Score score = new Score();
private Ball ball = new Ball();
private int[] ballHistory = new int[2];
private Lives life1 = new Lives();
private Lives life2 = new Lives();
private Lives life3 = new Lives();
private int lives = 3;
/**
* Constructor for objects of class PingWorld.
*/
public PingWorld(boolean gameStarted)
{
// Create a new world with WORLD_WIDTHxWORLD_HEIGHT cells with a cell size of 1x1 pixels.
super(WORLD_WIDTH, WORLD_HEIGHT, 1);
if (gameStarted)
{
GreenfootImage background = getBackground();
background.setColor(Color.BLACK);
addObject(ball, WORLD_WIDTH/2, WORLD_HEIGHT/2);
addObject(playerPaddle, 80, WORLD_HEIGHT - 50);
addObject(paddle2, 60, 50);
addObject(score,442, 37);
level = 1;
showLevel();
worldGraphics(background);
}
else
{
Greenfoot.setWorld(new IntroWorld());
}
}
private void worldGraphics(GreenfootImage bg){
int dashSize = 40;
int dashSpace = dashSize / 2;
int liveY = 30;
bg.setColor(Color.BLACK);
bg.fill();
bg.setColor(Color.WHITE);
bg.fillRect(0, 0, 10, WORLD_HEIGHT);
bg.fillRect(WORLD_WIDTH-10, 0, WORLD_WIDTH, WORLD_HEIGHT);
bg.fillRect(0, 0, WORLD_WIDTH, 10);
bg.fillRect(0, WORLD_HEIGHT - 10, WORLD_WIDTH, WORLD_HEIGHT);
addObject(life1, WORLD_WIDTH/2 - 100, liveY);
addObject(life2, WORLD_WIDTH/2 - 150, liveY);
addObject(life3, WORLD_WIDTH/2 - 200, liveY);
for (int i=0;i<WORLD_WIDTH/dashSize;i++){
bg.fillRect(i*dashSize + dashSpace, WORLD_HEIGHT/2 - 3, dashSize - dashSpace, 6);
}
}
public void act(){
showLevel();
ballTrail();
if (lives == 0){
Greenfoot.setWorld(new GameOverWorld(getLevel()));
}
}
public Paddle getPlayerPaddle(){
return playerPaddle;
}
public Paddle2 getAIPaddle(){
return paddle2;
}
public void showLevel(){
score.setImage(new GreenfootImage("Level " + level, 30, Color.WHITE, null));
//showText("Level " + level, 452, 37);
}
public void updateLevel(){
level++;
}
public int getLevel(){
return level;
}
public void setLevelRestart(){
level = 1;
}
private void ballPosHistory(){
ballHistory[0] = ball.getX();
ballHistory[1] = ball.getY();
}
private void ballTrail(){
addObject(new TrailBall(), ballHistory[0], ballHistory[1]);
ballPosHistory();
}
/*
* THis code removes lives when the ball hits the floor.
*/
public void killWombat(){
if(lives == 3){
((Lives)life1).setTransparency(100);
lives--;
}else if(lives == 2){
((Lives)life2).setTransparency(100);
lives--;
}else if (lives == 1){
((Lives)life3).setTransparency(100);
lives--;
}
}
}