简体   繁体   English

用另一个字符替换字符串的随机字符

[英]Replacing a random character of string with another character

For context: I am making a simple python game where you can walk around and collect berries.对于上下文:我正在制作一个简单的 python 游戏,您可以在其中四处走动并收集浆果。 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.我想做一个 function 用浆果表情符号替换一个被禁止的表情符号。 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问候迈克

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 mystring 变成一个包含所有字符的列表,mystring_list
  2. With help from Random module we get a random item in mystring_list在 Random 模块的帮助下,我们在 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.然后将 mystring 设置为 mystring_list 的字符串,其中括号/逗号/空格/撇号/引号已删除。 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).唯一的问题是,如果原始字符串中有空格,那么它们将在最后被删除(步骤 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.如果您不介意每个字符后原始字符串中的奇数空格,那么您可以删除.replace(" ", "") ,原始空格将被保留。

Hope I am of help!希望我能有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM