Teach Your Kids To Code: A Parent-friendly Guide to Python Programming

(vip2019) #1

244 Chapter 10


pic = pygame.image.load("CrazySmile.bmp")
colorkey = pic.get_at((0,0))
pic.set_colorkey(colorkey)
picx = 0
picy = 0
BLACK = (0,0,0)
WHITE = (255,255,255)
timer = pygame.time.Clock()
speedx = 5
speedy = 5
paddlew = 200
paddleh = 25
paddlex = 300
paddley = 550
picw = 100
pich = 100
points = 0
lives = 5
font = pygame.font.SysFont("Times", 24)

while keepGoing: # Game loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
picx += speedx
picy += speedy

if picx <= 0 or picx + pic.get_width() >= 800:
speedx = -speedx
if picy <= 0:
speedy = -speedy
if picy >= 500:
lives -= 1
speedy = -speedy

screen.fill(BLACK)
screen.blit(pic, (picx, picy))

# Draw paddle
paddlex = pygame.mouse.get_pos()[0]
paddlex -= paddlew/2
pygame.draw.rect(screen, WHITE, (paddlex, paddley, paddlew, paddleh))

# Check for paddle bounce
if picy + pich >= paddley and picy + pich <= paddley + paddleh \
and speedy > 0:
Free download pdf