Python 7 Levels, L6, Unit 57, Graphics and Basic Animation

Powered By EmbedPress

Exercise 1:

import pygame
pygame.init()

screen = pygame.display.set_mode([640,480])
screen.fill([255,255,255])
my_rect = pygame.Rect(250,100,300,200)
pygame.draw.rect(screen,[255,0,0],my_rect,0)
pygame.display.update()

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
pygame.quit()

Exercise 2:

import pygame
pygame.init()

screen = pygame.display.set_mode([640,480])
screen.fill([255,255,255])
dots = [[320,440],[200,360],[150,270],[160,190],[250,140],[320,190],[390,140],[480,190],[490,270],[440,360]]
pygame.draw.lines(screen,[255,0,0],True,dots,1)
pygame.display.update()

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
pygame.quit()

Exercise 3:

import pygame
pygame.init()

screen = pygame.display.set_mode([640,480])
screen.fill([255,255,255])
font1 = pygame.font.Font(None,50)
f1 =font1.render("hello world",1,[255,0,0])
screen.blit(f1,[100,100])
pygame.display.update()

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
pygame.quit()

Exercise 4:

import pygame
pygame.init()
from pathlib import Path

screen = pygame.display.set_mode([640,480])
screen.fill([255,255,255])
img = pygame.image.load(Path("image") / "ball.png")
screen.blit(img,[100,100])
pygame.display.update()

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
pygame.quit()

Exercise 5:

import pygame
pygame.init()
from pathlib import Path

screen = pygame.display.set_mode([640,480])
screen.fill([255,255,255])
img = pygame.image.load(Path("image") / "ball.png")

running = True
x=100
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    if x < 301:
        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+=5

pygame.quit()

Homework:

import pygame
import time
from pathlib import Path

pygame.init()

screen = pygame.display.set_mode([640,480])
screen.fill([255,255,255])
img = pygame.image.load(Path("image") / "ball.png")
start_time = time.time()

running = True
y=0
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    if y <= 480:
        screen.blit(img, [270, y])
        pygame.display.update()
        pygame.time.delay(5)
        pygame.draw.rect(screen, [255, 255, 255], [100, y, 100, 100], 0)
        y+=1
    if y==481:
        end_time = time.time()
        t = end_time - start_time
        font1 = pygame.font.Font(None, 50)
        text1 = font1.render("time:" + str(t) + "s", 1, [255, 0, 0])
        screen.blit(text1, [100, 240])
        pygame.display.update()
        y+=1
pygame.quit()

dahan1999

1 Comment

Leave a Reply

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

Related Posts