Pygame: Line fails to collide with rectangle - python

I am trying to write a program that looks like a radar map. I have a line drawn that rotates 360 degrees, and a "bogey" image on the radar. I'm trying to figure out hot to get a line collide with a rectangle, but I get this error. AttributeError: 'pygame.Rect' object has no attribute 'get_rect'
Basically, my question is how do you get a drawn line to collide with a blitted image?
Thanks.
import pygame
import math
from colors import *
from pygame.locals import *
pygame.init()
bkrd = pygame.image.load("radar//radar2.jpg")
bkrdrect = bkrd.get_rect()
bogey = pygame.image.load("radar//bogey2.png")
bogeyrect = bogey.get_rect()
window_size = window_width, window_height = 1101,822
window = pygame.display.set_mode( window_size, pygame.RESIZABLE )
pygame.display.set_caption( "Radar" )
clock = pygame.time.Clock()
FPS = 60
pygame.time.set_timer(USEREVENT + 1, 100)
pygame.key.set_repeat(0,0)
window.blit(bkrd,bkrdrect)
pygame.display.update()
angle = 0
eta = 0
bogey_one_x = 532
bogey_one_y = 210
def mainloop():
global angle,eta,bogey_one_x,bogey_one_y
while True:
angle +=1
eta +=1
bogey_one_x += .05
bogey_one_y +=.02
window.blit(bkrd,bkrdrect)
window.blit(bogey,(bogey_one_x,bogey_one_y))
radar = (685,413)
radar_len = 253
x = radar[0] + math.cos(math.radians(angle)) * radar_len
y = radar[1] + math.sin(math.radians(angle)) * radar_len
line = pygame.draw.line(window, (green), radar, (x,y), 1)
linerect = line.get_rect()
if linerect.colliderect(bogeyrect):
print "collide"
pygame.display.update()
for event in pygame.event.get():
if ( event.type == pygame.QUIT ) or \
( event.type == pygame.KEYDOWN and \
( event.key == pygame.K_ESCAPE) ):
running = False
pygame.quit()
mainloop()

Related

Pygame: Moving an image around another image in a circular path

I know that there are already some solutions here but unfortunately they did not help me with my problem. I want to move a satellite image around a planet image in a circular path. I know how to move it in a line from left to right but I don't get it how to make the circle. This is my code so far
import pygame
from pygame.locals import *
from sys import exit
from math import sin
from math import cos
player_image = 'dragon-500px.png'
pygame.init()
screen = pygame.display.set_mode((1080, 720), 0, 32)
pygame.display.set_caption("Animate X!")
mouse_cursor = pygame.image.load(player_image).convert_alpha()
image_earth = pygame.image.load('Erde.jpg').convert()
image_earth = pygame.transform.scale(image_earth, (300,300))
image_earth_rect = image_earth.get_rect()
image_earth_rect.center = (540, 360)
radius = 100
center = (540, 360)
direction = pygame.math.Vector2(1,0)
clock = pygame.time.Clock()
speed = 300.0
x = 0 - mouse_cursor.get_width()
y = 10
while True:
time_passed = clock.tick() / 1000.0
moved_distance = time_passed * speed
for event in pygame.event.get():
if event.type == QUIT:
exit()
direction.rotate_ip(4)
direction.normalize_ip()
Satellite_pos = [int(i) for i in center + direction*radius]
screen.fill((255,255,255))
if x > screen.get_width():
x = 0 - mouse_cursor.get_width()
elif y > screen.get_height():
y = 10
screen.blit(mouse_cursor, (x, y))
screen.blit(image_earth, image_earth_rect)
x+=moved_distance
y+= moved_distance
pygame.display.update() ```
Compute the position of the satellite dependent on an angle:
angle = 0
# [...]
while True:
# [...]
center = image_earth_rect.center
Satellite_pos = [center[0] + radius * cos(angle), center[1] + radius * sin(angle)]
angle += 0.01
# [...]

Pygame update bug

I am creating my game on python with pygame but I have got a bug while updating screen :
my older character position are displayed
I am using a pattern to fill my screen and I refresh it each time and this before my character so it shouldn't be laggy, this is my code :
import pygame, sys
from pygame.locals import *
pygame.init()
#Open Pygame window
taille_fenetre = [960, 640]
fenetre = pygame.display.set_mode(taille_fenetre)
pygame.key.set_repeat(400, 30)
clock = pygame.time.Clock()
#Chargement et collage du fond
fond = pygame.image.load("brock.png").convert()
for a in range(taille_fenetre[0] // fond.get_width() + 1):
for i in range(taille_fenetre[1] // fond.get_height() + 1):
fenetre.blit(fond, (a*fond.get_width(),i*fond.get_height()))
room_size = (taille_fenetre[0]-64*2,taille_fenetre[1]-64*2)
room = pygame.Surface(room_size)
room.fill((255,0,0))
terre = pygame.image.load("terre.png").convert()
for a in range(room.get_width() // terre.get_width() + 1):
for i in range(room.get_height() // terre.get_height() + 1):
room.blit(terre, (a*terre.get_width(),i*terre.get_height()))
shadow_room = pygame.image.load('shadow_room.png')
def blit_alpha(target, source, location, opacity):
x = location[0]
y = location[1]
temp = pygame.Surface((source.get_width(), source.get_height())).convert()
temp.blit(target, (-x, -y))
temp.blit(source, (0, 0))
temp.set_alpha(opacity)
target.blit(temp, location)
class Personnage:
def __init__(self):
self.imagesrc = "perso.png"
self.image = pygame.image.load(self.imagesrc).convert_alpha()
self.size = self.image.get_size()
self.position = [0,0]
self.vitesse = 5
self.sante = 5
def bouger(self,facteur):
self.position = [self.position[0] - self.vitesse * facteur[0],self.position[1] - self.vitesse * facteur[1]]
def afficher(self,fenetre):
print(self.position)
fenetre.blit(self.image,self.position)
#INFINITE LOOP
continuer = 1
perso = Personnage()
while continuer:
relachex = True
relachey = True
#Re-collage
fenetre.blit(fond, (0,0))
fenetre.blit(room,(64,64))
blit_alpha(room,shadow_room,(0,0),128)
for event in pygame.event.get(): #Attente des événements
if event.type == QUIT:
continuer = 0
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed()
if keys[K_LEFT]:
perso.bouger([1,0])
if keys[K_RIGHT]:
perso.bouger([-1,0])
if keys[K_DOWN]:
perso.bouger([0,-1])
if keys[K_UP]:
perso.bouger([0,1])
#REFRESH
perso.afficher(fenetre)
pygame.display.flip()
clock.tick(60)
you are drawing over the previous images again and again without clearing out the old stuff. You can't tell with the other images because they are static, but it shows with the character. Add:
fenetre.fill((0, 0, 0))
to the top of your game loop to refresh the screen to draw the new locations.

How can I mouse-scroll around a 2D tile map in pygame?

I am trying to play around with pygame and I have using some examples that I found to learn and create a simple. My next goal is to create a 2D tile map bigger than the screen size and then being able to scroll around with the mouse. I would like to make something similar as a strategic game, where if you move the mouse to the edges of the screen, the "camara" will move in that direction, showing that part of the map (I would like also to stop the camara if it reaches the end of the map). At this moment, if I hover the mouse over the edges, it will only move once.
import pygame, os
from pygame.locals import *
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
SCREEN_WIDTH = 5*40
SCREEN_HEIGHT = 7*40
#functions to create our resources
def load_image(name, colorkey=None):
try:
image = pygame.image.load(name)
except pygame.error, message:
print 'Cannot load image:', name
raise SystemExit, message
image = image.convert_alpha()
if colorkey is not None:
if colorkey is -1:
colorkey = image.get_at((0,0))
image.set_colorkey(colorkey, RLEACCEL)
return image, image.get_rect()
#classes for our game objects
class Camera(object):
def __init__(self, camera_func, width, height):
self.camera_func = camera_func
self.state = pygame.Rect(100,100, width, height)
def apply(self, rect):
l, t, w, h = rect
if 0 <= self.state[0] <= (SCREEN_WIDTH/5):
l += 10
elif (SCREEN_WIDTH - (SCREEN_WIDTH/5)) < self.state[0] <= SCREEN_WIDTH:
l -=10
if 0 <= self.state[1] <= (SCREEN_HEIGHT/5):
t += 10
elif (SCREEN_HEIGHT - (SCREEN_HEIGHT/5)) < self.state[1] <= SCREEN_HEIGHT:
t -=10
return rect.move(l,t)
def update(self):
pos = pygame.mouse.get_pos()
self.state.topleft = pos
#self.state = self.camera_func(self.state)
def complex_camera(camera):
l, t, w, h = camera
l, t, _, _ = -l, -t, w, h
l = min(0, l) # stop scrolling at the left edge
l = max(-(camera.width-SCREEN_WIDTH), l) # stop scrolling at the right edge
t = max(-(camera.height-SCREEN_HEIGHT), t) # stop scrolling at the bottom
t = min(0, t) # stop scrolling at the top
return pygame.Rect(l, t, w, h)
def main ():
pygame.init()
screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
pygame.display.set_caption("W40K")
grasstile = pygame.image.load('./textures/grass.png')
watertile = pygame.image.load('./textures/water.png')
waterbeach = pygame.image.load('./textures/dirt.png')
grassrect = grasstile.get_rect()
waterrect = watertile.get_rect()
waterb = waterbeach.get_rect()
TILESIZE = 40
tilemap = [
[grasstile,grasstile,waterbeach,waterbeach,watertile,watertile,waterbeach,waterbeach,grasstile,grasstile,waterbeach,watertile,watertile,watertile,waterbeach,waterbeach,waterbeach,grasstile,grasstile,waterbeach],
[grasstile,waterbeach,waterbeach,waterbeach,watertile,watertile,watertile,waterbeach,grasstile,grasstile,waterbeach,watertile,watertile,watertile,waterbeach,waterbeach,waterbeach,grasstile,grasstile,waterbeach],
[waterbeach,waterbeach,waterbeach,waterbeach,watertile,watertile,watertile,waterbeach,grasstile,grasstile,waterbeach,watertile,watertile,watertile,waterbeach,waterbeach,waterbeach,grasstile,grasstile,waterbeach],
[grasstile,waterbeach,waterbeach,waterbeach,waterbeach,waterbeach,grasstile,waterbeach,grasstile,grasstile,waterbeach,watertile,watertile,watertile,waterbeach,waterbeach,waterbeach,grasstile,grasstile,waterbeach],
[grasstile,grasstile,waterbeach,waterbeach,watertile,watertile,waterbeach,waterbeach,grasstile,grasstile,waterbeach,waterbeach,watertile,watertile,waterbeach,waterbeach,waterbeach,grasstile,grasstile,waterbeach],
[watertile,grasstile,waterbeach,watertile,watertile,watertile,waterbeach,waterbeach,grasstile,grasstile,waterbeach,watertile,watertile,watertile,waterbeach,waterbeach,waterbeach,grasstile,grasstile,waterbeach],
[watertile,watertile,waterbeach,waterbeach,waterbeach,waterbeach,waterbeach,waterbeach,grasstile,grasstile,waterbeach,waterbeach,watertile,watertile,waterbeach,waterbeach,waterbeach,grasstile,grasstile,waterbeach],
[grasstile,watertile,waterbeach,waterbeach,grasstile,grasstile,waterbeach,waterbeach,grasstile,grasstile,waterbeach,waterbeach,waterbeach,waterbeach,waterbeach,waterbeach,waterbeach,grasstile,grasstile,waterbeach],
[grasstile,grasstile,waterbeach,waterbeach,watertile,watertile,waterbeach,waterbeach,grasstile,grasstile,waterbeach,watertile,watertile,watertile,waterbeach,waterbeach,waterbeach,grasstile,grasstile,waterbeach],
[grasstile,grasstile,waterbeach,waterbeach,watertile,watertile,waterbeach,waterbeach,grasstile,grasstile,waterbeach,grasstile,watertile,watertile,waterbeach,waterbeach,waterbeach,grasstile,grasstile,waterbeach],
[grasstile,grasstile,waterbeach,waterbeach,watertile,watertile,waterbeach,waterbeach,grasstile,grasstile,waterbeach,watertile,grasstile,watertile,waterbeach,waterbeach,waterbeach,grasstile,grasstile,waterbeach],
[grasstile,grasstile,waterbeach,waterbeach,watertile,watertile,waterbeach,waterbeach,grasstile,grasstile,waterbeach,watertile,watertile,watertile,waterbeach,waterbeach,waterbeach,grasstile,grasstile,waterbeach],
[grasstile,grasstile,waterbeach,waterbeach,watertile,watertile,waterbeach,waterbeach,grasstile,grasstile,waterbeach,watertile,watertile,watertile,waterbeach,waterbeach,waterbeach,grasstile,grasstile,waterbeach],
[grasstile,grasstile,waterbeach,waterbeach,watertile,watertile,waterbeach,waterbeach,grasstile,grasstile,waterbeach,watertile,watertile,watertile,waterbeach,waterbeach,waterbeach,grasstile,grasstile,waterbeach],
[grasstile,grasstile,waterbeach,waterbeach,watertile,watertile,waterbeach,waterbeach,grasstile,grasstile,waterbeach,watertile,watertile,watertile,waterbeach,waterbeach,waterbeach,grasstile,grasstile,waterbeach],
]
#Creates surface of the background
map_surface = pygame.Surface((len(tilemap[0])*TILESIZE, len(tilemap)*TILESIZE))
#Display the surface
for y,row in enumerate(tilemap):
for x,tile_surface in enumerate(row):
map_surface.blit(tile_surface,(x*TILESIZE,y*TILESIZE))
total_level_width = len(tilemap[0]) * 40
total_level_height = len(tilemap) * 40
camera = Camera(complex_camera,total_level_width, total_level_height)
#mouse = Mouse()
#allsprites = pygame.sprite.RenderPlain((mouse))
clock = pygame.time.Clock()
while 1:
clock.tick(60)
#Handle Input Events
for event in pygame.event.get():
if event.type == QUIT:
return
elif event.type == KEYDOWN and event.key == K_ESCAPE:
return
screen.fill(BLACK)
#Camera moves.
camera.update()
#Display background.
screen.blit(map_surface, camera.apply(waterrect))
pygame.display.flip()
if __name__ == "__main__":
main()
pygame.quit()
I think that the problem is in the camara function and the apply function, but I have no clue to improve this and make the camara work properly.
Thanks!
It is hard for me to say exactly what is wrong with your sode ( i got too little experience in coding ) , but i just realised movin screen by mouse in my script and i did this :
screen=pygame.display.set_mode((WINDOWWIDTH,WINDOWHEIGHT),32)
Map=pygame.image.load(os.path.join('Image','MAP.png')).convert_alpha()
startX,startY=0,0 # starting coordinates for Map
while 1:
screen.fill(white)
screen.blit(Map,(startX,startY))
for event in pygame.event.get():
if event.type == MOUSEMOTION :
mousepos=pygame.mouse.get_pos()
if WINDOWHEIGHT-mousepos[1]<50: # if y-position of mouse is 50 pixel far from lower edge ..
startY -= 5 # ..move Map by 5 px
if mousepos [1]<50:
startY += 5
if mousepos [0]<50 :
startX += 5
if WINDOWWIDTH - mousepos[0]<50:
startX -= 5
pygame.display.flip()
Hope this can suggest you an idea how to improve your code

Drawing a square wave in pygame

This python code illustrates a sin wave in a pygame window.
I want to draw a square wave in this very same fashion as well, though I have no idea what this code might be or how to draw a square wave / how one is constructed in python.
Does anybody know how I can do this? Is it possible with the same imports or will I need new ones?
Code:
import sys, pygame, math
from pygame.locals import *
# set up a bunch of constants
WHITE = (255, 255, 255)
DARKRED = (128, 0, 0)
RED = (255, 0, 0)
BLACK = ( 0, 0, 0)
BGCOLOR = WHITE
WINDOWWIDTH = 640 # width of the program's window, in pixels
WINDOWHEIGHT = 480 # height in pixels
WIN_CENTERX = int(WINDOWWIDTH / 2) # the midpoint for the width of the window
WIN_CENTERY = int(WINDOWHEIGHT / 2) # the midpoint for the height of the window
FPS = 160 # frames per second to run at
AMPLITUDE = 80 # how many pixels tall the waves with rise/fall.
# standard pygame setup code
pygame.init()
FPSCLOCK = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.set_caption('Trig Waves')
fontObj = pygame.font.Font('freesansbold.ttf', 16)
# variables that track visibility modes
showSine = True
pause = False
xPos = 0
step = 0 # the current input f
posRecord = {'sin': []} # keeps track of the ball positions for drawing the waves
# making text Surface and Rect objects for various labels
sinLabelSurf = fontObj.render('sine', True, RED, BGCOLOR)
sinLabelRect = sinLabelSurf.get_rect()
instructionsSurf = fontObj.render('Press Q to toggle wave. P to pause.', True, BLACK, BGCOLOR)
instructionsRect = instructionsSurf.get_rect()
instructionsRect.left = 10
instructionsRect.bottom = WINDOWHEIGHT - 10
# main application loop
while True:
# event handling loop for quit events
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYUP and event.key == K_ESCAPE):
pygame.quit()
sys.exit()
# check for key presses that toggle pausing and wave visibility
if event.type == KEYUP:
if event.key == K_q:
showSine = not showSine
elif event.key == K_p:
pause = not pause
# fill the screen to draw from a blank state
DISPLAYSURF.fill(BGCOLOR)
# draw instructions
DISPLAYSURF.blit(instructionsSurf, instructionsRect)
# sine wave
yPos = -1 * math.sin(step) * AMPLITUDE
posRecord['sin'].append((int(xPos), int(yPos) + WIN_CENTERY))
if showSine:
# draw the sine ball and label
pygame.draw.circle(DISPLAYSURF, RED, (int(xPos), int(yPos) + WIN_CENTERY), 10)
sinLabelRect.center = (int(xPos), int(yPos) + WIN_CENTERY + 20)
DISPLAYSURF.blit(sinLabelSurf, sinLabelRect)
# draw the waves from the previously recorded ball positions
if showSine:
for x, y in posRecord['sin']:
pygame.draw.circle(DISPLAYSURF, DARKRED, (x, y), 4)
# draw the border
pygame.draw.rect(DISPLAYSURF, BLACK, (0, 0, WINDOWWIDTH, WINDOWHEIGHT), 1)
pygame.display.update()
FPSCLOCK.tick(FPS)
if not pause:
xPos += 0.5
if xPos > WINDOWWIDTH:
xPos = 0
posRecord = {'sin': []}
step = 0
else:
step += 0.008
step %= 2 * math.pi
Python ver.2.6, Pygame ver.1.9.2
I made some modifications to add squared wave.
See places with ### HERE.
import sys, pygame, math
from pygame.locals import *
# set up a bunch of constants
WHITE = (255, 255, 255)
DARKRED = (128, 0, 0)
RED = (255, 0, 0)
BLACK = ( 0, 0, 0)
GREEN = ( 0, 255, 0) ### HERE
BLUE = ( 0, 0, 255) ### HERE
BGCOLOR = WHITE
WINDOWWIDTH = 640 # width of the program's window, in pixels
WINDOWHEIGHT = 480 # height in pixels
WIN_CENTERX = int(WINDOWWIDTH / 2) # the midpoint for the width of the window
WIN_CENTERY = int(WINDOWHEIGHT / 2) # the midpoint for the height of the window
FPS = 160 # frames per second to run at
AMPLITUDE = 80 # how many pixels tall the waves with rise/fall.
# standard pygame setup code
pygame.init()
FPSCLOCK = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.set_caption('Trig Waves')
fontObj = pygame.font.Font('freesansbold.ttf', 16)
# variables that track visibility modes
showSine = True
showSquare = True ### HERE
pause = False
xPos = 0
step = 0 # the current input f
### HERE
posRecord = {'sin': [], 'square': []} # keeps track of the ball positions for drawing the waves
# making text Surface and Rect objects for various labels
### HERE
squareLabelSurf = fontObj.render('square', True, BLUE, BGCOLOR)
squareLabelRect = squareLabelSurf.get_rect()
sinLabelSurf = fontObj.render('sine', True, RED, BGCOLOR)
sinLabelRect = sinLabelSurf.get_rect()
instructionsSurf = fontObj.render('Press Q to toggle wave. P to pause.', True, BLACK, BGCOLOR)
instructionsRect = instructionsSurf.get_rect()
instructionsRect.left = 10
instructionsRect.bottom = WINDOWHEIGHT - 10
### HERE
yPosSquare = AMPLITUDE # starting position
# main application loop
while True:
# event handling loop for quit events
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYUP and event.key == K_ESCAPE):
pygame.quit()
sys.exit()
# check for key presses that toggle pausing and wave visibility
if event.type == KEYUP:
if event.key == K_q:
showSine = not showSine
elif event.key == K_p:
pause = not pause
# fill the screen to draw from a blank state
DISPLAYSURF.fill(BGCOLOR)
# draw instructions
DISPLAYSURF.blit(instructionsSurf, instructionsRect)
# sine wave
yPos = -1 * math.sin(step) * AMPLITUDE
posRecord['sin'].append((int(xPos), int(yPos) + WIN_CENTERY))
if showSine:
# draw the sine ball and label
pygame.draw.circle(DISPLAYSURF, RED, (int(xPos), int(yPos) + WIN_CENTERY), 10)
sinLabelRect.center = (int(xPos), int(yPos) + WIN_CENTERY + 20)
DISPLAYSURF.blit(sinLabelSurf, sinLabelRect)
# draw the waves from the previously recorded ball positions
if showSine:
for x, y in posRecord['sin']:
pygame.draw.circle(DISPLAYSURF, DARKRED, (x, y), 4)
### HERE - drawing horizontal lines
# square
posRecord['square'].append((int(xPos), int(yPosSquare) + WIN_CENTERY))
if showSquare:
# draw the sine ball and label
pygame.draw.circle(DISPLAYSURF, GREEN, (int(xPos), int(yPosSquare) + WIN_CENTERY), 10)
squareLabelRect.center = (int(xPos), int(yPosSquare) + WIN_CENTERY + 20)
DISPLAYSURF.blit(squareLabelSurf, squareLabelRect)
# draw the waves from the previously recorded ball positions
if showSquare:
for x, y in posRecord['square']:
pygame.draw.circle(DISPLAYSURF, BLUE, (x, y), 4)
# draw the border
pygame.draw.rect(DISPLAYSURF, BLACK, (0, 0, WINDOWWIDTH, WINDOWHEIGHT), 1)
pygame.display.update()
FPSCLOCK.tick(FPS)
if not pause:
xPos += 0.5
if xPos > WINDOWWIDTH:
#sine ### HERE
xPos = 0
posRecord['sin'] = []
step = 0
# square ### HERE
yPosSquare = AMPLITUDE
posRecord['square'] = []
else:
#sine ### HERE
step += 0.008
#step %= 2 * math.pi
# square ### HERE
# jump top and bottom every 100 pixels
if xPos % 100 == 0:
yPosSquare *= -1
# add vertical line
for x in range(-AMPLITUDE, AMPLITUDE):
posRecord['square'].append((int(xPos), int(x) + WIN_CENTERY))

Pygame, how to draw a shape to the screen and delete the previous surface?

So I have this code, and it does what it's supposed to fine. What I want it to do is randomly scale the square by different amounts, which it does. My problem lies with the blit function, my square only seems to scale up because blit doesn't delete the old shape it just copies the new one to the surface.
How can I make the shape expand and shrink, and not just expand?
My code:
import sys, random, pygame
from pygame.locals import *
pygame.init()
w = 640
h = 480
screen = pygame.display.set_mode((w,h))
morphingShape = pygame.Surface((20,20))
morphingShape.fill((255, 137, 0)) #random colour for testing
morphingRect = morphingShape.get_rect()
def ShapeSizeChange(shape, screen):
x = random.randint(-21, 20)
w = shape.get_width()
h = shape.get_height()
if w + x > 0 and h + x > 0:
shape = pygame.transform.smoothscale(shape, (w + x, h + x))
else:
shape = pygame.transform.smoothscale(shape, (w - x, h - x))
shape.fill((255, 137, 0))
rect = shape.get_rect()
screen.blit(shape, rect)
return shape
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
morphingShape = ShapeSizeChange(morphingShape, screen)
pygame.display.update()
On every frame (each iteration of the While loop) you should erase the screen. By default the screen (window) color is black, so you should clear the screen by calling screen.fill( (0,0,0) ). Below is the full code, now working as you expect:
import sys, random, pygame
from pygame.locals import *
pygame.init()
w = 640
h = 480
screen = pygame.display.set_mode((w,h))
morphingShape = pygame.Surface((20,20))
morphingShape.fill((255, 137, 0)) #random colour for testing
morphingRect = morphingShape.get_rect()
# clock object that will be used to make the animation
# have the same speed on all machines regardless
# of the actual machine speed.
clock = pygame.time.Clock()
def ShapeSizeChange(shape, screen):
x = random.randint(-21, 20)
w = shape.get_width()
h = shape.get_height()
if w + x > 0 and h + x > 0:
shape = pygame.transform.smoothscale(shape, (w + x, h + x))
else:
shape = pygame.transform.smoothscale(shape, (w - x, h - x))
shape.fill((255, 137, 0))
rect = shape.get_rect()
screen.blit(shape, rect)
return shape
while True:
# limit the demo to 50 frames per second
clock.tick( 50 );
# clear screen with black color
# THIS IS WHAT WAS REALLY MISSING...
screen.fill( (0,0,0) )
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
morphingShape = ShapeSizeChange(morphingShape, screen)
pygame.display.update()
Note that just the addition of screen.fill( (0,0,0) ) solves your question.

Categories