-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
159 lines (137 loc) · 4.99 KB
/
Copy pathmain.lua
File metadata and controls
159 lines (137 loc) · 4.99 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
-- Simple Asteroids Clone in Love2D Lua
local player = { x = 400, y = 300, angle = 0, speed = 0, maxSpeed = 400, radius = 10 }
local bullets = {}
local asteroids = {}
local asteroidSize = 3
local gameOver = false
function love.load()
love.window.setTitle("Simple Asteroids Clone")
love.window.setMode(800, 600)
love.graphics.setBackgroundColor(0, 0, 0)
-- Create asteroids
local numAsteroids = love.math.random(3, 6) -- Random amount of asteroids between 3 and 6
for i = 1, numAsteroids do
local asteroid = {
x = love.math.random(0, 800),
y = love.math.random(0, 600),
angle = love.math.random() * math.pi * 2,
speed = love.math.random(50, 150),
size = asteroidSize
}
table.insert(asteroids, asteroid)
end
end
function love.update(dt)
if not gameOver then
-- Player movement
if love.keyboard.isDown("up") then
player.speed = player.speed + 100 * dt
else
player.speed = player.speed - 100 * dt
end
if love.keyboard.isDown("left") then
player.angle = player.angle - 3 * dt
elseif love.keyboard.isDown("right") then
player.angle = player.angle + 3 * dt
end
-- Limit player speed
player.speed = math.max(0, math.min(player.speed, player.maxSpeed))
-- Update player position
player.x = player.x + player.speed * math.cos(player.angle) * dt
player.y = player.y + player.speed * math.sin(player.angle) * dt
-- Wrap player around the screen
if player.x < 0 then
player.x = 800
elseif player.x > 800 then
player.x = 0
end
if player.y < 0 then
player.y = 600
elseif player.y > 600 then
player.y = 0
end
-- Update bullets
for i, bullet in ipairs(bullets) do
bullet.x = bullet.x + bullet.speed * math.cos(bullet.angle) * dt
bullet.y = bullet.y + bullet.speed * math.sin(bullet.angle) * dt
-- Remove bullets when they go off-screen
if bullet.x < 0 or bullet.x > 800 or bullet.y < 0 or bullet.y > 600 then
table.remove(bullets, i)
end
-- Check for bullet-asteroid collision
for j, asteroid in ipairs(asteroids) do
if distance(bullet.x, bullet.y, asteroid.x, asteroid.y) < 20 * asteroid.size then
table.remove(bullets, i)
if asteroid.size > 1 then
local newAsteroid = {
x = asteroid.x,
y = asteroid.y,
angle = love.math.random() * math.pi * 2,
speed = love.math.random(50, 150),
size = asteroid.size - 1
}
table.insert(asteroids, newAsteroid)
table.insert(asteroids, newAsteroid)
end
table.remove(asteroids, j)
break
end
end
end
-- Update asteroids
for i, asteroid in ipairs(asteroids) do
asteroid.x = asteroid.x + asteroid.speed * math.cos(asteroid.angle) * dt
asteroid.y = asteroid.y + asteroid.speed * math.sin(asteroid.angle) * dt
-- Wrap asteroids around the screen
if asteroid.x < 0 then
asteroid.x = 800
elseif asteroid.x > 800 then
asteroid.x = 0
end
if asteroid.y < 0 then
asteroid.y = 600
elseif asteroid.y > 600 then
asteroid.y = 0
end
-- Check for asteroid-player collision
if distance(player.x, player.y, asteroid.x, asteroid.y) < player.radius + 20 * asteroid.size then
gameOver = true
end
end
end
end
function love.draw()
-- Draw player
love.graphics.setColor(255, 255, 255)
love.graphics.circle("line", player.x, player.y, player.radius)
-- Draw bullets
for _, bullet in ipairs(bullets) do
love.graphics.circle("fill", bullet.x, bullet.y, 3)
end
-- Draw asteroids
for _, asteroid in ipairs(asteroids) do
love.graphics.circle("line", asteroid.x, asteroid.y, 20 * asteroid.size)
end
-- Draw game over message if necessary
if gameOver then
love.graphics.setColor(255, 0, 0)
love.graphics.print("Game Over", 350, 280)
end
end
function love.keypressed(key)
if key == "space" then
-- Shoot bullet
local bulletSpeed = 600
local bullet = {
x = player.x,
y = player.y,
angle = player.angle,
speed = bulletSpeed
}
table.insert(bullets, bullet)
end
end
-- Helper function to calculate distance between two points
function distance(x1, y1, x2, y2)
return math.sqrt((x2 - x1)^2 + (y2 - y1)^2)
end