Python 7 Levels, L6, Unit 58, Animation Design

Powered By EmbedPress

Exercise 1:

import pygame
pygame.init()

screen = pygame.display.set_mode([640,480])
screen.fill([255,255,255])
img = pygame.image.load("ball.png")
x = 0
speed = 5
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    screen.blit(img,[x,100])
    pygame.display.update()
    pygame.draw.rect(screen,[255,255,255],[x,100,100,100],0)
    pygame.time.delay(10)
    x += speed
    if x >= 540 or x <= 0:
        speed = -speed
    
pygame.quit()

Exercise 2:

import pygame
pygame.init()

screen = pygame.display.set_mode([640,480])
screen.fill([255,255,255])
img = pygame.image.load("ball.png")
x = 0
y = 0
speed_x = 5
speed_y = 3
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    screen.blit(img,[x,y])
    pygame.display.update()
    pygame.draw.rect(screen,[255,255,255],[x,y,100,100],0)
    pygame.time.delay(10)
    x += speed_x
    y += speed_y
    if x >= 540 or x <= 0:
        speed_x = -speed_x
    if y >= 380 or y <= 0:
        speed_y = -speed_y
    
pygame.quit()

Exercise 3:

import pygame
pygame.init()

screen = pygame.display.set_mode([640,480])
screen.fill([255,255,255])
pygame.draw.line(screen,[0,0,0],[200,0],[80,480],5)
pygame.draw.line(screen,[0,0,0],[440,0],[560,480],5)

def linemiddle():
    points = []
    x = 320
    for y in range(0,480,20):
        points.append([x,y])
        if len(points) == 2:
            pygame.draw.lines(screen,[0,0,0],False,points,5)
            pygame.display.update()
            points = []

linemiddle()
my_car = pygame.image.load("car.jpg")
y = 340
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    screen.blit(my_car,[330,y])
    pygame.display.update()
    pygame.time.delay(200)
    pygame.draw.rect(screen,[255,255,255],[330,y,90,140],0)
    y -= 50
    if y <= -140:
        y = 340
pygame.quit()

Homework:

import pygame
pygame.init()
pygame.time.delay(10000)
screen = pygame.display.set_mode([640,480])
pygame.display.set_caption("Animation")
screen.fill([255,255,255])
num = 1
x = 0
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    screen.fill([255,255,255])
    img = pygame.image.load("a"+str(num)+".jpg")
    screen.blit(img,[x,240])
    pygame.display.update()
    num += 1
    if num == 5:
        num = 1
        x += 50
        if x >= 540:
            x = 0
    pygame.time.delay(200)
pygame.quit()

dahan1999

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts