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.
Related
I want to display text on the text box that says if I hit and how much damage I do and vice versa for the enemy, but I just can't figure out how to make the text to display in this manner.
Here is the code I'm working on:
def textBox(textv):
lText = []
text = font.render(str(textv),True,(1,1,1))
lText.append(text)
if len(lText) >= 10:
lText.pop(9)
screen.blit(lText[0],(20,400))
screen.blit(lText[1],(20,380))
while True:
battle_screen()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.blit(enemy_background,(20,20))
player.drawPlayer()
enemy.newEnemy()
textBox("Daniel")
textBox("Jenny")
pygame.display.update()
Rendere the text an add the rendered text into a list:
text_surf_list = []
text_surf = font.render("Daniel", True, (255, 255, 0))
text_surf_list.append(text_surf)
text_surf = font.render("Jenny", True, (255, 255, 0))
text_surf_list.append(text_surf)
Define a rectangular area for the text box:
text_box_rect = pygame.Rect(20, 20, 250, 360)
Iterate through list in reversed order. Draw the text from the bottom up. Remove the text from the list if it is not in the box (see How to remove items from a list while iterating?):
text_y = box_rect.bottom - font.get_height()
for text_surf in reversed(text_surf_list[:]):
if text_y <= box_rect.top:
text_surf_list.remove(text_surf)
else:
surf.blit(text_surf, (box_rect.left + 10, text_y))
text_y -= font.get_height()
Set a temporary clipping area while drawing the text box withpygame.Surface.set_clip:
# set clipping
surf.set_clip(box_rect)
# draw text box
# [...]
# disable clipping
surf.set_clip(None)
Minimal example:
import pygame, random
pygame.init()
window = pygame.display.set_mode((400, 400))
clock = pygame.time.Clock()
def draw_scroll_text_box(surf, font, box_rect, text_surf_list):
surf.set_clip(box_rect)
text_y = box_rect.bottom - font.get_height() - 10
for text_surf in reversed(text_surf_list[:]):
if text_y <= box_rect.top:
text_surf_list.remove(text_surf)
else:
surf.blit(text_surf, (box_rect.left + 10, text_y))
text_y -= font.get_height()
pygame.draw.rect(surf, (164, 164, 164), box_rect, 5)
surf.set_clip(None)
font = pygame.font.SysFont(None, 50)
text_box_rect = pygame.Rect(20, 20, 250, 360)
text_surf_list = []
timer_event = pygame.USEREVENT+1
pygame.time.set_timer(timer_event, 200)
background = pygame.Surface(window.get_size())
ts, w, h, c1, c2 = 50, *background.get_size(), (32, 32, 32), (64, 64, 64)
tiles = [((x*ts, y*ts, ts, ts), c1 if (x+y) % 2 == 0 else c2) for x in range((w+ts-1)//ts) for y in range((h+ts-1)//ts)]
[pygame.draw.rect(background, color, rect) for rect, color in tiles]
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == timer_event:
names = ["Daniel", "Jenny", "Patrick", "Sandy", "Bob"]
text_surf = font.render(random.choice(names), True, (255, 255, 0))
text_surf_list.append(text_surf)
window.blit(background, (0, 0))
draw_scroll_text_box(window, font, text_box_rect, text_surf_list)
pygame.display.flip()
clock.tick(60)
pygame.quit()
exit()
This question already has answers here:
Pygame - Mouse clicks not getting detected
(2 answers)
Closed 2 years ago.
I am currently working on a CS50 project studying the AI required for tic-tac-toe. however I can't get the runner.py file to run, as I get the following error:
Traceback (most recent call last): File
"/Users/newtracksuit/Downloads/tictactoe/runner.py", line 57, in
click, _, _ = pygame.mouse.get_pressed() ValueError: too many values to unpack (expected 3)
here is the full code provided, I have yet to write any actual functionality to the tic-tac-toe program, Just need to understand why this is not working yet
import pygame
import sys
import time
import tictactoe as ttt
pygame.init()
size = width, height = 600, 400
# Colors
black = (0, 0, 0)
white = (255, 255, 255)
screen = pygame.display.set_mode(size)
mediumFont = pygame.font.Font("OpenSans-Regular.ttf", 28)
largeFont = pygame.font.Font("OpenSans-Regular.ttf", 40)
moveFont = pygame.font.Font("OpenSans-Regular.ttf", 60)
user = None
board = ttt.initial_state()
ai_turn = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.fill(black)
# Let user choose a player.
if user is None:
# Draw title
title = largeFont.render("Play Tic-Tac-Toe", True, white)
titleRect = title.get_rect()
titleRect.center = ((width / 2), 50)
screen.blit(title, titleRect)
# Draw buttons
playXButton = pygame.Rect((width / 8), (height / 2), width / 4, 50)
playX = mediumFont.render("Play as X", True, black)
playXRect = playX.get_rect()
playXRect.center = playXButton.center
pygame.draw.rect(screen, white, playXButton)
screen.blit(playX, playXRect)
playOButton = pygame.Rect(5 * (width / 8), (height / 2), width / 4, 50)
playO = mediumFont.render("Play as O", True, black)
playORect = playO.get_rect()
playORect.center = playOButton.center
pygame.draw.rect(screen, white, playOButton)
screen.blit(playO, playORect)
# Check if button is clicked
click, _, _ = pygame.mouse.get_pressed()
if click == 1:
mouse = pygame.mouse.get_pos()
if playXButton.collidepoint(mouse):
time.sleep(0.2)
user = ttt.X
elif playOButton.collidepoint(mouse):
time.sleep(0.2)
user = ttt.O
else:
# Draw game board
tile_size = 80
tile_origin = (width / 2 - (1.5 * tile_size),
height / 2 - (1.5 * tile_size))
tiles = []
for i in range(3):
row = []
for j in range(3):
rect = pygame.Rect(
tile_origin[0] + j * tile_size,
tile_origin[1] + i * tile_size,
tile_size, tile_size
)
pygame.draw.rect(screen, white, rect, 3)
if board[i][j] != ttt.EMPTY:
move = moveFont.render(board[i][j], True, white)
moveRect = move.get_rect()
moveRect.center = rect.center
screen.blit(move, moveRect)
row.append(rect)
tiles.append(row)
game_over = ttt.terminal(board)
player = ttt.player(board)
# Show title
if game_over:
winner = ttt.winner(board)
if winner is None:
title = f"Game Over: Tie."
else:
title = f"Game Over: {winner} wins."
elif user == player:
title = f"Play as {user}"
else:
title = f"Computer thinking..."
title = largeFont.render(title, True, white)
titleRect = title.get_rect()
titleRect.center = ((width / 2), 30)
screen.blit(title, titleRect)
# Check for AI move
if user != player and not game_over:
if ai_turn:
time.sleep(0.5)
move = ttt.minimax(board)
board = ttt.result(board, move)
ai_turn = False
else:
ai_turn = True
# Check for a user move
click, _, _ = pygame.mouse.get_pressed()
if click == 1 and user == player and not game_over:
mouse = pygame.mouse.get_pos()
for i in range(3):
for j in range(3):
if (board[i][j] == ttt.EMPTY and tiles[i][j].collidepoint(mouse)):
board = ttt.result(board, (i, j))
if game_over:
againButton = pygame.Rect(width / 3, height - 65, width / 3, 50)
again = mediumFont.render("Play Again", True, black)
againRect = again.get_rect()
againRect.center = againButton.center
pygame.draw.rect(screen, white, againButton)
screen.blit(again, againRect)
click, _, _ = pygame.mouse.get_pressed()
if click == 1:
mouse = pygame.mouse.get_pos()
if againButton.collidepoint(mouse):
time.sleep(0.2)
user = None
board = ttt.initial_state()
ai_turn = False
pygame.display.flip()
pygame.mouse.get_pressed()
Returns a sequence of booleans representing the state of all the mouse buttons.
The number of buttons is either 4 or 6, that depends on the version of pygame. I recommend to get the state of a single button by subscription:
buttons = pygame.mouse.get_pressed()
if button[0] == 1:
# [...]
What is returned when you execute pygame.mouse.get_pressed()? I see (0, 0, 0) which is the three arguments your code expects.
You could work out what your code is returning and add the requisite number of underscores, but if you aren't interested in the remaining arguments, you can use * to handle the rest of the returned values, e.g.
click, *_ = pygame.mouse.get_pressed()
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!
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()
So when I and my acquaintance were working in pygame, we came across a problem:Every time the progress bar reached over 100, it would crash and show us an error. The error code reads:
"Traceback (most recent call last):
File "C:/Users/Tip/Documents/loading bar/load.py", line 150, in <module>
loading(progress/2)
File "C:/Users/Tip/Documents/loading bar/load.py", line 60, in loading
screen.blit(text, (300, 100))
TypeError: argument 1 must be pygame.Surface, not str
libpng warning: iCCP: known incorrect sRGB profile".
And to help, here is the code:
import pygame
import random
pygame.init()
progress = 0
into = True
black = [0, 0, 0]
white = [255, 255, 255]
intoWhite = [249, 244, 244]
green = [0, 255, 0]
screenWidth = 600
screenHeigth = 450
size = [screenWidth, screenHeigth]
font = pygame.font.SysFont("Comic Sans MS", 25)
intoPic = pygame.image.load("images/Can You Surive Pic.png")
cory = "audio/Cory in the House - Opening Sequence.mp3"
coryPic = pygame.image.load("images/CoryBaxterFull.png")
coryMusicBool = False
coryPicBool = False
basic = "audio/basic song.mp3"
shrekPicBool = False
shrekMusicBool = False
shrek = "audio/Shrek's Fairytale Freestyle.mp3"
shrekPic = pygame.image.load("images/shrek.png")
clock = pygame.time.Clock()
clock.tick(10)
count = 0
escPressed = False
screen = pygame.display.set_mode(size)
pygame.display.set_caption('Loading...')
escMessageText = "Press Esc to cancel"
escMessage = font.render(escMessageText, False, white)
escMessageText_2 = "You Can't Leave"
escMessage_2 = font.render(escMessageText_2, False, white)
def textobjecte(text, color, size):
if size == "small":
textsuraface = font.render(text, True, color)
return textsuraface, textsuraface.get_rect()
def loading(progress):
text = ""
if progress < 100:
text = font.render("loading: " + str(int(progress)) + "%", True, green)
screen.blit(text, (300, 100))
def message_to_screen(msg, color, y_displace, size="small"):
textsurf, textrect = textobjecte(msg, color, size)
textrect.center = (screenWidth/2), (screenHeigth/2) + y_displace
screen.blit(textsurf, textrect)
def playcory():
pygame.mixer.music.load(cory)
pygame.mixer.music.play(-1)
def playshrek():
pygame.mixer.music.load(shrek)
pygame.mixer.music.play(-1)
def basicsong():
pygame.mixer.music.load(basic)
pygame.mixer.music.play(-1)
basicsong()
while into == True:
pygame.event.pump()
screen.fill(intoWhite)
screen.blit(intoPic, (75, 0))
intoKey = pygame.key.get_pressed()
pygame.display.flip()
if intoKey[pygame.K_SPACE]:
into = False
while progress/2 < 100:
timeCount = random.randint(1, 10000)
increase = random.randint(1, 10)
progress += increase/10000
screen.fill(black)
pygame.event.pump()
pygame.draw.rect(screen, white, [300, 50, 200, 50])
pygame.draw.rect(screen, black, [301, 51, 198, 48])
if (progress/2) > 100:
pygame.draw.rect(screen, white, [302, 52, 196, 46])
else:
pygame.draw.rect(screen, white, [302, 52, progress, 46])
if coryMusicBool == True:
pygame.mixer.music.stop()
playcory()
coryMusicBool = False
if coryPicBool== True:
screen.blit(coryPic, (0, 0))
if shrekMusicBool == True:
pygame.mixer.music.stop()
playshrek()
shrekMusicBool = False
if shrekPicBool == True:
screen.blit(shrekPic, (100, 0))
if escPressed == False:
screen.blit(escMessage, (50, 50))
if escPressed == True:
screen.blit(escMessage_2, (50, 50))
keys = pygame.key.get_pressed()
if keys[pygame.K_ESCAPE]:
pygame.mixer.music.stop()
progress = 0
escPressed = True
keys2 = pygame.key.get_pressed()
if keys2[pygame.K_c]:
coryMusicBool = True
coryPicBool = True
keys3 = pygame.key.get_pressed()
if keys3[pygame.K_s]:
shrekPicBool = True
shrekMusicBool = True
if progress/2 >= 100:
progress = 200
loading(progress/2)
pygame.display.flip()
Our code is supposed to be a loading bar. The text that shows the problem of the progression is in the loading x % part.
Please help, and thank you.
Try This
import pygame
import random
pygame.init()
progress = 0
into = True
black = [0, 0, 0]
white = [255, 255, 255]
intoWhite = [249, 244, 244]
green = [0, 255, 0]
screenWidth = 600
screenHeigth = 450
size = [screenWidth, screenHeigth]
font = pygame.font.SysFont("Comic Sans MS", 25)
intoPic = pygame.image.load("images/Can You Surive Pic.png")
cory = "audio/Cory in the House - Opening Sequence.mp3"
coryPic = pygame.image.load("images/CoryBaxterFull.png")
coryMusicBool = False
coryPicBool = False
basic = "audio/basic song.mp3"
shrekPicBool = False
shrekMusicBool = False
shrek = "audio/Shrek's Fairytale Freestyle.mp3"
shrekPic = pygame.image.load("images/shrek.png")
clock = pygame.time.Clock()
clock.tick(10)
count = 0
escPressed = False
screen = pygame.display.set_mode(size)
pygame.display.set_caption('Loading...')
escMessageText = "Press Esc to cancel"
escMessage = font.render(escMessageText, False, white)
escMessageText_2 = "You Can't Leave"
escMessage_2 = font.render(escMessageText_2, False, white)
def textobjecte(text, color, size):
if size == "small":
textsuraface = font.render(text, True, color)
return textsuraface, textsuraface.get_rect()
def loading(progress):
text = ""
if progress < 101:
text = font.render("loading: " + str(int(progress)) + "%", True, green)
screen.blit(text, (300, 100))
def message_to_screen(msg, color, y_displace, size="small"):
textsurf, textrect = textobjecte(msg, color, size)
textrect.center = (screenWidth/2), (screenHeigth/2) + y_displace
screen.blit(textsurf, textrect)
def playcory():
pygame.mixer.music.load(cory)
pygame.mixer.music.play(-1)
def playshrek():
pygame.mixer.music.load(shrek)
pygame.mixer.music.play(-1)
def basicsong():
pygame.mixer.music.load(basic)
pygame.mixer.music.play(-1)
basicsong()
while into == True:
pygame.event.pump()
screen.fill(intoWhite)
screen.blit(intoPic, (75, 0))
intoKey = pygame.key.get_pressed()
pygame.display.flip()
if intoKey[pygame.K_SPACE]:
into = False
while progress/2 < 100:
timeCount = random.randint(1, 10000)
increase = random.randint(1, 10)
progress += increase/10000
screen.fill(black)
pygame.event.pump()
pygame.draw.rect(screen, white, [300, 50, 200, 50])
pygame.draw.rect(screen, black, [301, 51, 198, 48])
if (progress/2) > 100:
pygame.draw.rect(screen, white, [302, 52, 196, 46])
else:
pygame.draw.rect(screen, white, [302, 52, progress, 46])
if coryMusicBool == True:
pygame.mixer.music.stop()
playcory()
coryMusicBool = False
if coryPicBool== True:
screen.blit(coryPic, (0, 0))
if shrekMusicBool == True:
pygame.mixer.music.stop()
playshrek()
shrekMusicBool = False
if shrekPicBool == True:
screen.blit(shrekPic, (100, 0))
if escPressed == False:
screen.blit(escMessage, (50, 50))
if escPressed == True:
screen.blit(escMessage_2, (50, 50))
keys = pygame.key.get_pressed()
if keys[pygame.K_ESCAPE]:
pygame.mixer.music.stop()
progress = 0
escPressed = True
keys2 = pygame.key.get_pressed()
if keys2[pygame.K_c]:
coryMusicBool = True
coryPicBool = True
keys3 = pygame.key.get_pressed()
if keys3[pygame.K_s]:
shrekPicBool = True
shrekMusicBool = True
if progress/2 >= 100:
progress = 200
loading(progress/2)
pygame.display.flip()
For the SRGb error add this to your code pngcrush -ow -rem allb -reduce file.png. its from this link. libpng warning: iCCP: known incorrect sRGB profile
For your game quitting at 100% its because your while loop ends when you reach 100% so you should make it so the draw screen statement is in a separate loop from the draw the loading bar one
In the loading function, text is either a string if the progress is >= 100 or a pygame.Surface if the progress is < 100.
Since the pygame.Surface.blit method only takes a pygame.Surface as the first argument, a TypeError will be raised when you pass a string.
A simple fix would be to blit the text surface in the if clause:
def loading(progress):
if progress < 100:
text = font.render("loading: " + str(int(progress)) + "%", True, green)
screen.blit(text, (300, 100))
You could also blit a transparent surface or do text = font.render("", True, green), but that would be pointless. Just don't blit anything when the progress is >= 100.