In this project I have three kinds (or classes) of object to track. First is the state of the game. These are things like ball coordinates, which change as the game progresses. Second are constants; unchanging settings determined before rather than as the game runs. Finally there are coordinates, which are self explanatory.
Coordinate class definition:
Constant class definition:
State class definition:
Coordinate class definition:
Code:
version 15.1
class coordinate{
double x
double y
}
Code:
version 15.1
class constant{
velocity = 1
timestep = 1
leftbound = 0
rightbound = 100
lowerbound = 0
upperbound = 100
}
Code:
version 15.1
class state{
constant const
coordinate ballstart
coordinate ballcurrent
coordinate paddlestart
coordinate paddlecurrent
double xballvelocity
double yballvelocity
double gameover = 0
}
program .checkgameover
if `.ballcurrent.y' <= `.const.lowerbound'{
.gameover = 1
}
end
program .updateballposition
.ballcurrent.x = `.ballcurrent.x' + (`.xballvelocity' * `.const.timestep')
.ballcurrent.y = `.ballcurrent.y' + (`.yballvelocity' * `.const.timestep')
end
program .checkwallcollision
if `.ballcurrent.x' >= `.const.rightbound' {
.reverseforce
}
if `.ballcurrent.x' <= `.const.leftbound' {
.reverseforce
}
if `.ballcurrent.y' >= `.const.upperbound' {
.reverseforce
}
end
program .reverseforce
if `.xballvelocity' < 0{
.xballvelocity = abs(`.xballvelocity')
}
else{
.xballvelocity = -`.xballvelocity'
}
if `.yballvelocity' < 0{
.yballvelocity = abs(`.yballvelocity')
}
else{
.yballvelocity = -`.yballvelocity'
}
local xtemp = `.xballvelocity'
local ytemp = `.yballvelocity'
.xballvelocity = `xtemp'
.yballvelocity = `ytemp'
end
program .auto_update_paddle_position
.paddlecurrent.x = .ballcurrent.x
end
program .update_paddle_position
endversion 15.1
class state{
constant const
coordinate ballstart
coordinate ballcurrent
coordinate paddlestart
coordinate paddlecurrent
double xballvelocity
double yballvelocity
double gameover = 0
}
program .checkgameover
if `.ballcurrent.y' <= `.const.lowerbound'{
.gameover = 1
}
end
program .updateballposition
.ballcurrent.x = `.ballcurrent.x' + (`.xballvelocity' * `.const.timestep')
.ballcurrent.y = `.ballcurrent.y' + (`.yballvelocity' * `.const.timestep')
end
program .checkwallcollision
if `.ballcurrent.x' >= `.const.rightbound' {
.reverseforce
}
if `.ballcurrent.x' <= `.const.leftbound' {
.reverseforce
}
if `.ballcurrent.y' >= `.const.upperbound' {
.reverseforce
}
end
program .reverseforce
if `.xballvelocity' < 0{
.xballvelocity = abs(`.xballvelocity')
}
else{
.xballvelocity = -`.xballvelocity'
}
if `.yballvelocity' < 0{
.yballvelocity = abs(`.yballvelocity')
}
else{
.yballvelocity = -`.yballvelocity'
}
local xtemp = `.xballvelocity'
local ytemp = `.yballvelocity'
.xballvelocity = `xtemp'
.yballvelocity = `ytemp'
end
program .auto_update_paddle_position
.paddlecurrent.x = .ballcurrent.x
end
program .update_paddle_position
endversion 15.1
class state{
constant const
coordinate ballstart
coordinate ballcurrent
coordinate paddlestart
coordinate paddlecurrent
double xballvelocity
double yballvelocity
double gameover = 0
}
program .checkgameover
if `.ballcurrent.y' <= `.const.lowerbound'{
.gameover = 1
}
end
program .updateballposition
.ballcurrent.x = `.ballcurrent.x' + (`.xballvelocity' * `.const.timestep')
.ballcurrent.y = `.ballcurrent.y' + (`.yballvelocity' * `.const.timestep')
end
program .checkwallcollision
if `.ballcurrent.x' >= `.const.rightbound' {
.reverseforce
}
if `.ballcurrent.x' <= `.const.leftbound' {
.reverseforce
}
if `.ballcurrent.y' >= `.const.upperbound' {
.reverseforce
}
end
program .reverseforce
if `.xballvelocity' < 0{
.xballvelocity = abs(`.xballvelocity')
}
else{
.xballvelocity = -`.xballvelocity'
}
if `.yballvelocity' < 0{
.yballvelocity = abs(`.yballvelocity')
}
else{
.yballvelocity = -`.yballvelocity'
}
local xtemp = `.xballvelocity'
local ytemp = `.yballvelocity'
.xballvelocity = `xtemp'
.yballvelocity = `ytemp'
end
program .auto_update_paddle_position
.paddlecurrent.x = .ballcurrent.x
end
program .update_paddle_position
endversion 15.1
class state{
constant const
coordinate ballstart
coordinate ballcurrent
coordinate paddlestart
coordinate paddlecurrent
double xballvelocity
double yballvelocity
double gameover = 0
}
program .checkgameover
if `.ballcurrent.y' <= `.const.lowerbound'{
.gameover = 1
}
end
program .updateballposition
.ballcurrent.x = `.ballcurrent.x' + (`.xballvelocity' * `.const.timestep')
.ballcurrent.y = `.ballcurrent.y' + (`.yballvelocity' * `.const.timestep')
end
program .checkwallcollision
if `.ballcurrent.x' >= `.const.rightbound' {
.reverseforce
}
if `.ballcurrent.x' <= `.const.leftbound' {
.reverseforce
}
if `.ballcurrent.y' >= `.const.upperbound' {
.reverseforce
}
end
program .reverseforce
if `.xballvelocity' < 0{
.xballvelocity = abs(`.xballvelocity')
}
else{
.xballvelocity = -`.xballvelocity'
}
if `.yballvelocity' < 0{
.yballvelocity = abs(`.yballvelocity')
}
else{
.yballvelocity = -`.yballvelocity'
}
local xtemp = `.xballvelocity'
local ytemp = `.yballvelocity'
.xballvelocity = `xtemp'
.yballvelocity = `ytemp'
end
program .auto_update_paddle_position
.paddlecurrent.x = .ballcurrent.x
end
program .update_paddle_position
endversion 15.1
class state{
constant const
coordinate ballstart
coordinate ballcurrent
coordinate paddlestart
coordinate paddlecurrent
double xballvelocity
double yballvelocity
double gameover = 0
}
program .checkgameover
if `.ballcurrent.y' <= `.const.lowerbound'{
.gameover = 1
}
end
program .updateballposition
.ballcurrent.x = `.ballcurrent.x' + (`.xballvelocity' * `.const.timestep')
.ballcurrent.y = `.ballcurrent.y' + (`.yballvelocity' * `.const.timestep')
end
program .checkwallcollision
if `.ballcurrent.x' >= `.const.rightbound' {
.reverseforce
}
if `.ballcurrent.x' <= `.const.leftbound' {
.reverseforce
}
if `.ballcurrent.y' >= `.const.upperbound' {
.reverseforce
}
end
program .reverseforce
if `.xballvelocity' < 0{
.xballvelocity = abs(`.xballvelocity')
}
else{
.xballvelocity = -`.xballvelocity'
}
if `.yballvelocity' < 0{
.yballvelocity = abs(`.yballvelocity')
}
else{
.yballvelocity = -`.yballvelocity'
}
local xtemp = `.xballvelocity'
local ytemp = `.yballvelocity'
.xballvelocity = `xtemp'
.yballvelocity = `ytemp'
end
program .auto_update_paddle_position
.paddlecurrent.x = .ballcurrent.x
end
program .update_paddle_position
endversion 15.1
class state{
constant const
coordinate ballstart
coordinate ballcurrent
coordinate paddlestart
coordinate paddlecurrent
double xballvelocity
double yballvelocity
double gameover = 0
}
program .checkgameover
if `.ballcurrent.y' <= `.const.lowerbound'{
.gameover = 1
}
end
program .updateballposition
.ballcurrent.x = `.ballcurrent.x' + (`.xballvelocity' * `.const.timestep')
.ballcurrent.y = `.ballcurrent.y' + (`.yballvelocity' * `.const.timestep')
end
program .checkwallcollision
if `.ballcurrent.x' >= `.const.rightbound' {
.reverseforce
}
if `.ballcurrent.x' <= `.const.leftbound' {
.reverseforce
}
if `.ballcurrent.y' >= `.const.upperbound' {
.reverseforce
}
end
program .reverseforce
if `.xballvelocity' < 0{
.xballvelocity = abs(`.xballvelocity')
}
else{
.xballvelocity = -`.xballvelocity'
}
if `.yballvelocity' < 0{
.yballvelocity = abs(`.yballvelocity')
}
else{
.yballvelocity = -`.yballvelocity'
}
local xtemp = `.xballvelocity'
local ytemp = `.yballvelocity'
.xballvelocity = `xtemp'
.yballvelocity = `ytemp'
end
program .auto_update_paddle_position
.paddlecurrent.x = .ballcurrent.x
end
program .update_paddle_position
end

Comment