Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
FaultInOurPong
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Group3
FaultInOurPong
Commits
8ed50b14
Commit
8ed50b14
authored
8 years ago
by
Jie Luo
Browse files
Options
Downloads
Patches
Plain Diff
comments added to the model
parent
4f12761b
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
Game_Code/src/Game.java
+0
-155
0 additions, 155 deletions
Game_Code/src/Game.java
Game_Code/src/View.java
+1
-1
1 addition, 1 deletion
Game_Code/src/View.java
with
1 addition
and
156 deletions
Game_Code/src/Game.java
deleted
100644 → 0
+
0
−
155
View file @
4f12761b
import
java.awt.*
;
import
java.awt.event.*
;
import
java.awt.geom.Ellipse2D
;
import
java.awt.geom.Rectangle2D
;
import
java.util.HashMap
;
import
java.util.HashSet
;
import
javax.swing.*
;
public
class
Game
extends
JPanel
implements
KeyListener
,
ActionListener
{
private
int
height
,
width
;
private
Timer
t
=
new
Timer
(
5
,
this
);
private
boolean
first
;
private
HashSet
<
String
>
keys
=
new
HashSet
<
String
>();
// pad
private
final
int
SPEED
=
1
;
private
int
padH
=
10
,
padW
=
40
;
private
int
bottomPadX
,
topPadX
;
private
int
inset
=
10
;
// ball
private
double
ballX
,
ballY
,
velX
=
1
,
velY
=
1
,
ballSize
=
20
;
// score
private
int
scoreTop
,
scoreBottom
;
public
Game
()
{
addKeyListener
(
this
);
setFocusable
(
true
);
setFocusTraversalKeysEnabled
(
false
);
first
=
true
;
t
.
setInitialDelay
(
100
);
t
.
start
();
}
@Override
protected
void
paintComponent
(
Graphics
g
)
{
super
.
paintComponent
(
g
);
Graphics2D
g2d
=
(
Graphics2D
)
g
;
height
=
getHeight
();
width
=
getWidth
();
// initial positioning
if
(
first
)
{
bottomPadX
=
width
/
2
-
padW
/
2
;
topPadX
=
bottomPadX
;
ballX
=
width
/
2
-
ballSize
/
2
;
ballY
=
height
/
2
-
ballSize
/
2
;
first
=
false
;
}
// bottom pad
Rectangle2D
bottomPad
=
new
Rectangle
(
bottomPadX
,
height
-
padH
-
inset
,
padW
,
padH
);
g2d
.
fill
(
bottomPad
);
// top pad
Rectangle2D
topPad
=
new
Rectangle
(
topPadX
,
inset
,
padW
,
padH
);
g2d
.
fill
(
topPad
);
// ball
Ellipse2D
ball
=
new
Ellipse2D
.
Double
(
ballX
,
ballY
,
ballSize
,
ballSize
);
g2d
.
fill
(
ball
);
// scores
String
scoreB
=
"Bottom: "
+
new
Integer
(
scoreBottom
).
toString
();
String
scoreT
=
"Top: "
+
new
Integer
(
scoreTop
).
toString
();
g2d
.
drawString
(
scoreB
,
10
,
height
/
2
);
g2d
.
drawString
(
scoreT
,
width
-
50
,
height
/
2
);
}
@Override
public
void
actionPerformed
(
ActionEvent
e
)
{
// side walls
if
(
ballX
<
0
||
ballX
>
width
-
ballSize
)
{
velX
=
-
velX
;
}
// top / down walls
if
(
ballY
<
0
)
{
velY
=
-
velY
;
++
scoreBottom
;
}
if
(
ballY
+
ballSize
>
height
)
{
velY
=
-
velY
;
++
scoreTop
;
}
// bottom pad
if
(
ballY
+
ballSize
>=
height
-
padH
-
inset
&&
velY
>
0
)
if
(
ballX
+
ballSize
>=
bottomPadX
&&
ballX
<=
bottomPadX
+
padW
)
velY
=
-
velY
;
// top pad
if
(
ballY
<=
padH
+
inset
&&
velY
<
0
)
if
(
ballX
+
ballSize
>=
topPadX
&&
ballX
<=
topPadX
+
padW
)
velY
=
-
velY
;
ballX
+=
velX
;
ballY
+=
velY
;
// pressed keys
if
(
keys
.
size
()
==
1
)
{
if
(
keys
.
contains
(
"LEFT"
))
{
bottomPadX
-=
(
bottomPadX
>
0
)
?
SPEED
:
0
;
}
else
if
(
keys
.
contains
(
"RIGHT"
))
{
bottomPadX
+=
(
bottomPadX
<
width
-
padW
)
?
SPEED
:
0
;
}
}
// AI
double
delta
=
ballX
-
topPadX
;
if
(
delta
>
0
)
{
topPadX
+=
(
topPadX
<
width
-
padW
)
?
SPEED
:
0
;
}
else
if
(
delta
<
0
)
{
topPadX
-=
(
topPadX
>
0
)
?
SPEED
:
0
;
}
repaint
();
}
@Override
public
void
keyTyped
(
KeyEvent
e
)
{}
@Override
public
void
keyPressed
(
KeyEvent
e
)
{
int
code
=
e
.
getKeyCode
();
switch
(
code
)
{
case
KeyEvent
.
VK_LEFT
:
keys
.
add
(
"LEFT"
);
break
;
case
KeyEvent
.
VK_RIGHT
:
keys
.
add
(
"RIGHT"
);
break
;
}
}
@Override
public
void
keyReleased
(
KeyEvent
e
)
{
int
code
=
e
.
getKeyCode
();
switch
(
code
)
{
case
KeyEvent
.
VK_LEFT
:
keys
.
remove
(
"LEFT"
);
break
;
case
KeyEvent
.
VK_RIGHT
:
keys
.
remove
(
"RIGHT"
);
break
;
}
}
}
This diff is collapsed.
Click to expand it.
Game_Code/src/View.java
+
1
−
1
View file @
8ed50b14
...
...
@@ -107,7 +107,7 @@ public class View extends JFrame{
}
public
void
single
()
{
Pong_viewAndController
view_controller
=
new
Pong_viewAndController
(
this
);
Pong_viewAndController
view_controller
=
new
Pong_viewAndController
();
this
.
setVisible
(
false
);
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment