Pygame text not going away - image

So I have been working on a game and for some odd reason down in the level1 function I thought if I had a while loop with leveltext1 = false then I could write the text onto the screen, and then wait 2 seconds and then make level1text = true and it would go away. I guess it didnt...
Code:
#!/usr/bin/python
import pygame
import time
pygame.init()
blue = (25,25,112)
black = (0,0,0)
red = (200,0,0)
bright_red = (255,0,0)
white = (255,255,255)
groundcolor = (139,69,19)
green = (80,80,80)
other_green = (110,110,110)
lives = 0
gameDisplay = pygame.display.set_mode((1336,768))
pygame.display.set_caption("TheAviGame")
direction = 'none'
clock = pygame.time.Clock()
img = pygame.image.load('guyy.bmp')
famousdude = pygame.image.load('kitten1.bmp')
kitten = pygame.image.load('macharacterbrah.bmp')
dorritosman = pygame.image.load('Doritos.bmp')
backround = pygame.image.load('backroundd.bmp')
playernormal = pygame.image.load('normalguy.bmp')
playerocket = pygame.image.load('rocketguyy.bmp')
rocketcat = pygame.image.load('jetpackcat.bmp')
mlglogo = pygame.image.load('mlg.bmp')
level1bg = pygame.image.load('level1background.bmp')
def mts(text, textcolor, x, y, fs):
font = pygame.font.Font(None,fs)
text = font.render(text, True, textcolor)
gameDisplay.blit(text, [x,y])
def button(x,y,w,h,ic,ac):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(gameDisplay, ac,(x,y,w,h))
if click[0] == 1:
gameloop()
else:
pygame.draw.rect(gameDisplay, ic,(x,y,w,h))
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
#print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(black)
button(450,450,250,70,green,other_green)
mts("Play", white, 540, 470, 50)
mts("THE AVI GAME", red, 380, 100, 100)
gameDisplay.blit(famousdude, (100,100))
gameDisplay.blit(kitten, (700,300))
gameDisplay.blit(dorritosman, (1000,400))
pygame.display.update()
clock.tick(15)
def gameloop():
imgx = 1000
imgy = 100
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
imgx += 10
gameDisplay.blit(backround, (30,30))
gameDisplay.blit(rocketcat, (imgx,imgy))
pygame.display.update()
clock.tick(15)
def level1():
imgx = 500
imgy = 500
leveltext = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
while not leveltext:
gameDisplay.blit(level1bg, (0,0))
mts("LEVEL 1:", red, 450, 220, 60)
mts('Controls: W or Up to move up.', white, 450, 300, 40)
mts('Controls: A or Left to move left.', white, 450, 340, 40)
mts('Controls: S or Down to move down.', white, 450, 390, 40)
mts('Controls: D or Right to move Right.', white, 450, 430, 40)
time.sleep(2)
leveltext = True
pygame.display.update()
level1()

From my understanding, when you blit text onto a surface it permanently becomes part of that surface. If you want the font to disappear, you could try blitting it onto a copy of that surface and then blit the altered surface to the display.

Related

How to add random number of falling balls in pygame?

Here is some code for random falling balls, I am able to generate one falling ball. Having hard time to randomize the number of falling balls, also they need to be some random delays falling from "sky". Could you tell how to do it ?
import pygame
import random
pygame.init()
width, height = 640, 480
screen = pygame.display.set_mode((width, height))
robot = pygame.image.load("robot.png")
y1 = 0
y2 = 0
speed1 = 1
speed2 = 2
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
screen.fill((0, 0, 0))
robots = []
screen.blit(robot, (100, y1))
y1 += speed1
if y1 >= 640:
y1 = 0
pygame.display.flip()
clock.tick(60)
Maybe this is the best solution for your problem. I did a list with position (x, y) and then I used this values in for loop.
Here is code example:
balls_num = 100
robots = [[random.randint(0, width), random.randint(-600, 0)] for _ in range(balls_num)]
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
screen.fill((0, 0, 0))
for pos in robots:
screen.blit(robot, (pos[0], pos[1]))
pos[1] += speed1
if pos[1] >= 640:
pos[1] = -100
I hope it will help you, Adam

Collision problem between drawn circle and a group of sprites pygame (python) [duplicate]

This question already has answers here:
Sometimes the ball doesn't bounce off the paddle in pong game
(1 answer)
How do I detect collision in pygame?
(5 answers)
Closed 1 year ago.
I need to create a background of square sprites generated with a matrix of letters and have a ball that collides perfectly with these sprites regardless of its angle and speed. As you can see in my code the collision and bounce bugs are numerous, I cannot find the solution, can you help me, thank you
import pygame
from math import *
import sys
pygame.init()
clock = pygame.time.Clock()
display = pygame.display.set_mode((900, 900))
white = (255, 255, 255)
black = (30, 30, 30)
arial_font = pygame.font.SysFont("arial Black", 36)
TILE_SIZE = 100
level = [
"EEEEEEEE",
"E E",
"E E E E",
"E E",
"E X E",
"E E E E",
"E E",
"EEEEEEEE", ]
walls_group_sprites = pygame.sprite.Group()
spawn_ball = []
def create_level():
x = y = TILE_SIZE
for row in level:
for column in row:
if column == "E":
wall = Sprite("wall.png", x, y)
walls_group_sprites.add(wall)
if column == "X":
spawn_ball.append(x)
spawn_ball.append(y)
x += TILE_SIZE
y += TILE_SIZE
x = TILE_SIZE
class Sprite(pygame.sprite.Sprite):
def __init__(self, image_path, pos_x, pos_y):
super().__init__()
self.image = pygame.image.load(image_path)
self.rect = self.image.get_rect()
self.rect.center = [pos_x, pos_y]
class Ball:
def __init__(self, x, y, speed, angle):
self.x = x
self.y = y
self.color = white
self.angle = angle
self.speed = speed
self.radius = 25
def move(self):
# move Ball with math import cosinus/sinus
self.x += self.speed * cos(radians(self.angle))
self.y += self.speed * sin(radians(self.angle))
# Draw ball with pygame
self.circle = pygame.draw.circle(display, self.color, (self.x, self.y), self.radius)
def collide(self):
# My wall Sprite is a image (100X100 pixels) with rect, there are 100 sprites in the pygame group sprite
for wall_index in walls_group_sprites:
if self.y + self.radius > wall_index.rect.top and self.y - self.radius < wall_index.rect.bottom and self.x + self.radius > wall_index.rect.left and self.x - self.radius < wall_index.rect.right:
# if the ball.y is in the height of the sprite then the rebound is vertical
if wall_index.rect.top < self.y < wall_index.rect.bottom:
self.angle = 180 - int(self.angle)
# if the ball.x is in the width of the sprite then the rebound is horizontal
elif wall_index.rect.left < self.x < wall_index.rect.right:
self.angle = 360 - int(self.angle)
else:
#if the ball is in one of the two lower left or upper right corners then the rebound and sideways
if (self.y > wall_index.rect.bottom and self.x < wall_index.rect.left) or (self.y < wall_index.rect.top and self.x > wall_index.rect.right):
self.angle = 90 - int(self.angle)
#if the ball is in one of the two corners at the bottom right or the top left then the rebound and on the other side
elif (self.y > wall_index.rect.bottom and self.x > wall_index.rect.right) or (self.y < wall_index.rect.top and self.x < wall_index.rect.left):
self.angle = 270 - int(self.angle)
create_level()
ball = Ball(spawn_ball[0], spawn_ball[1], 7, 40)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN: ball.angle += 10
display.fill(black)
walls_group_sprites.draw(display)
ball.move()
ball.collide()
text = arial_font.render(f"Click to change Angle {abs(ball.angle)}", True, white)
display.blit(text, [200, 0])
clock.tick(60)
pygame.display.update()
my wall sprite (png)

How do I leave a trail behind a sprite with pygame?

It's known that if you move a sprite without filling the screen before it leaves a trail, however, I want to leave a cool trail behind my while moving other stuff (which means I cannot simply stop filling the screen.
Thanks in advance for any help
One solution would be to create another transparent surface (called alpha_surf here) with the size of the screen onto which you blit the objects with trails. It needs to be a per-pixel alpha surface which you can create by passing the pygame.SRCALPHA special flag.
Blit the objects and reduce the alpha of all pixels on the alpha_surf each frame by filling it with a transparent white and also pass the pygame.BLEND_RGBA_MULT flag so that only the alpha channel is affected.
import pygame as pg
from pygame.math import Vector2
class Player(pg.sprite.Sprite):
def __init__(self, pos, *groups):
super().__init__(*groups)
self.image = pg.Surface((50, 50), pg.SRCALPHA)
pg.draw.circle(self.image, pg.Color('dodgerblue'), (25, 25), 25)
self.rect = self.image.get_rect(center=pos)
self.vel = Vector2(0, 0)
self.pos = Vector2(pos)
def update(self):
self.pos += self.vel
self.rect.center = self.pos
def main():
pg.init()
screen = pg.display.set_mode((640, 480))
# Blit objects with trails onto this surface instead of the screen.
alpha_surf = pg.Surface(screen.get_size(), pg.SRCALPHA)
clock = pg.time.Clock()
all_sprites = pg.sprite.Group()
player = Player((150, 150), all_sprites)
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
return
elif event.type == pg.KEYDOWN:
if event.key == pg.K_d:
player.vel.x = 5
elif event.key == pg.K_a:
player.vel.x = -5
elif event.key == pg.K_w:
player.vel.y = -5
elif event.key == pg.K_s:
player.vel.y = 5
elif event.type == pg.KEYUP:
if event.key == pg.K_d and player.vel.x > 0:
player.vel.x = 0
elif event.key == pg.K_a and player.vel.x < 0:
player.vel.x = 0
elif event.key == pg.K_w:
player.vel.y = 0
elif event.key == pg.K_s:
player.vel.y = 0
# Reduce the alpha of all pixels on this surface each frame.
# Control the fade speed with the alpha value.
alpha_surf.fill((255, 255, 255, 220), special_flags=pg.BLEND_RGBA_MULT)
all_sprites.update()
screen.fill((20, 50, 80)) # Clear the screen.
all_sprites.draw(alpha_surf) # Draw the objects onto the alpha_surf.
screen.blit(alpha_surf, (0, 0)) # Blit the alpha_surf onto the screen.
pg.display.flip()
clock.tick(60)
if __name__ == '__main__':
main()
pg.quit()
Alternatively, you could create several versions of the sprite image with different alpha values and also store the previous positions of the sprite. Then just blit the images with lower alpha at the previous positions.
You can also blit other images or particles instead of the self.image if you want to create a different kind of trail, for example smoke.
Here's another variant with a separate, different image for the trail which gets blitted before the self.images of the sprites are blitted, so that it'll appear below them:
import pygame as pg
from pygame.math import Vector2
class Player(pg.sprite.Sprite):
def __init__(self, pos, *groups):
super().__init__(*groups)
self.image = pg.Surface((50, 70), pg.SRCALPHA)
self.image.fill(pg.Color('sienna1'))
self.rect = self.image.get_rect(center=pos)
# A separate image for the trail (just a single-color circle).
self.trail_image = pg.Surface((40, 40), pg.SRCALPHA)
pg.draw.circle(self.trail_image, pg.Color('dodgerblue'), (20, 20), 20)
self.trail_rect = self.trail_image.get_rect()
self.vel = Vector2(0, 0)
self.pos = Vector2(pos)
def update(self):
self.pos += self.vel
self.rect.center = self.pos
# Update the rect of the trail as well, because we'll blit it there.
self.trail_rect.center = self.rect.center
def main():
pg.init()
screen = pg.display.set_mode((640, 480))
# Blit objects with trails onto this surface instead of the screen.
alpha_surf = pg.Surface(screen.get_size(), pg.SRCALPHA)
clock = pg.time.Clock()
all_sprites = pg.sprite.Group()
sprites_with_trails = pg.sprite.Group()
player = Player((150, 150), all_sprites, sprites_with_trails)
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
return
elif event.type == pg.KEYDOWN:
if event.key == pg.K_d:
player.vel.x = 5
elif event.key == pg.K_a:
player.vel.x = -5
elif event.key == pg.K_w:
player.vel.y = -5
elif event.key == pg.K_s:
player.vel.y = 5
elif event.type == pg.KEYUP:
if event.key == pg.K_d and player.vel.x > 0:
player.vel.x = 0
elif event.key == pg.K_a and player.vel.x < 0:
player.vel.x = 0
elif event.key == pg.K_w:
player.vel.y = 0
elif event.key == pg.K_s:
player.vel.y = 0
# Reduce the alpha of all pixels on this surface each frame.
# Control the fade speed with the alpha value.
alpha_surf.fill((255, 255, 255, 244), special_flags=pg.BLEND_RGBA_MULT)
all_sprites.update()
screen.fill((20, 50, 80)) # Clear the screen.
# Blit the trails onto the alpha_surf.
for sprite in sprites_with_trails:
alpha_surf.blit(sprite.trail_image, sprite.trail_rect)
screen.blit(alpha_surf, (0, 0)) # Blit the alpha_surf onto the screen.
all_sprites.draw(screen) # Draw the objects onto the alpha_surf.
pg.display.flip()
clock.tick(60)
if __name__ == '__main__':
main()
pg.quit()

Pygame in Python: Screen.Blit Problems

I'm creating a little Pong-like program using Python and Pygame. I will edit it to add a point system and other features soon, but I want to address a problem first.
In this Pong program, you get three lives, and if you lose on the last life, it should display a game over image. It works perfectly fine with the first three lives, but it does not produce the game over image, and produces this instead:
Traceback (most recent call last):
File "/Volumes/JARED'S USB 3/Python/Pie Game.py", line 258, in <module>
gameplay1()
File "/Volumes/JARED'S USB 3/Python/Pie Game.py", line 84, in gameplay1
change_level1()
File "/Volumes/JARED'S USB 3/Python/Pie Game.py", line 241, in change_level1
gameplay2()
File "/Volumes/JARED'S USB 3/Python/Pie Game.py", line 161, in gameplay2
change_level2()
File "/Volumes/JARED'S USB 3/Python/Pie Game.py", line 245, in change_level2
gameplay3()
File "/Volumes/JARED'S USB 3/Python/Pie Game.py", line 238, in gameplay3
game_over()
File "/Volumes/JARED'S USB 3/Python/Pie Game.py", line 255, in game_over
screen.blit(game_over1(300,250))
TypeError: 'pygame.Surface' object is not callable
My code consists of lots of def functions, but the others work perfectly fine. This is my code:
import pygame, time, sys, random
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption ("Pong Squash")
def gameplay1():
global game_over_display, lives, points, lives_remaining, game_over1, lives1, points1, lives_remaining1, font1, white, black, green, yellow, lives_number, lives_count, position_x, position_y, velocity_x1, velocity_y1, position1, position2, velocity1, velocity2, color, width, position, player, ball
game_over_display = "game_over.png"
lives = "lives.png"
points = "points.png"
lives_remaining = "lives_remaining.png"
game_over1 = pygame.image.load(game_over_display).convert()
lives1 = pygame.image.load(lives).convert()
points1 = pygame.image.load(points).convert()
lives_remaining1 = pygame.image.load(lives_remaining).convert()
font1 = pygame.font.Font(None, 40)
white = 255,255,255
black = 0, 0, 0
green = 0, 250, 0
yellow = 255, 255, 0
lives_count = font1.render(('3'), True, white)
position_x = 175
position_y = 375
velocity_x1 = 0
velocity_y1 = 0
position1 = 275
position2 = 150
velocity1 = 2
velocity2 = 2
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == pygame.K_SPACE:
if waiting:
waiting = False
reset_ball()
if event.key == pygame.K_LEFT:
velocity_x1 = (velocity_x1 - 3)
elif event.key == pygame. K_RIGHT:
velocity_x1 = (velocity_x1 + 3)
elif event.type == KEYUP:
if event.key == K_LEFT or event.key == K_RIGHT:
velocity_x1 = 0
screen.fill((0, 0, 0))
color = 255
width = 0
position = position_x, position_y, 250, 50
position_x += velocity_x1
position_y += velocity_y1
position1 += velocity1
position2 += velocity2
player = pygame.draw.rect(screen, (color, color, color), position, width)
ball = pygame.draw.rect(screen, (color, color, color), (position1, position2, 25, 25), width)
screen.blit(lives1, (450, 0))
screen.blit(points1,(0, 0))
screen.blit(lives_count,(560,5))
pygame.display.update()
if player.colliderect(ball):
velocity1 = - velocity1
velocity2 = - velocity2
if position_x > 350 or position_x < 0:
velocity_x1 = 0
elif position1 > 575 or position1 < 0:
velocity1 = - velocity1
elif position2 < 0:
velocity2 = - velocity2
velocity2 = velocity2 + 0.5
elif position2 > 475:
change_level1()
def gameplay2():
global game_over_display, lives, points, lives_remaining, game_over1, lives1, points1, lives_remaining1, font1, white, black, green, yellow, lives_number, lives_count, position_x, position_y, velocity_x1, velocity_y1, position1, position2, velocity1, velocity2, color, width, position, player, ball
game_over_display = "game_over.png"
lives = "lives.png"
points = "points.png"
lives_remaining = "lives_remaining.png"
game_over1 = pygame.image.load(game_over_display).convert()
lives1 = pygame.image.load(lives).convert()
points1 = pygame.image.load(points).convert()
lives_remaining1 = pygame.image.load(lives_remaining).convert()
font1 = pygame.font.Font(None, 40)
white = 255,255,255
black = 0, 0, 0
green = 0, 250, 0
yellow = 255, 255, 0
lives_count = font1.render(('2'), True, white)
position_x = 175
position_y = 375
velocity_x1 = 0
velocity_y1 = 0
position1 = 275
position2 = 150
velocity1 = 2
velocity2 = 2
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == pygame.K_SPACE:
if waiting:
waiting = False
reset_ball()
if event.key == pygame.K_LEFT:
velocity_x1 = (velocity_x1 - 3)
elif event.key == pygame. K_RIGHT:
velocity_x1 = (velocity_x1 + 3)
elif event.type == KEYUP:
if event.key == K_LEFT or event.key == K_RIGHT:
velocity_x1 = 0
screen.fill((0, 0, 0))
color = 255
width = 0
position = position_x, position_y, 250, 50
position_x += velocity_x1
position_y += velocity_y1
position1 += velocity1
position2 += velocity2
player = pygame.draw.rect(screen, (color, color, color), position, width)
ball = pygame.draw.rect(screen, (color, color, color), (position1, position2, 25, 25), width)
screen.blit(lives1, (450, 0))
screen.blit(points1,(0, 0))
screen.blit(lives_count,(560,5))
pygame.display.update()
if player.colliderect(ball):
velocity1 = - velocity1
velocity2 = - velocity2
if position_x > 350 or position_x < 0:
velocity_x1 = 0
elif position1 > 575 or position1 < 0:
velocity1 = - velocity1
elif position2 < 0:
velocity2 = - velocity2
velocity2 = velocity2 + 0.5
elif position2 > 475:
change_level2()
def gameplay3():
global game_over_display, lives, points, lives_remaining, game_over1, lives1, points1, lives_remaining1, font1, white, black, green, yellow, lives_number, lives_count, position_x, position_y, velocity_x1, velocity_y1, position1, position2, velocity1, velocity2, color, width, position, player, ball
game_over_display = "game_over.png"
lives = "lives.png"
points = "points.png"
lives_remaining = "lives_remaining.png"
game_over1 = pygame.image.load(game_over_display).convert()
lives1 = pygame.image.load(lives).convert()
points1 = pygame.image.load(points).convert()
lives_remaining1 = pygame.image.load(lives_remaining).convert()
font1 = pygame.font.Font(None, 40)
white = 255,255,255
black = 0, 0, 0
green = 0, 250, 0
yellow = 255, 255, 0
lives_count = font1.render(('1'), True, white)
position_x = 175
position_y = 375
velocity_x1 = 0
velocity_y1 = 0
position1 = 275
position2 = 150
velocity1 = 2
velocity2 = 2
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == pygame.K_SPACE:
if waiting:
waiting = False
reset_ball()
if event.key == pygame.K_LEFT:
velocity_x1 = (velocity_x1 - 3)
elif event.key == pygame. K_RIGHT:
velocity_x1 = (velocity_x1 + 3)
elif event.type == KEYUP:
if event.key == K_LEFT or event.key == K_RIGHT:
velocity_x1 = 0
screen.fill((0, 0, 0))
color = 255
width = 0
position = position_x, position_y, 250, 50
position_x += velocity_x1
position_y += velocity_y1
position1 += velocity1
position2 += velocity2
player = pygame.draw.rect(screen, (color, color, color), position, width)
ball = pygame.draw.rect(screen, (color, color, color), (position1, position2, 25, 25), width)
screen.blit(lives1, (450, 0))
screen.blit(points1,(0, 0))
screen.blit(lives_count,(560,5))
pygame.display.update()
if player.colliderect(ball):
velocity1 = - velocity1
velocity2 = - velocity2
if position_x > 350 or position_x < 0:
velocity_x1 = 0
elif position1 > 575 or position1 < 0:
velocity1 = - velocity1
elif position2 < 0:
velocity2 = - velocity2
velocity2 = velocity2 + 0.5
elif position2 > 475:
game_over()
def change_level1():
global game_over_display, lives, points, lives_remaining, game_over1, lives1, points1, lives_remaining1, font1, white, black, green, yellow, lives_number, lives_count, position_x, position_y, velocity_x1, velocity_y1, position1, position2, velocity1, velocity2, color, width, position, player, ball
gameplay2()
pygame.display.update()
def change_level2():
global game_over_display, lives, points, lives_remaining, game_over1, lives1, points1, lives_remaining1, font1, white, black, green, yellow, lives_number, lives_count, position_x, position_y, velocity_x1, velocity_y1, position1, position2, velocity1, velocity2, color, width, position, player, ball
gameplay3()
pygame.display.update()
def reset_ball():
global game_over_display, lives, points, lives_remaining, game_over1, lives1, points1, lives_remaining1, font1, white, black, green, yellow, lives_number, lives_count, position_x, position_y, velocity_x1, velocity_y1, position1, position2, velocity1, velocity2, color, width, position, player, ball
velocity1 = 2
velocity2 = 2
pygame.display.update()
def game_over():
global game_over_display, lives, points, lives_remaining, game_over1, lives1, points1, lives_remaining1, font1, white, black, green, yellow, lives_number, lives_count, position_x, position_y, velocity_x1, velocity_y1, position1, position2, velocity1, velocity2, color, width, position, player, ball
screen.fill((0,0,0))
screen.blit(game_over1(300,250))
pygame.display.update()
gameplay1()
Sorry, this is so long. Please help! I'm new to Pygame!
screen.blit(game_over1(300,250))
docs: pygame.Surface
screen.blit(game_over1, (300,250))

image changing to another image changing in pygame

i made a program in pygame ware a mine turtle runs in a sqare but when you click in the box he explodes but when i change the image to an explodtion it shows th mineturtle first
here's my code:
import pygame, sys, time
from pygame.locals import *
pygame.init()
FPS = 40 # frames per second setting
fpsClock = pygame.time.Clock()
# set up the window
DISPLAYSURF = pygame.display.set_mode((400, 300), 0, 32)
pygame.display.set_caption('Animation')
WHITE = (255, 255, 255)
print("if you click the mine tutle box he will explode")
cat = pygame.image.load("mineturtle.PNG")
catx = 10
caty = 10
direction = 'right'
s=15
while True: # the main game loop
DISPLAYSURF.fill(WHITE)
if direction == 'right':
catx += s
if catx == 280:
direction = 'down'
elif direction == 'down':
caty += s
if caty == 220:
direction = 'left'
elif direction == 'left':
catx -= s
if catx == 10:
direction = 'up'
elif direction == 'up':
caty -= s
if caty == 10:
direction = 'right'
DISPLAYSURF.blit(cat, (catx, caty))
for event in pygame.event.get():
if event.type == MOUSEBUTTONDOWN:
b = pygame.image.load("b.png")
time.sleep(2)
pygame.quit()
sys.exit()
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
fpsClock.tick(FPS)
i already have all of the files in the same loaction but when i did the image swich id didn't work! plz help
To show changes on screen you need to call pygame.display.update(). When you call sleep and exit, it does not update the screen, and no changes are seen.
time.
EDIT:
If you wish to show a new image for 2 seconds and then close the program, you should use pygame.time module. Have a variable that will store the time since the last click. You add the result of tick(). When the value will be high enough, you can exit pygame.
This will not freeze the window, and you will be able to see the changes.
i just did this
import pygame, sys, time
from pygame.locals import *
pygame.init()
FPS = 40 # frames per second setting
fpsClock = pygame.time.Clock()
# set up the window
DISPLAYSURF = pygame.display.set_mode((400, 300), 0, 32)
pygame.display.set_caption('Animation')
WHITE = (255, 255, 255)
print("if you click the mine tutle box he will explode")
cat = pygame.image.load("mineturtle.PNG")
catx = 10
caty = 10`enter code here`
direction = 'right'
s=15
explodetick=0
move=True
while True: # the main game loop
DISPLAYSURF.fill(WHITE)
#for event in pygame.event.get():
#
if explodetick>0:
explodetick+=1
if explodetick==81:
pygame.quit()
if move:
if direction == 'right':
catx += s
if catx == 280:
direction = 'down'
elif direction == 'down':
caty += s
if caty == 220:
direction = 'left'
elif direction == 'left':
catx -= s
if catx == 10:
direction = 'up'
elif direction == 'up':
caty -= s
if caty == 10:
direction = 'right'
DISPLAYSURF.blit(cat, (catx, caty))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == MOUSEBUTTONDOWN:
cat = pygame.image.load("b.png")
pygame.display.update()
explodetick=1
move=False
pygame.display.update()
fpsClock.tick(FPS)

Resources