Exercise:
import pygame
import random
class Ball(pygame.sprite.Sprite):
def __init__(self,image_file,location,speed):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image_file)
self.rect = self.image.get_rect()
self.rect.left,self.rect.top = location
self.speed = speed
def move(self):
self.rect = self.rect.move(self.speed)
if self.rect.left < 0 or self.rect.right > width:
self.speed[0] = -self.speed[0]
if self.rect.top < 0 and (self.rect.left < 240 or self.rect.right > 400) :
self.speed[1] = -self.speed[1]
pygame.init()
clock = pygame.time.Clock()
pygame.key.set_repeat(500,50)
size = width,height = 640,480
screen = pygame.display.set_mode(size)
screen.fill([255,255,255])
ball = Ball("desk_ball.png",[320,240],[10,8])
bat = Ball("bat.png",[320,460],[0,0])
goal = Ball("goal.png",[240,0],[0,0])
screen.blit(ball.image,ball.rect)
pygame.display.update()
done = False
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
'''elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
bat.rect.centerx += 10
elif event.key == pygame.K_LEFT:
bat.rect.centerx -= 10'''
if event.type == pygame.MOUSEMOTION:
bat.rect.centerx = event.pos[0]
if not done:
ball.move()
if pygame.sprite.collide_rect(ball,bat):
ball.speed[1] = -8
if pygame.sprite.collide_rect(ball,goal):
ball.speed[1] = 8
screen.blit(ball.image,ball.rect)
screen.blit(bat.image,bat.rect)
if ball.rect.bottom > height:
done = True
else:
over_font = pygame.font.Font(None,50)
over_surf = over_font.render("Game over",1,[255,0,0])
screen.blit(over_surf,[240,240])
screen.blit(goal.image,goal.rect)
pygame.display.update()
clock.tick(20)
screen.fill([255,255,255])
pygame.quit()
Homework:
import pygame
import random
class Ball(pygame.sprite.Sprite):
def __init__(self,image_file,location,speed):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image_file)
self.rect = self.image.get_rect()
self.rect.left,self.rect.top = location
self.speed = speed
def move(self):
self.rect = self.rect.move(self.speed)
if self.rect.left < 0 or self.rect.right > width:
self.speed[0] = -self.speed[0]
if self.rect.top < 0 and (self.rect.left < 240 or self.rect.right > 400) :
self.speed[1] = -self.speed[1]
pygame.init()
clock = pygame.time.Clock()
pygame.key.set_repeat(500,50)
size = width,height = 640,480
screen = pygame.display.set_mode(size)
screen.fill([255,255,255])
ball = Ball("desk_ball.png",[320,240],[10,8])
bat = Ball("bat.png",[320,460],[0,0])
goal = Ball("goal.png",[240,0],[0,0])
screen.blit(ball.image,ball.rect)
pygame.display.update()
score = 0
lives = 3
done = False
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
'''elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
bat.rect.centerx += 10
elif event.key == pygame.K_LEFT:
bat.rect.centerx -= 10'''
if event.type == pygame.MOUSEMOTION:
bat.rect.centerx = event.pos[0]
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_y and lives == 0:
lives = 3
done = False
elif event.key == pygame.K_n and lives == 0:
running = False
if not done:
ball.move()
if pygame.sprite.collide_rect(ball,bat):
ball.speed[1] = -8
if pygame.sprite.collide_rect(ball,goal):
score += 1
ball.speed[1] = 8
screen.blit(ball.image,ball.rect)
screen.blit(bat.image,bat.rect)
for num in range(lives-1):
screen.blit(ball.image,[600-num*40,0])
if ball.rect.bottom > height:
lives -= 1
ball.rect.left,ball.rect.top = 320,240
if lives == 0:
done = True
else:
over_font = pygame.font.Font(None,50)
over_surf = over_font.render("Game over",1,[255,0,0])
screen.blit(over_surf,[240,240])
yn_font = pygame.font.Font(None,40)
yn_surf = yn_font.render("Y:continue N:quit",1,[255,0,0])
screen.blit(yn_surf,[210,280])
score_font = pygame.font.Font(None,40)
score_surf = score_font.render("score: "+str(score),1,[255,0,0])
screen.blit(score_surf,[0,0])
screen.blit(goal.image,goal.rect)
pygame.display.update()
clock.tick(20)
screen.fill([255,255,255])
pygame.quit()