How to change the value of a variable in a class [Python] - python-3.x

I want to add a function that updates the value of the self.ScoreP variable
I have a number on the screen, which prints out the current value of the ScoreP (updateScoreBoard()). It is accurate and works perfectly, but I am also printing out getScoreP. ScoreP prints 0 no matter what the score currently is.
import pygame
class ScoreBoard():
def __init__(self):
self.WIDTH = 1024
self.HEIGHT = 576
self.WHITE = (255, 255, 255)
self.BLACK = (0,0,0)
self.minFont = "font/Minecraft.ttf"
self.scoreFont = pygame.font.Font(self.minFont, 75)
self.ScoreP = 0
self.ScorePStr = str(self.ScoreP)
self.ScoreO = 0
self.ScoreOStr = str(self.ScoreO)
self.ScorePWidth, self.ScorePHeight = self.scoreFont.size(str(self.ScoreP))
self.ScoreOWidth, self.ScoreOHeight = self.scoreFont.size(str(self.ScoreO))
self.ScorePX = (self.WIDTH/2)-self.ScorePWidth*2
self.ScorePY = 10
self.ScoreOX = self.WIDTH/2 + self.ScoreOWidth
self.ScoreOY = 10
def updateScoreBoard(self, screen):
pygame.draw.rect(screen, self.BLACK, [self.ScorePX, self.ScorePY, self.ScorePWidth, self.ScorePHeight])
scorePRender = self.scoreFont.render("{}".format(self.ScoreP), False, self.WHITE)
screen.blit(scorePRender, (self.ScorePX, self.ScorePY))
pygame.draw.rect(screen, self.BLACK, [self.ScoreOX, self.ScoreOY, self.ScoreOWidth, self.ScoreOHeight])
scoreORender = self.scoreFont.render("{}".format(self.ScoreO), False, self.WHITE)
screen.blit(scoreORender, (self.ScoreOX, self.ScoreOY))
pygame.display.flip()
def updateScore(self, playerIncrease, opponentIncrease):
self.ScoreP += playerIncrease
self.ScorePStr = self.ScoreP
self.ScoreO += opponentIncrease
self.ScoreOStr = self.ScoreO
def getScoreP(self):
return self.ScoreP
However, the getScore function prints out 0
Even though, the game properly keeps track of and redraws the score
Thank you in advance

Here I don't think I changed much, but it works. Also next time give us your whole code including how you test it, because the problem may be there. I added a test at the end that you can delete.
import pygame
import random
class ScoreBoard:
def __init__(self,w,h):
pygame.font.init()
self.WIDTH = w
self.HEIGHT = h
self.screen = pygame.display.set_mode((1024,576))
self.WHITE = (255, 255, 255)
self.BLACK = (0,0,0)
self.minFont = None
self.scoreFont = pygame.font.Font(self.minFont, 75)
self.ScoreP = 0
self.ScoreO = 0
self.ScorePWidth, self.ScorePHeight = self.scoreFont.size(str(self.ScoreP))
self.ScoreOWidth, self.ScoreOHeight = self.scoreFont.size(str(self.ScoreO))
self.ScorePX = (self.WIDTH/2)-self.ScorePWidth*2
self.ScorePY = 10
self.ScoreOX = self.WIDTH/2 + self.ScoreOWidth
self.ScoreOY = 10
def updateScoreBoard(self):
self.ScorePWidth, self.ScorePHeight = self.scoreFont.size(str(int(self.ScoreP)))
self.ScoreOWidth, self.ScoreOHeight = self.scoreFont.size(str(int(self.ScoreO)))
screen = self.screen
screen.fill(self.WHITE)
pygame.draw.rect(screen, self.BLACK, [self.ScorePX, self.ScorePY, self.ScorePWidth, self.ScorePHeight])
scorePRender = self.scoreFont.render("{}".format(int(self.ScoreP)), False, self.WHITE)
screen.blit(scorePRender, (self.ScorePX, self.ScorePY))
pygame.draw.rect(screen, self.BLACK, [self.ScoreOX, self.ScoreOY, self.ScoreOWidth, self.ScoreOHeight])
scoreORender = self.scoreFont.render(str(int(self.ScoreO)), False, self.WHITE)
screen.blit(scoreORender, (self.ScoreOX, self.ScoreOY))
pygame.display.flip()
def updateScore(self, playerIncrease, opponentIncrease):
self.ScoreP += playerIncrease
self.ScoreO += opponentIncrease
def getScoreP(self):
return self.ScoreP
running = True
a = ScoreBoard(1024,576)
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
break
a.updateScore(random.random()/10,random.random()/10)
a.updateScoreBoard()

Related

AttributeError: 'list' object has no attribute 'x' in Python

I'm working on a basic game with python, but the system gives an error
impostors.x = new_x_pos
AttributeError: 'list' object has no attribute 'x'
Below is my code, can anyone tell me what I'm doing wrong? I've checked very carefully but still can't find the error, Since I'm a newbie, I don't have much experience so I hope you guys can help me
import pgzrun
import random
FONT_COLOR = (255, 255, 255) #màu RGB
WIDTH = 1300
HEIGHT = 700
CENTER_X = WIDTH / 2
CENTER_Y = HEIGHT / 2
CENTER = (CENTER_X, CENTER_Y)
START_SPEED = 10
COLORS = ["orange", "blue"]
current_level = 1
final_level = 5
game_over = False
game_complete = False
impostors = []
animation = []
def draw():
global impostors,current_level,game_over,game_complete
screen.clear()
screen.blit("dark",(0,0))
if game_over:
display_message("Game Over", "Press Space to play again")
elif game_complete:
display_message("You win", "Press Space to play again")
else:
for im in impostors:
im.draw()
def update():
global impostors,current_level,game_over,game_complete
if len(impostors) == 0:
impostors = make_impostors(current_level)
if (game_over or game_complete) and keyboard.space:
impostors = []
current_level = 1
game_complete = False
game_over = False
def make_impostors(number_of_impostors):
colors_to_create = get_colors_to_create(number_of_impostors)
new_impostors = create_impostors(colors_to_create)
layout_impostors(new_impostors)
animate_impostors(new_impostors)
return new_impostors
def get_colors_to_create(number_of_impostors):
colors_to_create = ["red"]
for i in range(0,number_of_impostors):
random_color = random.choice(COLORS)
colors_to_create.append(random_color)
return colors_to_create
def create_impostors(colors_to_create):
new_impostors = []
for color in colors_to_create:
impostor = Actor(color + "-im")
new_impostors.append(impostors)
return new_impostors
def layout_impostors(impostors_to_layout):
number_of_gaps = len(impostors_to_layout) + 1
gap_size = WIDTH/number_of_gaps
random.shuffle(impostors_to_layout)
for index, impostor in enumerate(impostors_to_layout):
new_x_pos = (index + 1)*gap_size
impostor.x = new_x_pos
def animation_impostors(impostors_to_animate):
for impostors in impostors_to_animate:
duration = START_SPEED - current_level
impostors.anchor = ("center", "bottom")
animation = animation(impostors, duration = duration, on_finished = handle_game_over, y = HEIGHT)
animations.append(animation)
def handle_game_over():
global game_over
game_over = True
def on_mouse_down(pos):
global impostors, current_level
for impostors in impostors:
if impostors.collidepoint(pos):
if "red" in impostors.image:
red_impostor_click()
else:
handle_game_over()
def red_impostor_click():
global current_level, impostors, animation, game_complete
stop_animations(animations)
if current_level == final_level:
game_complete = True
else:
current_level = current_level + 1
impostors = []
animations = []
def stop_animations(animations_to_stop):
for animation in animations_to_stop:
if animation.running:
animation.stop()
def display_message(heading_text, sub_heading_text):
screen.draw.text(heading_text, fontsize = 60, center = CENTER, color = FONT_COLOR)
screen.draw.text(sub_heading_text,
fontsize = 30,
center = (CENTER_X, CENTER_Y + 30),
color = FONT_COLOR)
pgzrun.go()
The problem is the loop for impostors in impostors:. Rename the loop variable to impostor:
def on_mouse_down(pos):
global impostors, current_level
for impostor in impostors:
if impostor.collidepoint(pos):
if "red" in impostors.image:
red_impostor_click()
else:
handle_game_over()

Pygame SysFont not rendering inside of function

I was making a game where you chop wood in a forest every day and was doing the function to destroy a tree after 3 seconds by putting a SysFont at the bottom of the screen, counting down from three seconds which by then it would remove the tree. I binded the key Q to chop down the first tree(for testing purposes), but instead I got no removal of the tree, and no SysFont counting down - all it did was lag the game because of the time.sleep(1) i had put in. The entire code is below:
import pygame
import time
pygame.init()
root = pygame.display.set_mode((603, 573))
pygame.display.set_caption("Homework")
window_is_open = True
white = (255, 255, 255)
black = (0, 0, 0)
width = 10
leaves_width = 30
height = 20
leaves_height = 10
x = 0
tree_trunk_x = 10
y = 0
tree_trunk_y = 10
vel = 5
brown = (150, 75, 0)
green = (58, 95, 11)
tree_one_property = True
tree_two_property = True
tree_three_property = True
tree_four_property = True
tree_five_property = True
tree_six_property = True
tree_seven_property = True
tree_eight_property = True
tree_nine_property = True
tree_ten_property = True
tree_eleven_property = True
tree_twelve_property = True
tree_thirteen_property = True
tree_fourteen_property = True
tree_fifteen_property = True
tree_sixteen_property = True
tree_seventeen_property = True
tree_eighteen_property = True
tree_nineteen_property = True
tree_twenty_property = True
tree_twenty_one_property = True
tree_twenty_two_property = True
tree_twenty_three_property = True
tree_twenty_four_property = True
tree_twenty_five_property = True
def create_tree(tree_x, tree_y, tree):
if tree:
trunk_x = tree_x + 10
trunk_y = tree_y + 10
pygame.draw.rect(root, brown, (trunk_x, trunk_y, width, height))
pygame.draw.rect(root, green, (tree_x, tree_y, leaves_width, leaves_height))
def destroy_tree(tree_property_name):
count = pygame.font.SysFont('Tahoma', 18, True, False)
countdown = count.render('3', True, (0, 0, 0))
root.blit(countdown, (590, 569))
pygame.display.update()
time.sleep(1)
count = pygame.font.SysFont('Tahoma', 18, True, False)
countdown = count.render('2', True, (0, 0, 0))
root.blit(countdown, (590, 569))
pygame.display.update()
time.sleep(1)
count = pygame.font.SysFont('Tahoma', 18, True, False)
countdown = count.render('1', True, (0, 0, 0))
root.blit(countdown, (590, 569))
pygame.display.update()
time.sleep(1)
tree_property_name = False
root.fill(white)
while window_is_open:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
window_is_open = False
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
x += vel
if keys[pygame.K_LEFT]:
x -= vel
if keys[pygame.K_UP]:
y -= vel
if keys[pygame.K_DOWN]:
y += vel
if keys[pygame.K_q]:
destroy_tree(tree_one_property)
root.fill(white)
font = pygame.font.SysFont('Tahoma', 18, True, False)
score = font.render('Score:', True, (0, 0, 0))
root.blit(score, (410, 0))
rectangle = pygame.draw.rect(root, (0, 0, 0), (x, y, width, 10))
tree_one = create_tree(0, 0, tree_one_property)
tree_two = create_tree(50, 0, tree_two_property)
tree_three = create_tree(100, 0, tree_three_property)
tree_four = create_tree(150, 0, tree_four_property)
tree_five = create_tree(200, 0, tree_five_property)
tree_six = create_tree(0, 50, tree_six_property)
tree_seven = create_tree(50, 50, tree_seven_property)
tree_eight = create_tree(100, 50, tree_eight_property)
tree_nine = create_tree(150, 50, tree_nine_property)
tree_ten = create_tree(200, 50, tree_ten_property)
tree_eleven = create_tree(0, 100, tree_eleven_property)
tree_twelve = create_tree(50, 100, tree_twelve_property)
tree_thirteen = create_tree(100, 100, tree_thirteen_property)
tree_fourteen = create_tree(150, 100, tree_fourteen_property)
tree_fifteen = create_tree(200, 100, tree_fifteen_property)
tree_sixteen = create_tree(0, 150, tree_sixteen_property)
tree_seventeen = create_tree(50, 150, tree_seventeen_property)
tree_eighteen = create_tree(100, 150, tree_eighteen_property)
tree_nineteen = create_tree(150, 150, tree_nineteen_property)
tree_twenty = create_tree(200, 150, tree_twenty_property)
tree_twenty_one = create_tree(0, 200, tree_twenty_one_property)
tree_twenty_two = create_tree(50, 200, tree_twenty_two_property)
tree_twenty_three = create_tree(100, 200, tree_twenty_three_property)
tree_twenty_four = create_tree(150, 200, tree_twenty_four_property)
tree_twenty_five = create_tree(200, 200, tree_twenty_five_property)
pygame.display.update()
pygame.quit()
It looks like you are drawing the font (almost) entirely off the bottom of the screen.The lines below:
root = pygame.display.set_mode((603, 573))
...
root.blit(countdown, (590, 569))
indicate that your screen has a height of 573 pixels. You are blit'ing your text with its top at 569, which is only 4 pixels onto the screen. In reality that likely means that the text is entirely off the bottom of the screen.
Try moving it a bit higher.
In fact you can actually get the size of the bounding box of the text surface countdown like this:
text_box_size = countdown.get_size()
See docs here. You can get the height and width of the text box like this and use that to determine the offset from the bottom of the screen you want to pass to blit().
I have edited this answer to add a response to the question in your comment asking why your tree was still rendering. This would be to much to add in a follow on comment.
In your code you have a method def destroy_tree(tree_property_name) and in it you try to update the variable tree_property_name and make it False. That would work if the variable was passed to the function as pass by reference, but in python that is not how arguments are passed. So when you set it to False inside the method all that does is change the value seen inside the scope of the method. The value seen by the caller of the function is not changed.
You can get more explanation by looking at the answer to How do I pass a variable by reference?:
Your code would be significantly cleaner if you made the trees into a class. It would also allow you to be able to change the internal values in the class when the class is passed into a method.

Trying to get ball to return to middle after ball has left screen

I'm trying to recreate a game of pong just for fun.
Here is my full code as of right now.
import pygame, random, math
pygame.init()
#colours:-------------------------------------------------------------
R = random.randrange(1,255)
B = random.randrange(1,255)
G = random.randrange(1,255)
WHITE = (255, 255, 255)
GREEN = (39, 133, 20)
YELLOW = (252, 252, 25)
BLACK = (0, 0, 0)
BLUE = (30, 100, 225)
RED = (255,0,0)
RANDOM_COLOR = (R, B, G)
#Surface:-------------------------------------------------------------
width = 700
height = 600
size = (width, height)
screen = pygame.display.set_mode(size)
screen_rect = screen.get_rect()
pygame.display.set_caption("Pong Remake")
background = pygame.image.load("background.png").convert()
background = pygame.transform.scale(background, (width, height))
logo = pygame.image.load("logo.png").convert()
logo.set_colorkey((BLACK))
credits = pygame.image.load("credits.png")
credits.set_colorkey((BLACK))
#variables:-----------------------------------------------------------
clock = pygame.time.Clock()
done = False
text = pygame.font.Font(None,25)
display_instructions = True
instruction_page = 1
start_font = pygame.font.Font("C:\Windows\Fonts\BAUHS93.TTF", 35)
instruction_font = pygame.font.Font(None, 17)
win_lose_font = pygame.font.Font("C:\Windows\Fonts\BAUHS93.TTF",50)
score = pygame.font.Font(None, 100)
bounce = pygame.mixer.Sound("bounce.wav")
playerOne_score = 0
playerTwo_score = 0
playerOne = ""
playerTwo = ""
x = 350
y = 300
ball_rect = pygame.Rect(x,y,10,10)
paddleOne_rect = pygame.Rect(10, 250, 20, 60)
paddleTwo_rect = pygame.Rect(670, 250, 20, 60)
x_speed = random.randrange(5, 10)
y_speed = random.randrange(5,10)
def draw_background(screen, pic, x,y):
screen.blit(pic, (x,y))
#main loop
#INPUT v ---------------------------------------------------------
#Start Page
while not done and display_instructions:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.MOUSEBUTTONDOWN:
instruction_page += 1
if instruction_page == 2:
display_instructions = False
#Shows the start of the page
if instruction_page == 1:
draw_background(screen, logo, 100,-150)
draw_background(screen, credits, 100,50)
instruction_text = instruction_font.render("How to Play. The objective to this game is to score the ball on the other side before the opponent can.", False, WHITE)
instruction_text_three = instruction_font.render("First Player to get 10 points wins, Have Fun and Good Luck!", False, WHITE)
instruction_text_two = instruction_font.render("For Player One, use the a and the z keys to move up and down, For Player Two, use the k and m keys.", False, WHITE)
continue_text= start_font.render("Click to Play...",True, WHITE)
screen.blit(continue_text, [200, 400])
screen.blit(instruction_text, [0,500])
screen.blit(instruction_text_three, [0,532])
screen.blit(instruction_text_two,[0,516])
if instruction_page == 2:
display_instructions = False
clock.tick(60)
pygame.display.flip()
while not done:
click = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.MOUSEBUTTONDOWN:
click = True
#INPUT ^ =========================================================
#PROCESS v -------------------------------------------------------
str(playerOne_score)
str(playerTwo_score)
scoreOne = text.render("Player One:" + str(playerOne_score), False, WHITE)
scoreTwo = text.render("Player Two:" + str(playerTwo_score), False, WHITE)
#moves paddles with keys on keyboar
key = pygame.key.get_pressed()
if key[pygame.K_a]: paddleOne_rect.move_ip(0, -10)
if key[pygame.K_z]: paddleOne_rect.move_ip(0, 10)
if key[pygame.K_k]: paddleTwo_rect.move_ip(0, -10)
if key[pygame.K_m]: paddleTwo_rect.move_ip(0, 10)
#makes sure paddles stay on screen
paddleOne_rect.clamp_ip(screen_rect)
paddleTwo_rect.clamp_ip(screen_rect)
ball_rect.move_ip(x_speed, y_speed)
if ball_rect.y + ball_rect.height> screen_rect.height or ball_rect.y < 0:
y_speed = y_speed * -1
bounce.play()
if ball_rect.collidelist([paddleOne_rect, paddleTwo_rect]) > -1:
x_speed = -x_speed
R = random.randrange(1,255)
B = random.randrange(1,255)
G = random.randrange(1,255)
bounce.play()
if ball_rect.x >= 700:
x_speed * -1
playerOne_score += 1
pygame.display.flip
if ball_rect.x <= 0:
x_speed * -1
playerTwo_score += 1
#PROCESS ^ =======================================================
#DRAWING GOES BELOW HERE v ------------------------------------
draw_background(screen, background, 0,0)
screen.blit(scoreOne, (0,0))
screen.blit(scoreTwo, (500,0))
pygame.draw.ellipse(screen, WHITE,ball_rect )
pygame.draw.rect(screen,RANDOM_COLOR, paddleOne_rect)
pygame.draw.rect(screen,RANDOM_COLOR, paddleTwo_rect)
pygame.draw.line(screen, WHITE, (350,0),(350,700), 1)
#DRAWING GOES ABOVE HERE ^ ------------------------------------
pygame.display.flip()
clock.tick(60)
pygame.quit()
What I am currently having problems with at the moment is when the ball goes off the screen, I want it to go back to the middle again as someone has scored a point. But I'm a bit stuck on what to do.
If you guys can help me out, that would be amazing!!
There is a lot of code here, so this does not follow specific variables you used, but I hope this helps.
1) Find the width of you screen
2) Take the x and y coordinates that you use to know where to draw the ball
3) Make an if statement that essentially says
(pseudocode)
if x > 1000
score1 += 1
x = 500
if x < 0
score2 += 1
x = 500
``
I hope this can set you on the right track, and I suggest checking out the pygame docs.
Cheers!

List in object gets overwritten

After some work I finished my first algorithm for a maze generator and now I am trying to make it visual in Pygame. I first let the algorithm generate a maze and then I make a visual representation of it.
Here I get into multiple problems, but I think they are all linked to the same thing which is that the first cell of the maze gets overwritten in some way. Because of this, what I get is totally not a maze at all but just some lines everywhere.
I tried putting the removal of walls in a seperate method, but that does not seem to work also. I looked if the remove walls method gets called at the first cell and it says it is true, but in some way the values of that cell gets overwritten.
Code:
import pygame
import random
WIDTH = 300
HEIGHT = 300
CellSize = 30
Rows = int(WIDTH/30)
Columns = int(HEIGHT/30)
current = None
grid = []
visited = []
DISPLAY = pygame.display.set_mode((WIDTH, HEIGHT))
class Cell:
def __init__(self, r, c):
self.r = r
self.c = c
self.x = CellSize * c
self.y = CellSize * r
self.sides = [True, True, True, True] # Top, Bottom, Left, Right
self.visited = False
self.neighbours = []
self.NTop = None
self.NBottom = None
self.NRight = None
self.NLeft = None
self.NTopIndex = None
self.NBottomIndex = None
self.NRightIndex = None
self.NLeftIndex = None
self.nr = None
self.nc = None
self.random = None
self.neighbour = None
def index(self, nr, nc):
self.nr = nr
self.nc = nc
if self.nr < 0 or self.nc < 0 or self.nr > Rows-1 or self.nc > Columns-1:
return -1
return self.nr + self.nc * Columns
def neighbour_check(self):
# Get neighbour positions in Grid
self.NTopIndex = self.index(self.r, self.c - 1)
self.NBottomIndex = self.index(self.r, self.c + 1)
self.NRightIndex = self.index(self.r + 1, self.c)
self.NLeftIndex = self.index(self.r - 1, self.c)
# Look if they are truly neighbours and then append to neighbour list
if self.NTopIndex >= 0:
self.NTop = grid[self.NTopIndex]
if not self.NTop.visited:
self.neighbours.append(self.NTop)
if self.NBottomIndex >= 0:
self.NBottom = grid[self.NBottomIndex]
if not self.NBottom.visited:
self.neighbours.append(self.NBottom)
if self.NRightIndex >= 0:
self.NRight = grid[self.NRightIndex]
if not self.NRight.visited:
self.neighbours.append(self.NRight)
if self.NLeftIndex >= 0:
self.NLeft = grid[self.NLeftIndex]
if not self.NLeft.visited:
self.neighbours.append(self.NLeft)
# Choose random neighbour
if len(self.neighbours) > 0:
self.random = random.randint(0, len(self.neighbours) - 1)
self.neighbour = self.neighbours[self.random]
# Remove the wall between self and neighbour
if self.neighbour == self.NTop:
if self == grid[0]:
print('TOP')
self.sides[0] = False
self.NTop.sides[1] = False
elif self.neighbour == self.NBottom:
if self == grid[0]:
print('BOTTOM')
self.sides[1] = False
self.NBottom.sides[0] = False
elif self.neighbour == self.NLeft:
if self == grid[0]:
print('LEFT')
self.sides[2] = False
self.NLeft.sides[3] = False
elif self.neighbour == self.NRight:
if self == grid[0]:
print('RIGHT')
self.sides[3] = False
self.NRight.sides[2] = False
else:
print('SIDES ERROR')
return self.neighbours[self.random]
else:
return -1
def draw(self):
global DISPLAY, CellSize
# Top
if self.sides[0]:
pygame.draw.line(DISPLAY, (0, 0, 0), (self.x, self.y), (self.x + CellSize, self.y))
# Bottom
if self.sides[1]:
pygame.draw.line(DISPLAY, (0, 0, 0), (self.x, self.y + CellSize), (self.x + CellSize, self.y + CellSize))
# Left
if self.sides[2]:
pygame.draw.line(DISPLAY, (0, 0, 0), (self.x, self.y), (self.x, self.y + CellSize))
# Right
if self.sides[3]:
pygame.draw.line(DISPLAY, (0, 0, 0), (self.x + CellSize, self.y), (self.x + CellSize, self.y + CellSize))
class Maze:
def __init__(self):
global current
self.next = None
self.running = True
self.DISPLAY = None
self.display_running = True
def init_cells(self):
# Make grid and make cell 0 the begin of the algorithm
global current
for i in range(0, Columns):
for j in range(0, Rows):
cell = Cell(j, i)
grid.append(cell)
current = grid[0]
def init_maze(self):
global current, visited
print(grid[0].sides)
# Start Algorithm
while self.running:
# Check if the current cell is visited, if not make it visited and choose new neighbour
if not current.visited:
current.visited = True
visited.append(current)
self.next = current.neighbour_check()
if not self.next == -1:
# If it finds a neighbour then make it the new current cell
# self.next.visited = True
current = self.next
elif self.next == -1 and len(visited) > 0:
# If it doesn't then look trough the path and backtrack trough it to find a possible neighbour
if len(visited) > 1:
del visited[-1]
current = visited[-1]
# If the last cell of the visited list is Cell 0 then remove it
elif len(visited) <= 1:
del visited[-1]
elif len(visited) <= 0:
# Stop the Algorithm
self.running = False
print('Done')
def draw(self):
DISPLAY.fill((255, 255, 255))
# Get the maze made by the algorithm and draw it on the screen
for i in range(0, len(grid)):
grid[i].draw()
pygame.display.update()
while self.display_running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.display_running = False
maze = Maze()
maze.init_cells()
maze.init_maze()
maze.draw()
I put some print methods in it for debugging purposes.
And am still a beginner in programming, I know it could probably be way cleaner or that some naming of methods could be better.
What I want to happen is that in def init_maze the maze blueprints gets written out and that in def draw the blueprint gets drawn on the screen.

My player sprite keeps on disappearing if it lands on the platform

I am trying to build my first Platformer Game. So far, I have make moving left and right but unfortunately, I have encountered a error that I have not been able to fix when I implemented collisions and gravity. My player keeps on dissolving like spider man if it lands on the platform. The character is still existent, and lands on the platform, but unfortunately, he becomes invisible. There is no error message, and I suspect is has to do with the collision check.
hits = pygame.sprite.spritecollide(object, allPlatforms, False)
if hits:
object.rect.y = hits[0].rect.top + 1
object.vy = 0
print(object.rect.midbottom)
It prints out the Players location in the code, and the player is still existent and movable, but it just doesn't show. Is there something that I did that makes the character vanish?
import pygame
import random
WIDTH = 500
HEIGHT = 400
FPS = 30
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
playerImage = "blockBandit/BlockBandit.png"
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((50, 50))
self.image = pygame.image.load(playerImage).convert()
self.rect = self.image.get_rect()
self.rect.center = (WIDTH / 2, HEIGHT / 2)
self.vx = 0
self.vy = 0
class Platform(pygame.sprite.Sprite):
def __init__(self, x, y, w, h):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((w, h))
self.image.fill(GREEN)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Block Bandit")
clock = pygame.time.Clock()
allPlatforms = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()
player = Player()
all_sprites.add(player)
p1 = Platform(0, HEIGHT - 40, WIDTH, 40)
all_sprites.add(p1)
allPlatforms.add(p1)
def moveCharacter(object):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
object.vx += -2
if keys[pygame.K_RIGHT]:
object.vx += 2
object.vx = object.vx * 0.9
if (abs(object.vx) < 1):
object.vx = 0
if (abs(object.vx) > 10):
if(object.vx < 0):
object.vx = -10
else:
object.vx = 10
object.vy = object.vy + 1
object.rect.x += object.vx
object.rect.y += object.vy
hits = pygame.sprite.spritecollide(object, allPlatforms, False)
if hits:
object.rect.y = hits[0].rect.top + 1
object.vy = 0
print(object.rect.midbottom)
running = True
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
moveCharacter(player)
#Update State
all_sprites.update()
#Render
screen.fill(BLACK)
all_sprites.draw(screen)
#screen.blit(player.icon, (20, 40))
pygame.display.flip()
pygame.quit()
Am I doing something wrong? Thanks!
The y attribute of the rect is the same as the top coordinate, so you're setting the top of the player sprite to the top of the platform sprite here object.rect.y = hits[0].rect.top + 1. And if the platform comes later in the sprite group, it will be blitted after the player and the player won't be visible.
Just change that line to object.rect.bottom = hits[0].rect.top + 1.

Resources