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)
My problem is that in asignstring it only inerts berries some times and not every time.
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("~~~~~~~~~~~~~~~~")
The insert berry part is here elif a == bers1 and i == bers2: How can i make it spawn a berry at a random place every single time?
Regards Mike
Aucun commentaire:
Enregistrer un commentaire