-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
155 lines (126 loc) · 3.6 KB
/
main.lua
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
-- Load values at the start of the program
function love.load()
-- Constant Values
SCREEN_WIDTH = love.graphics.getWidth();
SCREEN_HEIGHT = love.graphics.getHeight();
-- Game Values
frameCount = 0
-- Rectangle Starting Values
rectPosX = 20
rectPosY = 20
rectWidth = 50
rectHeight = 50
-- Rectangle Color Values
rectColorR = 0
rectColorG = 100
rectColorB = 100
-- Rectangle Motion Values
rectMovingUp = false;
rectMovingRight = true;
rectVelocityX = 5;
rectVelocityY = 5;
-- Setting Mood Values
moodDisplacement = 0;
mood = "A"
end
-- Draw Each Frame
function love.draw()
-- Set Graphics Color
love.graphics.setColor(rectColorR, rectColorG, rectColorB)
-- Draw Rectangle on Screen
love.graphics.rectangle("fill", rectPosX, rectPosY, rectWidth, rectHeight)
-- Display Frame Count and Velocity
local frameString = "Frame: "..frameCount -- Using '..' to concat strings
love.graphics.print(frameString, 0, 0)
love.graphics.print(("Velocity X: "..rectVelocityX), 0, 20);
love.graphics.print(("Velocity Y: "..rectVelocityY), 0, 40);
love.graphics.print("MOOD", 700, 500)
-- Add & Display Mood
mood = mood.."A"
if(string.len(mood)>70)
then
moodDisplacement = moodDisplacement - 3
love.graphics.print(mood, moodDisplacement, SCREEN_HEIGHT-20)
else
love.graphics.print(mood, 0, SCREEN_HEIGHT-20)
end
end
-- Update Variables Each Frame
function love.update(dt)
-- Move Rectangle According to Y Direction
if(rectMovingUp)
then
rectPosY = rectPosY + rectVelocityY
else
rectPosY = rectPosY - rectVelocityY
end
-- Move Rectangle According to X Direction
if(rectMovingRight)
then
rectPosX = rectPosX + rectVelocityX
else
rectPosX = rectPosX - rectVelocityX
end
-- Get Rectangle Collision Info
local collidedRight = (rectPosX + rectWidth) >= SCREEN_WIDTH
local collidedLeft = rectPosX <= 0
local collidedUp = rectPosY <= 0
local collidedDown = (rectPosY + rectHeight) >= SCREEN_HEIGHT
-- Change Direction on Collision
if (collidedRight or collidedLeft or collidedUp or collidedDown)
then
rectMovingUp = not rectMovingUp
rectMovingRight = not rectMovingRight
end
--Reposition on Out of Bounds X Position
if (collidedRight)
then
rectPosX = SCREEN_WIDTH - rectWidth
end
if (collidedLeft)
then
rectPosX = 0
end
--Reposition on Out of Bounds Y Position
if (collidedDown)
then
rectPosY = SCREEN_HEIGHT - rectHeight
end
if (collidedUp)
then
rectPosY = 0
end
-- Adjusting Velocities
local adjustmentX = love.math.random(-8,8)
local adjustmentY = love.math.random(-8,8)
rectVelocityX = rectVelocityX + adjustmentX
rectVelocityY = rectVelocityY + adjustmentY
-- Maxing Velocity
if (rectVelocityX > 10)
then
rectVelocityX = 10
end
if (rectVelocityY > 10)
then
rectVelocityY = 10
end
-- Minimizing Velocity
if (rectVelocityX < -10)
then
rectVelocityX = -10
end
if (rectVelocityY < -10)
then
rectVelocityY = -10
end
-- Getting Random New Color Values
local newColorR = love.math.random(1,255)
local newColorG = love.math.random(1,255)
local newColorB = love.math.random(1,255)
-- Setting New Rectangle Color
rectColorR = newColorR
rectColorG = newColorG
rectColorB = newColorB
-- Incrementing Frame Count
frameCount = frameCount + 1
end