-3

I have another question related to this game, here it is: https://stackoverflow.com/questions/28545444/python-3-game-bug-message-repeating

I made a game thats a knock off of Cookie Clicker (just for coding practice!). And I ran into a problem where the user can hold down the enter key, and get coins really fast. I'd like to prevent that. here is my code:

coin = 0
keepgoing = True

while keepgoing == True:
        print('You have ' + str(coin) + ' cmd-coins' )
        response = input('Press enter to get a coin!')
        if response == 'q':
                keepgoing = False
                print('Thanks for playing!')
coin = coin + 1
5
  • Press enter to get a coin... you hit enter really fast and you get coins really fast... uhh, what is supposed to be happening? (Shouldn't the line incrementing coin be within the loop?) Feb 13, 2015 at 16:12
  • @Two-BitAlchemist Could his problem be that he wants someone to have to repeatedly press down the Enter key, and currently the easiest way to win the game is simply to hold the key down? Feb 13, 2015 at 16:26
  • Also, slight nitpick: while keepgoing == True is normally written while keepgoing. (== True is implied by the while keyword in the first place.) Feb 13, 2015 at 16:40
  • @Two-BitAlchemist Take a look at my answer to see what I mean. Feb 13, 2015 at 18:15
  • Sorry if i wasn't clear enough, i would like the user to press the enter key repeatedly instead of just holding it down.
    – GForcedog
    Feb 16, 2015 at 16:05

3 Answers 3

2

I wrote a rather "gimmicky" solution to your problem, which works to an extent but definitely isn't flawless. Based on your compiler you may have to change the conditional value for start-end such that it is a value which allows you to static press as fast as humanly possible without breaking but not allow the user to hold the enter key. For me in WingIde, 0.03 is the optimal value.

Were you to import a module such as Pygame you would find this sort of game much easier to make.

import time

coins = 0
keepgoing = True
end = -1

while keepgoing:
    print('You have ' + str(coins) + ' cmd-coins' )
    response = input('Press enter to get a coin!')
    start = time.time()
    if start-end < 0.03:
        print ("Don't hold down the Enter key you cheater!")
        keepgoing = False
    if response == 'q':
        print('Thanks for Playing!')
        keepgoing = False
    coins = coins + 1
    end = time.time()

NOTE: This solution is for Python 3, for < Python 3 you'll have to substitute raw_input() for input(). Also, I wouldn't advise trying to use it on an online compiler.

0

G Force dog, You could change your game a bit by making a few changes till someone or I figure out a good solution(Mom gave orders to go to bed). Till we don't find a solution, use this code which isn't exactly a solution.

coins = 0
real = 0
while real == 0:
    if True:
        print('You have ' + str(coins) + ' cmd-coins' )
        response = input('Press c to get a coin, then press ENTER!')
        if response == 'c' or response == 'C':
            coins = coins + 1
        if response == 'q':
            print('Thanks for playing!')
            real = 1

And 2 things:-

  1. Nice answer Yeniaul! Good try!
  2. Elizion, nice try but your method isn't working. But it is a good idea. I have used time too, but it seems that when one presses ENTER key for a long time, it just comes out on the screen all together.

I think I do have somewhat of an idea forming... if we make another while loop, after importing 'time', with a condition which makes the loop active every 2 seconds by assigning a variable 'time.clock()' and when its value is a multiple of 2, it activates! Got it? I know what to do, but can't seem to put it in code. See if you can, reader(especially Elizion and Yeniaul)

-1

Try using time.sleep(seconds of delay) for slowdown So you could do

coin = 0
keepgoing = True

while keepgoing == True:
        time.sleep(0.25)
        print('You have ' + str(coin) + ' cmd-coins' )
        response = input('Press enter to get a coin!')
        if response == 'q':
                keepgoing = False
                print('Thanks for playing!')
coin = coin + 1

This would make it a quarter-second delay before they could get another coin.

6
  • The game would still be broken. Holding down the Enter key would still be the easiest way to accumulate coins. Feb 13, 2015 at 17:31
  • You definitely did not try this code before you posted it. Feb 13, 2015 at 17:32
  • That's hardly a solution. Feb 13, 2015 at 17:40
  • Plus, just your code throws an error when pressing enter. i get "Unexpected EOF"
    – Yeniaul
    Feb 13, 2015 at 17:47
  • @Yeniaul Your "answer" contains errors thoughtlessly copied and pasted from the OP's question, and your issue with Elizion's code is that you didn't read his answer: it only works on Python 3 and you have to adjust the code to run it on Python 2. Feb 13, 2015 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.