pygame - How do people make a Level in a game with a list of strings in python -


i know because making games in pygame , use method because looks neat , tidy.

exe

level = "wwwwwwww" \         "wssssssw" \         "wssssssw" \         "wssssssw" \         "wssssssw" \         "wwwwwwww" 

here s's empty space , w's blocks

if use method, need define dimensions of level - levelwidth , levelheight, in case 8 , 6, can find block @ x, y code:

levelwidth = 8 levelheight = 6  def getblock(x, y):      return level[x * levelwidth + y] 

remembering indexes x , y both start @ 0 , end last item @ index length - 1 because of python's method of indexing.

here different method have used in situations this:

level = ['wwwwwwww',          'wssssssw',          'wssssssw',          'wssssssw',          'wssssssw',          'wwwwwwww'] 

with method need find block @ (x, y) is:

level[x][y] 

but remember if want able edit level, need create new string each row edit, because cant assign items string. e.g: set block @ x, y string new:

level[y] = level[x][:y] + new + level[x][y+1:] 

see https://docs.python.org/3/tutorial/introduction.html#strings more information on indexing method have shown based off of examples in book http://inventwithpython.com/pygame/ , example of levels in use in book in chapter 9 - star pusher


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -