1

For context: I am making a simple python game where you can walk around and collect berries. My whole code looks like this if you wants to test it.

import keyboard
import time
import emoji
import random

def assignstring():
    print("~~~~~~~~~~~~~~~~")
    s = ""
    bers1 = random.randint(0, 22)
    bers2 = random.randint(0, 10)
    print(bers1, bers2)
    for a in range(9):
        for i in range(21):
            if i == posx and a == posy:
                s = s + emoji.emojize(":bear:")
            elif a == bers1 and i == bers2:
                s = s + "!"
            else:
                s = s + emoji.emojize(":prohibited:")
        print(s)
        s = ""
    print("~~~~~~~~~~~~~~~~")

def poschange(posx, posy):
    if keyboard.is_pressed("left"):
        posx = posx - 1
        time.sleep(0.1)
    if keyboard.is_pressed("right"):
        posx = posx + 1
        time.sleep(0.1)
    if keyboard.is_pressed("down"):
        posy = posy + 1
        time.sleep(0.1)
    if keyboard.is_pressed("up"):
        posy = posy - 1
        time.sleep(0.1)
    if posx > 20:
        posx = 20
    if posx < 0:
        posx = 0
    if posy < 0:
        posy = 0
    if posy > 8:
        posy = 8
    return posx, posy

string = ""
posx = 10
posy = 4
c = 11
savex = 0
savy = 0
assignstring()


while True:
    savex = posx
    savey = posy
    posx = (poschange(posx, posy))[0]
    posy = (poschange(posx, posy))[1]
    if savex != posx:
        assignstring()
        print(posx, posy)
    if savey != posy:
        assignstring()
        print(posx, posy)

I wanna make a function that replaces one of the prohibited emoji with a berry emoji. Here is some psuedo code i wrote:

def berryspawn():
    berrycord1 = random.randint(0, 10)
    berrycord1 = random.randint(0, 22)
    #saves the coordinates and replaces the emoji at that location with a berry emoji.

I want it to save the coordinates so it can track if the player is touching the berry or not. What would be the easiest way to do this??

Regards Mike

Miklath
  • 55
  • 1
  • 6
  • Can't you just store it as a global var? – kabooya Mar 14 '21 at 16:22
  • I suppose for the coordinate part, but what about replacing the replacing part... and: wouldn't it be easier to return the coordinates – Miklath Mar 14 '21 at 16:24
  • Yeah, that's also a very good solution! You can change global variables inside a function btw (see, https://www.w3schools.com/python/python_variables_global.asp) – kabooya Mar 14 '21 at 16:29

1 Answers1

0

I can help you with the changing a random character in string part of it, I think.

Here's what I'd do, worked through step by step (tried and tested):

import random # need to use randints

mystring = "foo" # this'd be your string


# Now to get the string length so we can get a random char from it. First to 
# turn it to a list, as individual characters in strings can't be changed.

mystring_list = list(mystring)

mystring_list[random.randint(0, (len(mystring_list) - 1))] = # add your new 
# character value here

# Finally you need to change the list back to its string form. We'll also 
# remove all []s and 's plus spaces and commas so it's back to complete string 
# format.

mystring = str(mystring_list).replace('[', "").replace(']', "").replace("'", 
"").replace(",", "").replace(" ", "")

I think this sorts it. Breakdown of the process:

  1. mystring is turned to a list of all its characters, mystring_list
  2. With help from Random module we get a random item in mystring_list
  3. Then that item is changed.
  4. mystring is then set to a string of mystring_list with brackets/commas/spaces/apostrophes/quotes removed. So basically a string.

The only thing wrong with this is that if you had spaces in the original string then they will be removed at the end (step 4). If you don't mind odd spaces in the original string after every char then you can remove .replace(" ", ""), and original spaces will be retained.

Hope I am of help!

Viggo BF
  • 26
  • 6
  • see also https://stackoverflow.com/questions/1228299/changing-one-character-in-a-string for more info on modifying strings – Viggo BF Mar 14 '21 at 17:12
  • This is really close to what I'm looking for, but there are two problems 1. the output looks something like this: \n\n\n\n!\n\n\n\n 2. It needs to save the coordinate of which character it replaces – Miklath Mar 14 '21 at 17:22
  • 1) to get the character that was changed, set a variable (calling it changed_int) to `random.randint(0, (len(mystring_list) - 1))`. 2) then, when you change the character in the list, use `[changed_int]` instead of `[random.randint(0, (len(mystring_list) - 1))]` (else it'll get a new number; you want the same number). hope this helps. – Viggo BF Mar 14 '21 at 17:45
  • for the first problem, i honestly don't know what happened. perhaps, could you tell me what the original string looks like, and does it contain emojis? – Viggo BF Mar 14 '21 at 17:48
  • Update: I fixed the \n string problem. – Miklath Mar 14 '21 at 17:50
  • Also is there a way to get the line number the changed letter is on? – Miklath Mar 14 '21 at 17:52