[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

how to execute a method every few seconds

Zoe Phoenix

4/14/2008 8:07:00 AM

I'm playing around with making a small test game with what little Ruby
code I've learned so far and I feel like I'm doing good for only having
been learning the past couple of weeks or so. I've mostly been figuring
things out as I go, but I need a little help.

Say I wanted a character to be afflicted with a "poison" status that
removes 5% of their current HP. I know it should be a method that occurs
every few seconds (or just every turn) and that it should loop until
something is used to cure it or the battle ends, but I'm not sure how to
do this. I'd like to make it repeat every, say, 15 seconds or so, but I
guess it doesn't really matter how often, since this is just for
practice. The following code is incomplete (and possibly incorrect), but
please advise:

def poison
#loop begins.. help?
character.hp - (character.hp * 0.03)
puts 'Your HP dropped!' + Character[1].hp'/'+Character[1].maxhp
end #loop ends if battle ends or if antidote used
end


If anyone would like to school me on how I would apply an "antidote"
item to remove the effect, feel free to teach. :-p


Before anybody says anything, I'm just doing this for practice... I have
no intention of trying to make a full game, lol. Just practicing with
bits and pieces of one. :-p
--
Posted via http://www.ruby-....

18 Answers

Phillip Gawlowski

4/14/2008 8:17:00 AM

0

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Zoe Phoenix wrote:
| I'm playing around with making a small test game with what little Ruby
| code I've learned so far and I feel like I'm doing good for only having
| been learning the past couple of weeks or so. I've mostly been figuring
| things out as I go, but I need a little help.
|
| Say I wanted a character to be afflicted with a "poison" status that
| removes 5% of their current HP. I know it should be a method that occurs
| every few seconds (or just every turn) and that it should loop until
| something is used to cure it or the battle ends, but I'm not sure how to
| do this. I'd like to make it repeat every, say, 15 seconds or so, but I
| guess it doesn't really matter how often, since this is just for
| practice. The following code is incomplete (and possibly incorrect), but
| please advise:

On every update (tick in your game, be it screen redrawing, or turn,
whatever), you should have a method updating the game state. In that
method, you can execute the relevant updating code.

Quick and dirty example:

while game do
~ update_enemies
~ update_environment
~ update_effects
~ update_playerstate # You'd check for poison in this method
~ draw_screen
~ get_input
end

If it shouldn't happen always, you could check for a counter (the modulo
operate could come in handy here ;).

- --
Phillip Gawlowski
Twitter: twitter.com/cynicalryan

~ - You know you've been hacking too long when...
...your SO asks you where you want to eat on a friday night and you want to:
cat yellowpages | grep pizza | grep carryout | more
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail....

iEYEARECAAYFAkgDEwMACgkQbtAgaoJTgL/toQCbBoLWIgw6Mg6Ykr6Qt7526x28
12cAnRMuzf+ygMyaRz5W12TGmMDgv4xy
=jD1Z
-----END PGP SIGNATURE-----

Ken Bloom

4/14/2008 2:46:00 PM

0

On Mon, 14 Apr 2008 03:06:50 -0500, Zoe Phoenix wrote:

> I'm playing around with making a small test game with what little Ruby
> code I've learned so far and I feel like I'm doing good for only having
> been learning the past couple of weeks or so. I've mostly been figuring
> things out as I go, but I need a little help.
>
> Say I wanted a character to be afflicted with a "poison" status that
> removes 5% of their current HP. I know it should be a method that occurs
> every few seconds (or just every turn) and that it should loop until
> something is used to cure it or the battle ends, but I'm not sure how to
> do this. I'd like to make it repeat every, say, 15 seconds or so, but I
> guess it doesn't really matter how often, since this is just for
> practice. The following code is incomplete (and possibly incorrect), but
> please advise:
>
> def poison
> #loop begins.. help?
> character.hp - (character.hp * 0.03)
> puts 'Your HP dropped!' + Character[1].hp'/'+Character[1].maxhp
> end #loop ends if battle ends or if antidote used
> end

You could use a thread

Thread.new do
until antidote?
character.hp -= whatever
puts 'Your HP dropped...'
sleep 5
end
end

but as Phillip mentioned, it's probably better to look at how your game
is organized as a whole, because lots of little threads doing stuff like
this is going to get confusing, buggy, and dangerous quickly.



--
Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu...

James Tucker

4/14/2008 3:54:00 PM

0


On 14 Apr 2008, at 09:17, Phillip Gawlowski wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Zoe Phoenix wrote:
> | I'm playing around with making a small test game with what little
> Ruby
> | code I've learned so far and I feel like I'm doing good for only
> having
> | been learning the past couple of weeks or so. I've mostly been
> figuring
> | things out as I go, but I need a little help.
> |
> | Say I wanted a character to be afflicted with a "poison" status that
> | removes 5% of their current HP. I know it should be a method that
> occurs
> | every few seconds (or just every turn) and that it should loop until
> | something is used to cure it or the battle ends, but I'm not sure
> how to
> | do this. I'd like to make it repeat every, say, 15 seconds or so,
> but I
> | guess it doesn't really matter how often, since this is just for
> | practice. The following code is incomplete (and possibly
> incorrect), but
> | please advise:
>
> On every update (tick in your game, be it screen redrawing, or turn,
> whatever), you should have a method updating the game state. In that
> method, you can execute the relevant updating code.
>
> Quick and dirty example:
>
> while game do
> ~ update_enemies
> ~ update_environment
> ~ update_effects
> ~ update_playerstate # You'd check for poison in this method
> ~ draw_screen
> ~ get_input
> end

Can I make a very very strong suggestion that the design de-couples
game-world logic from both rendering and all other I/O forms. The
logic bound operations in domain time cannot be regulated with regard
to the dynamic complexity of a modern logic set (running in 'real
time' aka. as fast as you can complete that loop), and even more so in
a very high level language like ruby.

Don't take my word for it either: gaffer.org.

Failure to do so often leaves one with a game that requires very
specific relative timings on inputs if rapid tapping style input is
required, and under optic-flow systems causes a coupling between
twitch flow specifics that turns out to not only feel horrible, but
also causes damage to the player (eye strain, and rsi from input usage
styles). Specific frequencies obviously vary from engine to engine,
however, there have been real failures caused directly by this paradigm.

> If it shouldn't happen always, you could check for a counter (the
> modulo
> operate could come in handy here ;).
>
> - --
> Phillip Gawlowski
> Twitter: twitter.com/cynicalryan
>
> ~ - You know you've been hacking too long when...
> ...your SO asks you where you want to eat on a friday night and you
> want to:
> cat yellowpages | grep pizza | grep carryout | more
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.8 (MingW32)
> Comment: Using GnuPG with Mozilla - http://enigmail....
>
> iEYEARECAAYFAkgDEwMACgkQbtAgaoJTgL/toQCbBoLWIgw6Mg6Ykr6Qt7526x28
> 12cAnRMuzf+ygMyaRz5W12TGmMDgv4xy
> =jD1Z
> -----END PGP SIGNATURE-----
>


Zoe Phoenix

4/15/2008 2:36:00 AM

0

yeah, I can see what you're saying, Ken, I was just trying to get an
idea of how basically to do something like that in a short, simple test
"game". Not really so much a game as just a test to see if the code
functions the way it's supposed to. I'm nowhere near ready to create a
full game yet. :-p

I'm just trying to get used to some of the concepts a game would
require, such as adding items to inventory, taking them out, using items
on characters to remove status effects, etc. I'm trying to keep it on a
simple, one character/enemy level right now. I want to understand how to
do these basic things on a small, one character scale before I get into
trying to do anything larger, even on just a text-based level.

Simple battle with 1 enemy with only 'attack' and 'item' as options,
attacks always succeed (so I don't have to worry about programming the
possibility of them missing the target) no special skills, except the
enemy can inflict 'poison' on the character. I'm slowly figuring things
out as I go.

--
Posted via http://www.ruby-....

Morris Goodman

1/9/2011 9:26:00 PM

0

On Sun, 9 Jan 2011 17:18:27 +0000 (UTC), cindys
<cindys200@hotmail.com> wrote:

>On Jan 9, 6:12 am, Morris Goodman <goodman...@gmail.com> wrote:
>snip
>>
>> Why unfortunately?  Physical handicap is called disability because it
>> is physical disability:  someone confined to a wheelchair won't be
>> running many marathons.  Clouding it in PC-speak such as "physically
>> challenged" is meaningless because such a person cannot overcome the
>> challenge.
>snip
>---
>Often, they can. They are "challenged" because they find it more
>challenging to engage in certain activities, the normal activities of
>daily living (for example) that other people can do with ease, and
>when a disabled person is able to accomplish certain things in spite
>of his disability, he has overcome the "challenge."

When they're confined to a wheelchair, the "challenge" to run a
marathon is overwhelming and therefore overcoming it is not even a
remote possibility. They're disabled because they're not able to do
certain things.

There are, of course, those who promote PC-speak on behalf of those
'challenged" even when the challengees themselves don't approve of the
terms.

But to return to the Jewish subject matter, do you approve of a
restaurant or club being able to refuse admission to anyone without
giving a reason, when such policies can effectively be used to exclude
blacks, Jews etc?

cindys

1/9/2011 9:35:00 PM

0

On Jan 9, 1:25 pm, Morris Goodman <goodman...@gmail.com> wrote:
> On Sun, 9 Jan 2011 17:18:27 +0000 (UTC), cindys
snip
>
> But to return to the Jewish subject matter, do you approve of a
> restaurant or club being able to refuse admission to anyone without
> giving a reason, when such policies can effectively be used to exclude
> blacks, Jews etc?  
----
I don't know what goes on in Britain, but in the USA, if it becomes
apparent that a club is discriminating on the basis of race or
religion, they are breaking the law, and they will be in lots of
trouble, shut down, fined etc.

In the 1970s/1980s, Club 54 in NYC used to allow/refuse admission to
people on the basis of how they were dressed, but if it ever became
apparent that they were consistently turning away black people, for
example, they would have been breaking the law against discrimination,
and it probably would have cost them big bucks. As far as Jews are
concerned, nobody can tell who is Jewish on the basis of appearance
(unless someone is wearing a kippah, for example), and then the same
thing would hold true.

So, in the USA, these policies cannot effectively be used to exclude
blacks and Jews.
Best regards,
---Cindy S.

sheldonlg

1/9/2011 10:02:00 PM

0

On 1/9/2011 4:25 PM, Morris Goodman wrote:
> On Sun, 9 Jan 2011 17:18:27 +0000 (UTC), cindys
> <cindys200@hotmail.com> wrote:
>
>> On Jan 9, 6:12 am, Morris Goodman<goodman...@gmail.com> wrote:
>> snip
>>>
>>> Why unfortunately? Physical handicap is called disability because it
>>> is physical disability: someone confined to a wheelchair won't be
>>> running many marathons. Clouding it in PC-speak such as "physically
>>> challenged" is meaningless because such a person cannot overcome the
>>> challenge.
>> snip
>> ---
>> Often, they can. They are "challenged" because they find it more
>> challenging to engage in certain activities, the normal activities of
>> daily living (for example) that other people can do with ease, and
>> when a disabled person is able to accomplish certain things in spite
>> of his disability, he has overcome the "challenge."
>
> When they're confined to a wheelchair, the "challenge" to run a
> marathon is overwhelming and therefore overcoming it is not even a
> remote possibility. They're disabled because they're not able to do
> certain things.

They may not be able "run" marathons, but there are hundreds or
thousands of them them that "wheelchair" marathons. Just think of the
arm strength needed for that!

>
> There are, of course, those who promote PC-speak on behalf of those
> 'challenged" even when the challengees themselves don't approve of the
> terms.

You may not know this being in Britain, but there are many jokes about
that terminology such as "vertically challenged" for "short" and things
like that. IOW, it is often used tongue-in-cheek. When used correctly
it refers to doing things in a different way but getting to the same
objective, albeit with greater difficulty (see above about the marathon
for an example).

>
> But to return to the Jewish subject matter, do you approve of a
> restaurant or club being able to refuse admission to anyone without
> giving a reason, when such policies can effectively be used to exclude
> blacks, Jews etc?
>

No.

--
Shelly

Morris Goodman

1/9/2011 10:22:00 PM

0

On Sun, 9 Jan 2011 21:35:24 +0000 (UTC), cindys
<cindys200@hotmail.com> wrote:

>On Jan 9, 1:25 pm, Morris Goodman <goodman...@gmail.com> wrote:
>> On Sun, 9 Jan 2011 17:18:27 +0000 (UTC), cindys
>snip
>>
>> But to return to the Jewish subject matter, do you approve of a
>> restaurant or club being able to refuse admission to anyone without
>> giving a reason, when such policies can effectively be used to exclude
>> blacks, Jews etc?  
>----
>I don't know what goes on in Britain, but in the USA, if it becomes
>apparent that a club is discriminating on the basis of race or
>religion, they are breaking the law, and they will be in lots of
>trouble, shut down, fined etc.

If anything, Britain is bending over backwards to accommodate
minorities. The diversity industry is positively thriving.

>In the 1970s/1980s, Club 54 in NYC used to allow/refuse admission to
>people on the basis of how they were dressed, but if it ever became
>apparent that they were consistently turning away black people, for
>example, they would have been breaking the law against discrimination,
>and it probably would have cost them big bucks. As far as Jews are
>concerned, nobody can tell who is Jewish on the basis of appearance
>(unless someone is wearing a kippah, for example), and then the same
>thing would hold true.

Without in any way wishing to start a whole new discussion, some
people are visibly Jewish regardless what they are wearing. Which is
not to say that all Jews are.

>So, in the USA, these policies cannot effectively be used to exclude
>blacks and Jews.

I think they can, provided it is done carefully, and I would be
surprised if it never happened. You may, for instance, allow in a few
token blacks or Jews and refuse admission to the rest. Then nobody
can accuse you of discriminating on the basis of race or religion.

cindys

1/9/2011 11:13:00 PM

0

On Jan 9, 2:21 pm, Morris Goodman <goodman...@gmail.com> wrote:
snip
>
> Without in any way wishing to start a whole new discussion, some
> people are visibly Jewish regardless what they are wearing.  Which is
> not to say that all Jews are.

Can you please elaborate on how some people are visibly Jewish
regardless of what they are wearing? How does a Jew look?

>
> >So, in the USA, these policies cannot effectively be used to exclude
> >blacks and Jews.
>
> I think they can, provided it is done carefully, and I would be
> surprised if it never happened.  You may, for instance, allow in a few
> token blacks or Jews and refuse admission to the rest.  Then nobody
> can accuse you of discriminating on the basis of race or religion.

Yeah, that probably happens. So? If the injured party believes he has
been discriminated against for racial or religious reasons, he can sue
the owner of the establishment, and he may lose or win the case
depending on how well the injured party is able to present his/her
case to the judge. So, I don't understand what's your point? That an
owner of an establishment should be obligated to let everybody in, no
matter what?
Best regards,
---Cindy S.

cindys

1/10/2011 1:42:00 AM

0

On Jan 9, 4:25 pm, Morris Goodman <goodman...@gmail.com> wrote:
> On Sun, 9 Jan 2011 23:13:18 +0000 (UTC), cindys
>
> <cindys...@hotmail.com> wrote:
> >On Jan 9, 2:21 pm, Morris Goodman <goodman...@gmail.com> wrote:
> >snip
>
> >> Without in any way wishing to start a whole new discussion, some
> >> people are visibly Jewish regardless what they are wearing.  Which is
> >> not to say that all Jews are.
>
> >Can you please elaborate on how some people are visibly Jewish
> >regardless of what they are wearing? How does a Jew look?
>
> Ah, I see a new discussion starting regardless.
>
> It's one of those things that's easier to recognise than to define.
> Perhaps it takes one to know one.  I can give you a list of famous
> people who, were you to look at them, you would instantly recognise as
> Jewish.  Put simply, we are an ethnicity as well as a religion.  Each
> ethnicity tends to have certain distinctive features.

Well, what are these distinctively "Jewish" features?


> Of course, not
> all Jews are ethnically identifiable, thanks to outmarriage etc, and
> we have the added complication of converts from various ethnicities.


>
> >> >So, in the USA, these policies cannot effectively be used to exclude
> >> >blacks and Jews.
>
> >> I think they can, provided it is done carefully, and I would be
> >> surprised if it never happened.  You may, for instance, allow in a few
> >> token blacks or Jews and refuse admission to the rest.  Then nobody
> >> can accuse you of discriminating on the basis of race or religion.
>
> >Yeah, that probably happens. So? If the injured party believes he has
> >been discriminated against for racial or religious reasons, he can sue
> >the owner of the establishment, and he may lose or win the case
> >depending on how well the injured party is able to present his/her
> >case to the judge.
>
> Didn't you say suggest earlier that this was a criminal matter rather
> than a civil one?  Who is going to fund such a civil suit?

I believe it is a civil suit, yes, although someone can correct me. If
someone decides to sue, the person needs to hire an attorney and pay
the bill himself. But one can make arrangements with an attorney such
that the attorney does not get paid unless there is a monetary award,
and then he/she gets a certain percentage (which is generally quite
hefty).


>
> >So, I don't understand what's your point? That an
> >owner of an establishment should be obligated to let everybody in, no
> >matter what?
>
> If that's not the case, then discrimination on the basis of race or
> religion always remains a real possibility.  

So, it does...(shoulder shrug).
Best regards,
---Cindy S.