Deleting a Block
So I am creating a game in pygame
I was wondering how to delete the block in class Level_01.
DudeHexafandra
[deleted]
Example
level.pop(1) #This remove's place 2 in the array (we count from 0 in Python)
51LV3RC0D3R
@ZhongRietveld I don't get what you are saying. I tried screen.pop(1) because I couldn't use level as I was in line 291 in the function main(). So when I tried running it, it said that 'pygame.Surface' has no attribute pop.
[deleted]
@DudeHexafandra No, you use the varible name from the array so,
level.pop(1)
[deleted]
@ZhongRietveld Is this what you neat to let is work as you want to?
51LV3RC0D3R
@ZhongRietveld I tried it and I think I get what you are doing, but the problem is that I am writing my
level.pop(1)
in
def main():
and it says that level is undefined.
[deleted]
@DudeHexafandra that is, because its a local var so you have to return it
#return level
#Use the function with the return
new_level=<class_name>.<function_name()
#function name is option, because the init()
#Is always executed
[deleted]
@ZhongRietveld If I am right.
51LV3RC0D3R
@ZhongRietveld So just to make sure I got what you said the
<class_name>
is Level.02 and the
function_name()
is what exactly in this case?
Sorry, i am just a little new to pygame.
[deleted]
@DudeHexafandra A bit what i mean is that you use <funtcion_name> from a class only when the builtin function is not init().
[deleted]
With
level.pop()
For get what i say, I know what you need to do.
You know that you have 3 lists, so we can remove a list in "level".
Just copy and past and you're good to go!
This is for line 177 and 191.
then I do
but the level.pop(1) is in class Level_01(Level): so i am kinda stuck.
since that is where the portal is.
But anyway her is the solution to your problem.
Global constants
Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
Screen dimensions
SCREEN_WIDTH = 965
SCREEN_HEIGHT = 430
class Player(pygame.sprite.Sprite):
""" This class represents the bar at the bottom that the player
controls. """
class Platform(pygame.sprite.Sprite):
""" Platform the user can jump on """
class Level(object):
""" This is a generic super-class used to define a level.
Create a child class for each level with level-specific
info. """
Create platforms for the level
class Level_01(Level):
class Level_02(Level):
def init(self, player):
# Call the parent constructor
Level.init(self, player)
# Array with width, height, x, and y of platform
level = [[210, 70, 500, 500],
[210, 70, 300, 400],
[210, 70, 600, 200],
]
def main():
""" Main Program """
pygame.init()
if name == "main":
main()