Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Jochen Bauer
spn-website
Commits
50614557
Commit
50614557
authored
Apr 30, 2018
by
Hubert Denkmair
Browse files
initialize new snakes with demo code
parent
d13f06cb
Changes
3
Hide whitespace changes
Inline
Side-by-side
ide/templates/ide/initial-bot.lua
0 → 100644
View file @
50614557
--- WELCOME TO YOUR FIRST SNAKE!
-- you can edit this code, save and run it.
-- you should see log output at the bottom of this page,
-- and a live view on your snake's wellbeing on the right
--- init() is called once upon creation of the bot
-- initialize your data here, and maybe set colors for your snake
function
init
()
self
.
colors
=
{
0xFF0000
,
0x808080
}
end
--- step() is called once every frame, maybe up to 60 times per second.
-- implement your game logic here.
-- after deciding what your bot should do next,
-- just return the desired steering angle.
-- a negative angle means turn left and a positive angle means turn right.
-- with 0, the snake keeps its current direction.
function
step
()
-- your snake needs food to grow
-- to find food in your head's surroundings, call something like that:
local
food
=
findFood
(
100
,
0
.
8
)
-- this will give you all food in maximum distance of 100 around your head,
-- with a value of at least 0.8
-- you can iterate over the result:
for
i
,
item
in
food
:
pairs
()
do
-- distance of the food item, relative to the center of your head
local
distance
=
item
.
dist
-- direction to the food item, in radiens.
-- 0 means "straight ahead", math.pi means "right behind you"
local
direction
=
item
.
d
end
-- you should also look out for your enemies.
-- to find snake segments around you, call:
local
segments
=
findSegments
(
100
,
false
)
-- in return, you get a list of
-- all snake segments nearer than 100 to your head,
-- in this case not including your own segments:
for
i
,
item
in
segments
:
pairs
()
do
-- id of the bot the segment belongs to
-- (you can compare this to self.id)
local
bot
=
item
.
bot
-- distance to the center of the segment
local
distance
=
item
.
dist
-- direction to the segment
local
direction
=
item
.
d
-- radius of the segment
local
radius
=
item
.
r
if
distance
<
10
then
-- you can send some log output to the web IDE, but it's rate limited.
log
(
"oh no, i'm going to die!"
)
end
end
return
0
.
002
-- this will lead us in a large circle, clockwise.
end
\ No newline at end of file
ide/views.py
View file @
50614557
...
...
@@ -4,6 +4,7 @@ from django.db.models import BooleanField, Case, Max, Value, When
from
django.forms
import
ModelForm
from
django.http
import
JsonResponse
from
django.shortcuts
import
render
,
redirect
from
django.template.loader
import
render_to_string
from
core.models
import
SnakeVersion
,
ActiveSnake
,
ServerCommand
,
UserProfile
...
...
@@ -44,6 +45,7 @@ def snake_edit(request, snake_id=-1):
snake
=
SnakeVersion
.
objects
.
get
(
pk
=
snake_id
)
except
SnakeVersion
.
DoesNotExist
:
snake
=
SnakeVersion
(
version
=
0
,
user
=
request
.
user
)
snake
.
code
=
render_to_string
(
'ide/initial-bot.lua'
)
if
snake
.
user
!=
request
.
user
:
raise
PermissionDenied
...
...
visualization/static/visualization/Snake.js
View file @
50614557
"
use strict
"
;
"
use strict
"
;
function
SnakeSegment
(
snake
)
{
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment