mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
all: require calling optfn() ? / optfn() or {...} for fn optfn() ? {}
This commit is contained in:
@@ -60,12 +60,12 @@ fn (mut v Vec) randomize(min_x int, min_y int, max_x int, max_y int) {
|
||||
// part of snake's body representation
|
||||
struct BodyPart {
|
||||
mut:
|
||||
pos Vec = {
|
||||
x: block_size
|
||||
y: block_size
|
||||
}
|
||||
pos Vec = {
|
||||
x: block_size
|
||||
y: block_size
|
||||
}
|
||||
color termui.Color = green
|
||||
facing Orientation = .top
|
||||
facing Orientation = .top
|
||||
}
|
||||
|
||||
// snake representation
|
||||
@@ -75,9 +75,9 @@ mut:
|
||||
direction Orientation
|
||||
body []BodyPart
|
||||
velocity Vec = Vec{
|
||||
x: 0
|
||||
y: 0
|
||||
}
|
||||
x: 0
|
||||
y: 0
|
||||
}
|
||||
}
|
||||
|
||||
// length returns the snake's current length
|
||||
@@ -125,8 +125,16 @@ fn (mut s Snake) move() {
|
||||
piece.facing = s.direction
|
||||
new_x := piece.pos.x + s.velocity.x
|
||||
new_y := piece.pos.y + s.velocity.y
|
||||
piece.pos.x += if new_x > block_size && new_x < width - block_size { s.velocity.x } else { 0 }
|
||||
piece.pos.y += if new_y > block_size && new_y < height - block_size { s.velocity.y } else { 0 }
|
||||
piece.pos.x += if new_x > block_size && new_x < width - block_size {
|
||||
s.velocity.x
|
||||
} else {
|
||||
0
|
||||
}
|
||||
piece.pos.y += if new_y > block_size && new_y < height - block_size {
|
||||
s.velocity.y
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
s.body[i] = piece
|
||||
}
|
||||
@@ -205,10 +213,10 @@ fn (s Snake) check_overlap() bool {
|
||||
|
||||
fn (s Snake) check_out_of_bounds() bool {
|
||||
h := s.get_head()
|
||||
return h.pos.x + s.velocity.x <= block_size ||
|
||||
h.pos.x + s.velocity.x > s.app.width - s.velocity.x || h.pos.y + s.velocity.y <= block_size ||
|
||||
h.pos.y + s.velocity.y >
|
||||
s.app.height - block_size - s.velocity.y
|
||||
return h.pos.x + s.velocity.x <= block_size
|
||||
|| h.pos.x + s.velocity.x > s.app.width - s.velocity.x
|
||||
|| h.pos.y + s.velocity.y <= block_size
|
||||
|| h.pos.y + s.velocity.y > s.app.height - block_size - s.velocity.y
|
||||
}
|
||||
|
||||
// draw draws the parts of the snake
|
||||
@@ -233,10 +241,10 @@ fn (s Snake) draw() {
|
||||
// rat representation
|
||||
struct Rat {
|
||||
mut:
|
||||
pos Vec = {
|
||||
x: block_size
|
||||
y: block_size
|
||||
}
|
||||
pos Vec = {
|
||||
x: block_size
|
||||
y: block_size
|
||||
}
|
||||
captured bool
|
||||
color termui.Color = grey
|
||||
app &App
|
||||
@@ -244,8 +252,8 @@ mut:
|
||||
|
||||
// randomize spawn the rat in a new spot within the playable field
|
||||
fn (mut r Rat) randomize() {
|
||||
r.pos.randomize(2 * block_size + buffer, 2 * block_size + buffer, r.app.width - block_size -
|
||||
buffer, r.app.height - block_size - buffer)
|
||||
r.pos.randomize(2 * block_size + buffer, 2 * block_size + buffer, r.app.width - block_size - buffer,
|
||||
r.app.height - block_size - buffer)
|
||||
}
|
||||
|
||||
struct App {
|
||||
@@ -255,7 +263,7 @@ mut:
|
||||
rat Rat
|
||||
width int
|
||||
height int
|
||||
redraw bool = true
|
||||
redraw bool = true
|
||||
state GameState = .game
|
||||
}
|
||||
|
||||
@@ -387,9 +395,8 @@ fn (mut a App) move_snake(direction Orientation) {
|
||||
fn (a App) check_capture() bool {
|
||||
snake_pos := a.snake.get_head().pos
|
||||
rat_pos := a.rat.pos
|
||||
return snake_pos.x <= rat_pos.x + block_size &&
|
||||
snake_pos.x + block_size >= rat_pos.x && snake_pos.y <= rat_pos.y + block_size && snake_pos.y +
|
||||
block_size >= rat_pos.y
|
||||
return snake_pos.x <= rat_pos.x + block_size && snake_pos.x + block_size >= rat_pos.x
|
||||
&& snake_pos.y <= rat_pos.y + block_size&& snake_pos.y + block_size >= rat_pos.y
|
||||
}
|
||||
|
||||
fn (mut a App) draw_snake() {
|
||||
@@ -454,12 +461,12 @@ fn (mut a App) draw_gameover() {
|
||||
}
|
||||
|
||||
mut app := &App{}
|
||||
app.termui = termui.init({
|
||||
app.termui = termui.init(
|
||||
user_data: app
|
||||
event_fn: event
|
||||
frame_fn: frame
|
||||
init_fn: init
|
||||
hide_cursor: true
|
||||
frame_rate: 10
|
||||
})
|
||||
app.termui.run()
|
||||
)
|
||||
app.termui.run() ?
|
||||
|
||||
Reference in New Issue
Block a user