Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Push a player back when he is hurt?

Name: Anonymous 2010-04-07 14:40

Hello programmers! I am having a tad bit of trouble with my lua code, I am trying to make the play go in the opposite direction (about 5 pixels back) when he is hurt by an enemy or sharp object.

basically like in the older snes and gameboy version of zelda.
The problem is that it keeps going and going until it goes off the screen into oblivion. Anyone know how to get this working correctly?

function playerDirection()
if Player[1].state == 2 and Player[1].hurt == 1 then
Player.x = Player.x + 5
local sound = hurt_01
voice = sound:play()
end
if Player[1].state == 3 and Player[1].hurt == 1 then
Player.x = Player.x - 5
local sound = hurt_01
voice = sound:play()
end
if Player[1].state == 1 and Player[1].hurt == 1 then
Player.y = Player.y + 5
local sound = hurt_01
voice = sound:play()
end
if Player[1].state == 0 and Player[1].hurt == 1 then
Player.y = Player.y - 5
local sound = hurt_01
voice = sound:play()
end
end

Name: Anonymous 2010-04-07 14:44

Hello programmers!
HELLO MOTHERFUCKER
GET ACQUAINTED WITH MY FRIEND
I CALL HIM THE [code] TAG

Name: Anonymous 2010-04-07 14:46

>>2
Oh! It's the shouty man.

Name: Anonymous 2010-04-07 14:46

>>1
Wrap the player state in a monad.

Name: Anonymous 2010-04-07 14:47

>>4
This is definitely the correct solution to the problem.

Name: Anonymous 2010-04-07 14:49

>>2
HEY I DIDN'T THINK I NEEDED IT BRO

Name: Anonymous 2010-04-07 14:56

>>1
Where is the code that tells the player when to stop being hurt, i.e., Player[1].hurt = 0?

Name: Anonymous 2010-04-07 14:57

>>1
Look at all that repeated code and magic numbers, you should be ashamed of yourself.

The problem is that it keeps going and going until it goes off the screen into oblivion
Can you elaborate on this. I've noticed there aren't any edge checks in the code, are you just saying that it goes off screen or that it seems to loop infinitely?

>>4
Haskells State monad is pretty limited, I mean, is it that fucking difficult to have a key value table?

Name: Anonymous 2010-04-07 15:02

Your code is horrendous.
But still, add this line:
Player[1].hurt = 0
before each end. Helping you is useless though, if you can't figure out something so basic.

Wait a second
Lua
IHBT

Name: Anonymous 2010-04-07 15:03

>>8
Write your own monad then. Also how the heck would you define a dictionary, [(String, Dynamic)]?

Name: Anonymous 2010-04-07 15:04

Hello programmers!
HELLO MOTHERFUCKER
GET ACQUAINTED WITH MY FRIEND
I CALL HIM THE TAB KEY

Name: Anonymous 2010-04-07 15:10

>>10
First, I did write rewrite the State Monad when I was figuring out what they were, but I did it in Scheme. I used a list of key value pairs, but if I was doing it again I would use a more efficient tree structure.
Second, you are right, Haskell doesn't have a generic mixable type, but you could have the dictionary be either specialised by the data you were storing [(String,A)]. Presumably you could make a "sexp" data type that would have different constructors for strings, numbers, etc. although it wouldn't work for new user defined types.

Name: Anonymous 2010-04-07 15:10

>>12
s/did write/did/

Name: Anonymous 2010-04-07 16:05

>>7
I could put it in the main loop however, it cancels out the push

>>8
ill just paste the whole program then

--Colo(u)rs
green = Color.new(0,255,0)
red = Color.new(255,0,0)
white = Color.new(255,255,255)
tranblk = Color.new(0,0,0,127)
gray = Color.new(155,155,155)

--Images
grass = Image.load("grass.png")
flower = Image.load("flower.png")
p_u = Image.load("p_u.png")
p_d = Image.load("p_d.png")
p_l = Image.load("p_l.png")
p_r = Image.load("p_r.png")
player = Image.load("p_d.png")
--CreateImages
--player = Image.createEmpty(32,32)
block = Image.createEmpty(32,32)
block2 = Image.createEmpty(32,32)
display = Image.createEmpty(480,9)
hurt_01 = Sound.load("hurt_01.wav",false)
hurt_02 = Sound.load("hurt_02.wav",false)

block:clear(green)
block2:clear(red)
display:clear(Color.new(0,0,0 ,127))
--player:clear(white)

Player = { x = 30, y = 100 }
Player[1] = { health = 100, state = 1, hurt = 0 }
playerHeight = 32
playerWidth = 32

Block = {}
Block[1] = { x = 100,y = 0, height = block:height(), width = block:width() }
Block[2] = { x = 300,y = 30, height = block:height(), width = block:width() }
Block[3] = { x = 200,y = 58, height = block:height(), width = block:width() }
Block[4] = { x = 100,y = 32, height = block:height(), width = block:width() }

Block2 = {}
Block2[1] = { x = 100,y = 64, height = block2:height(), width = block2:width() }
Block2[2] = { x = 100,y = 96, height = block2:height(), width = block2:width() }

function movePlayer()
pad = Controls.read()
if pad:left() and Player[1].health > 0  then
Player.x = Player.x - 2
Player[1].state = 2
end
if pad:right() and Player[1].health > 0  then
Player.x = Player.x + 2
Player[1].state = 3
end
if pad:up() and Player[1].health > 0  then
Player.y = Player.y - 2
Player[1].state = 1
end
if pad:down() and Player[1].health > 0  then
Player.y = Player.y + 2
Player[1].state = 0
end
end

function playerDirection()
if Player[1].state == 2 and Player[1].hurt == 1 then
Player.x = Player.x + 5
local sound = hurt_01
voice = sound:play()
end
if Player[1].state == 3 and Player[1].hurt == 1 then
Player.x = Player.x - 5
local sound = hurt_01
voice = sound:play()
end
if Player[1].state == 1 and Player[1].hurt == 1 then
Player.y = Player.y + 5
local sound = hurt_01
voice = sound:play()
end
if Player[1].state == 0 and Player[1].hurt == 1 then
Player.y = Player.y - 5
local sound = hurt_01
voice = sound:play()
end
end

function collisionCheck(object)
if (Player.x + playerWidth > object.x) and (Player.x < object.x + object.width) and (Player.y + playerHeight > object.y) and (Player.y < object.y + object.height) then
Player.x = oldx
Player.y = oldy
end
end

function collisionHurt (object)
if (Player.x + playerWidth > object.x) and (Player.x < object.x + object.width) and (Player.y + playerHeight > object.y) and (Player.y < object.y + object.height) then
Player[1].hurt = 1
Player.x = oldx
Player.y = oldy
Player[1].health = Player[1].health - 1
end
end

function playerFace()
if Player[1].state == 0 then
screen:blit(Player.x,Player.y,p_d)
end
if Player[1].state == 1 then
screen:blit(Player.x,Player.y,p_u)
end
if Player[1].state == 2 then
screen:blit(Player.x,Player.y,p_l)
end
if Player[1].state == 3 then
screen:blit(Player.x,Player.y,p_r)
end
end

function pasteBlocks()
collisionCheck(Block[1])
collisionCheck(Block[2])
collisionCheck(Block[3])
collisionCheck(Block[4])
collisionHurt(Block2[1])
collisionHurt(Block2[2])

for a = 1,4 do
screen:blit(Block[a].x,Block[a].y,block)
end

for b = 1,2 do
screen:blit(Block2[b].x,Block2[b].y,block2)
end
end

function pasteimages()
for bgx = 0, 14 do
for bgy = 0,8 do
screen:blit(32 * bgx, 32 * bgy, grass)
end
end
screen:blit(100,100,flower)
screen:blit(300,220,flower)
end

function HUD()
screen:blit(0,263,display)
printCentered(264,"Health: " .. Player[1].health .. Player[1].hurt .. Player[1].state,white)
end

function printCentered(y,text,color)
local length = string.len(text)
local x = 240 - ((length*8)/2)
screen:print(x,y,text,color)
end

while true do

Player[1].hurt = 0

oldx = Player.x
oldy = Player.y

screen:clear()

movePlayer()

playerDirection()

pasteimages()

pasteBlocks()

screen:blit(Player.x,Player.y,player)

playerFace()

HUD()

screen.waitVblankStart()
screen.flip()
end

Name: Anonymous 2010-04-07 16:19

>>9
oh shi-

that worked, never mind, I'm dumb thanks for your time.

Name: Anonymous 2010-04-07 16:47

ONCE AGAIN THE BRAVE /prog/RIDERS MAKE THE WORLD A BETTER PLACE. WE HAVE YET ANOTHER SATISFIED CUSTOMER

Name: Anonymous 2010-04-07 16:49

WE FINALLY HAVE A SATISFIED CUSTOMER
ftfy

Name: Anonymous 2010-11-27 0:24

Name: Anonymous 2010-12-17 1:29

Xarn is a bad boyfriend

Don't change these.
Name: Email:
Entire Thread Thread List