Post by johnno56 on Feb 7, 2021 22:34:37 GMT -6
Almost missed this tiny problem. It's all about a single pixel.
I will attach a simple 'snake' game. Within the game you will notice that the images are either sprites or primitives.
The game runs fine using sprites. Try it.
Then comment out the DrawImage()'s in "Draw level" and "Draw snake" sections and un-comment the "setcolor()" and "rectfill()" in the same sections and run it again.
The top right pixel of each of the primitives are either a different colour or non-existent. Also, a rogue pixel appears in the bottom right corner of the score.
I have gone over the code multiple times and cannot figure out the problem. It's not a very noticeable problem except when using a single coloured background.
Sprites
Primitives
You may have to magnify the primitives image...
If both methods work on your machines then just ignore this post... lol
I will attach a simple 'snake' game. Within the game you will notice that the images are either sprites or primitives.
The game runs fine using sprites. Try it.
Then comment out the DrawImage()'s in "Draw level" and "Draw snake" sections and un-comment the "setcolor()" and "rectfill()" in the same sections and run it again.
The top right pixel of each of the primitives are either a different colour or non-existent. Also, a rogue pixel appears in the bottom right corner of the score.
I have gone over the code multiple times and cannot figure out the problem. It's not a very noticeable problem except when using a single coloured background.
xmax = 640
ymax = 480
title$ = "Snake"
WindowOpen(1,title$,windowpos_centered,windowpos_centered,xmax,ymax,0)
CanvasOpen(1,xmax,ymax,0,0,xmax,ymax,0)
' ==================================================================
' Snake, found on an old USB stick from Marcus.
'
' By Marcus Johansson.
' ==================================================================
fnt14 = 1: loadFont(1,"8bit.ttf", 14)
Font(fnt14)
green = 1: loadImage(1,"green.png")
blue = 2: loadImage(2,"blue.png")
orange = 3: loadImage(3,"orange.png")
white = 4: loadImage(4,"white.png")
randomize(timer)
' Max length of snake.
SNAKE_MAX_LENGTH = 30*40
' Directions.
UP = 1
DN = 2
LT = 3
RT = 4
' Snake positions.
Dim snake[SNAKE_MAX_LENGTH, 2]
' Level, holding information about green and blue pills.
Dim level[40, 30]
' Outer game loop.
do
' Initialize snake.
snakeLength = 1
snakeHead = 0
snakeDirection = 0
speed = 10
speedCounter = speed
snake[snakeHead, 0] = 20
snake[snakeHead, 1] = 15
' Initialize score.
score = 0
' Clear level.
for y = 0 to 29
for x = 0 to 39
level[x, y] = 0
next
next
' Set game over flag to false.
gameOver = false
' Timer for spawning a new pill?
nextPlupTime = timer
do
clearcanvas
' Spawn a new pill.
if timer >= nextPlupTime then
if rand(3) = 0 then
level[rand(40), rand(30)] = 2
else
level[rand(40), rand(30)] = 1
end if
nextPlupTime = timer + 2000 + rand(2000)
end if
' Turn.
if key(K_UP) and snakeDirection <> DN then
snakeDirection = UP
end if
if key(K_DOWN) and snakeDirection <> UP then
snakeDirection = DN
end if
if key(K_LEFT) and snakeDirection <> RT then
snakeDirection = LT
end if
if key(K_RIGHT) and snakeDirection <> LT then
snakeDirection = RT
end if
' Move snake?
speedCounter = speedCounter - 1
if speedCounter = 0 then
prev = snakeHead
if snakeDirection > 0 then
snakeHead = (snakeHead + 1) mod SNAKE_MAX_LENGTH
end if
speedCounter = speed
if snakeDirection = UP then
snake[snakeHead, 0] = snake[prev, 0]
snake[snakeHead, 1] = snake[prev, 1] - 1
elseif snakeDirection = DN then
snake[snakeHead, 0] = snake[prev, 0]
snake[snakeHead, 1] = snake[prev, 1] + 1
elseif snakeDirection = LT then
snake[snakeHead, 0] = snake[prev, 0] - 1
snake[snakeHead, 1] = snake[prev, 1]
elseif snakeDirection = RT then
snake[snakeHead, 0] = snake[prev, 0] + 1
snake[snakeHead, 1] = snake[prev, 1]
end if
' Game over if hitting the screen borders.
x = snake[snakeHead, 0]
y = snake[snakeHead, 1]
if x < 0 or x >= 40 or y < 0 or y >= 30 then
gameOver = true
else
' Something to eat?
if level[x, y] > 0 then
snakeLength = snakeLength + 1
' Blue pills are worth 10 times as much as green
' ones but they also make the player faster.
if level[x, y] = 2 then
score = score + 100
speed = max(speed - 1, 0)
end if
if level[x, y] = 1 then
score = score + 10
end if
level[x, y] = 0
end if
end if
end if
' Draw -----------------------------------------------------------------
' Clear screen.
clearcanvas
' Draw level.
for y = 0 to 29
for x = 0 to 39
if level[x, y] = 1 then
DrawImage(green, x * 16, y * 16)
'setcolor(rgb(0, 255, 0))
'rectfill(x*16, y*16, 16, 16)
elseif level[x, y] = 2 then
DrawImage(blue, x * 16, y * 16)
'setcolor(rgb(0, 192, 255))
'rectfill(x*16, y*16, 16, 16)
end if
next
next
' Draw snake.
setcolor(rgb(255, 255, 255))
x = snake[snakeHead, 0]
y = snake[snakeHead, 1]
for i = (snakeHead - snakeLength + 1) to snakeHead
j = i
if j < 0 then
j = j + SNAKE_MAX_LENGTH
end if
DrawImage(white, snake[j, 0] * 16, snake[j, 1] * 16)
'rectfill(snake[j, 0]*16, snake[j, 1]*16, 16, 16)
if i <> snakeHead then
if snake[j, 0] = x and snake[j, 1] = y then
gameOver = true
end if
end if
next
' Write score.
setcolor(rgb(255, 255, 0))
'set caret width(primary)/2, 4
'center "SCORE"
drawtext(str(score), (xmax - 16) / 2, 0)
wait(20)
update()
loop until gameOver
loop
Sprites
Primitives
You may have to magnify the primitives image...
If both methods work on your machines then just ignore this post... lol