im currently working a little text based rpg game and im making a leveling system, and i dont know how to always run a command so it always checks how much xp a player has.
heres the code so far
xp = 0
lvl = 1
if (xp >= lvl * 10):
print("Level Up!")
lvl = 2
xpup = int(input("Command: "))
xp = xpup
i want to make it so the if statement is always running. How do i do that?
Related
I am super new to Python. I've been taking a Python Course on Coursera and fooling around with a little bit of code. Prior to today I was doing the class on a chromebook, and therefore using the sandbox tool provided. I've purchased an actual PC to download python since. I wrote a little code that was working just fine in the sandbox tool, but when I enter it into python it keeps booting me out. Can anyone spot anything obviously wrong with my code that I should change?
It should take an input of a date, exercise, and then keep looping through asking you about your sets and reps, saving results as tuples in a dictionary, until you enter 0, where it exits the loop, and prints the results. Stumped!
dt = input("Date:")
ex = input("Exercise:")
d = dict()
set = 0
while True:
wrk = input("Insert as Reps x Weight: ")
if wrk == "0":
break
set = set + 1
d[set] = wrk
print(dt)
print(ex)
print(d)
Indentation is really important in Python since it uses whitespace to differentiate between blocks of code.
Here,
observe the indentation of the while statement. Note that the indentation in the if block remains the same. This is because we want break to execute only if wrk is 0.
On the other hand, we keep set+1 outside because the condition did not match and we wanted our program to keep running.
python
dt = input("Date:")
ex = input("Exercise:")
d = dict()
set = 0
while True:
wrk = input("Insert as Reps x Weight: ")
if wrk == "0":
break
set = set + 1
d[set] = wrk
print(dt)
print(ex)
print(d)
This works as expected.
I developed this script in SIKULI, but I would like to add more functions and I'm thinking about using Python and learning the language.
SIKULI has OCR but still contains some bugs, not performing so well. Likewise I would like to use OCR/tesseract/pytesseract in python.
The idea of this script is to make you heal in a game, according to the current % of health, reading the number of the current health bar vs the total value it has.
After reading this number, a calculation is performed with the result in %.
Could you help me fix this script by inserting the necessary libraries and tweaking the code to make it work in Python?
#==Config HP==
Healing_1 = 95
Hk_healing_1 = '5'
Healing_2 = 65
Hk_healing_2 = '1'
Healing_3 = 30
Hk_healing_3 = '3'
#==Code==
while True:
GAME = App("GAME")
GAME.focus()
Settings.OcrTextRead = True
#==hp==
hp = Region(88,28,191,23)
hp = hp.text()
hp2 = hp.replace(",","")
X = hp2.split("/")
try:
check_hp = float(X[0]) / float(X[1]) * 100
except:
type(Hk_healing_3)
#==hp check==
if check_hp < 100:
if check_hp <= Healing_3:
type(Hk_healing_3)
elif check_hp <= Healing_2:
type(Hk_healing_2)
elif check_hp <= Healing_1:
type(Hk_healing_1)
else:
pass
Just for information, this function should identify the text in a region of the screen. The text of the screen, will be displayed as xxxx/xxxx (current value / total value) - it's could be other format as x/x, xx/xx, xxx/xxx, xxxx/xxxx, xxxxx/xxxxx, x/xxxx, x/xxxxxxx ...
The text will be split to perform the calculation (% of current value).
I'm trying to create a snake algorithm that beats the game on its own, I've gone for the Hamiltonian cycle method with the capability of creating shortcuts, I was trying to create the cycle by using a pathfinding algorithm and compute the longest path where the head of the snake is the start, the tail the end and the 2 blocks in between are walls, the pathfinding library in python uses a matrix to represent the map of pixels, so to generate my matrix I use this block of code:
from pathfinding.core.grid import Grid
from pathfinding.finder.a_star import AStarFinder
from pathfinding.core.diagonal_movement import DiagonalMovement
matrix=[]
row=[]
yi=0
xi=0
while yi<800:
if row != []:
matrix.append(row)
yi += 20
row.clear() #This causes crash
while xi<1400:
row.append(1)
if xi == 40 or xi == 60:
if yi == 20:
row.append(0)
xi += 20
grid = Grid(matrix=matrix)
start = grid.node(4, 1)
end = grid.node(1, 1)
finder = AStarFinder(diagonal_movement=DiagonalMovement.always)
path, runs = finder.find_path(start, end, grid)
print('operations:', runs, 'path length:', len(path))
print(grid.grid_str(path=path, start=start, end=end))
But whenever I run it, it crashes,
I've narrowed it down to the line
row.clear()
but I have no clue why it does this, if I remove it no walls are created, other methods of emptying the list like :row=[] or
while i<len(row):
row.remove(i)
row += 1
give me the same result, I get no error message, nothing prints it just crashes, its even more clear on the entire code because the window displaying the game of snake doesn't display anything and the windows crash window appears, I'm using Windows 10, python 3.8.2
I'm quite new to programming so please excuse my inefficient code, I do it for fun and performance brings me little pleasure, any help is greatly appreciated
I hope I didn't miss anything obvious making you waste time but as long as my code is fixed I'm a happy chappy.
Thanks
Turns out I'm really stupid and forgot to add xi = 0 so it could start the indented loop again instead of just looping into infinity
I'm pretty much blind when getting crashes like what is the problem? When the program is at the state of rabbits spawning here
rabbitCounter += 1
if rabbitCounter >= NEW_RABBIT:
rabbitCounter = 0
rabbits.append(pygame.Rect(random.randint(0, WINDOW_WIDTH
- RABBIT_SIZE), random.randint (0, WINDOW_HEIGHT - RABBIT_SIZE),
RABBIT_SIZE, RABBIT_SIZE))
It chrashes when I press the space bar to go to the other loop, level 2. I did this so I
could test the game fine but it seems not to work. Hopefully the problem gets found. It hangs the window gets greyed and the "Program has crashed dialog" comes up.
Here's all the files and py.files: https://www.dropbox.com/sh/gqke6hfooz7mbnr/Qm8NMlyNqc
It seems to be these two sections if I take a guess:
#Gris spawning
while pigSpawn == True:
pigCounter += 1
if pigCounter >= NEW_PIG:
pigCounter = 0
pigs.append(pygame.Rect(random.randint(0, WINDOW_WIDTH
- PIG_SIZE), random.randint (0, WINDOW_HEIGHT - PIG_SIZE),
PIG_SIZE, PIG_SIZE))
#Vildsvin spawning
while boarSpawn == True:
boarCounter += 1
if boarCounter >= NEW_BOAR:
boarCounter = 0
boars.append(pygame.Rect(random.randint(0, WINDOW_WIDTH
- BOAR_SIZE), random.randint (0, WINDOW_HEIGHT - BOAR_SIZE),
BOAR_SIZE, BOAR_SIZE))
In level 1 the programmer created 20 pigs, 20 boars etc.. then setup a while-loop that handled events. In that single loop it handled all events (to move the game). Spawned pigs/boars as needed etc..
The code in level 2 just contains a never ending while loop that add more and more pigs.
Probably until you run out of memory. Basically once you are in the pigSpawn loop you just continue to loop forever adding more and more pigs until the program crashes.
the fallowing is a tic tak toe game code in python, can some one show me how i can make it in GUI form with a reset option and shows who wins at the end. like X win or O win?
board = " 1 | 2 | 3\n-----------\n 4 | 5 | 6\n-----------\n 7 | 8 | 9"
.
checkboard=[1,2,3,4,5,6,7,8,9,1,4,7,2,5,8,3,6,9,1,5,9,3,5,7]
spaces=range(1,10)
def moveHandler(board,spaces,checkboard,player,n):
if player==1:
check="X"
else:
check="O"
while spaces.count(n)==0:
print "\nInvalid Space"
n=playerinput(player)
spaces=spaces.remove(n)
board=board.replace(str(n),check)
for c in range(len(checkboard)):
if checkboard[c]==n:
checkboard[c]=check
status = checkwinner(checkboard,check)
return board,status
def checkwinner(checkboard,check):
a,b,c=0,1,2
while a<=21:
combo = [checkboard[a],checkboard[b],checkboard[c]]
if combo.count(check) == 3:
status =1
break
else:
status =0
a+=3
b+=3
c+=3
return status
def playerinput(player):
try:
key = int(raw_input('\n\nPlayer ' + str(player) + ': Please select a space '))
except ValueError:
print "Invalid Space"
key = playerinput(player)
return key
while True:
player = len(spaces)%2 +1
if player == 1:
player = 2
else:
player =1
print "\n\n" + board
key = playerinput(player)
board,status =moveHandler(board,spaces,checkboard,player,key)
if status == 1:
print '\n\nPlayer ' + str(player) + ' is the winner!!!'
print board
break
elif len(spaces)==0:
print "No more spaces left. Game ends in a TIE!!!"
print board
break
else:
continue
Clearly you need to choose a GUI toolkit (Python supports many of them), use it to paint the board as a 3 x 3 grid of squares, and change the playerinput function to accept input by (e.g.) the current player double clicking on the empty square he or she wants to play in.
Then, you need to change the print statements to show information on the GUI surface.
The game however would be much better if it didn't try to control the flow of events but rather responded to events initiated by the players -- that's how real GUI apps should be done, rather than by minimal retrofitting of some interface on top of what's intrinsically designed as a command-line interactive procedure.
Each of these tasks is a substantial one, especially the overall refactoring I recommend in the last paragraph, and entirely depends in its details on what GUI toolkit you choose -- so you might want to start with that, and then generally break the question up into the various subtasks ("one question per question", since many of the latter may arise;-).
There are several SO questions on the subject of GUI choice for Python, so I recommend you study them rather than asking a new one. My personal favorite is PyQt (though more and more often I just do a simple browser-based interface with a local-only server powering it), but other popular ones include wxPython, Tkinter, PyGtk, and others listed here -- happy hunting!
Check out the python wiki for info about different GUI toolkits. I would recommend looking at wxPython, and going from there