Python 7 Levels, L6, Unit 62, Event Input

Powered By EmbedPress

Exercise 1:

import pygame

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 or self.rect.bottom > height:
            self.speed[1] = -self.speed[1]

pygame.init()
pygame.key.set_repeat(100,50)
clock = pygame.time.Clock()
size = width,height = 640,480
screen = pygame.display.set_mode(size)
screen.fill([255,255,255])
ball = Ball("ball.png",[320,240],[2,-2])
pygame.display.update()

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_UP:
                ball.rect.top -= 5
            elif event.key == pygame.K_DOWN:
                ball.rect.top += 5
    screen.blit(ball.image,ball.rect)
    pygame.display.update()
    screen.fill([255,255,255])
    clock.tick(30)
pygame.quit()

Exercise 2:

import pygame

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 or self.rect.bottom > height:
            self.speed[1] = -self.speed[1]

pygame.init()
pygame.key.set_repeat(100,50)
clock = pygame.time.Clock()
size = width,height = 640,480
screen = pygame.display.set_mode(size)
screen.fill([255,255,255])
ball = Ball("ball.png",[320,240],[2,-2])
pygame.display.update()

held_down = False
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        '''elif event.type == pygame.MOUSEMOTION:
            ball.rect.center = event.pos'''
        if event.type == pygame.MOUSEBUTTONDOWN:
            held_down = True
        '''elif event.type == pygame.MOUSEBUTTONUP:
            held_down = False'''
        '''elif event.type == pygame.MOUSEMOTION:
            if held_down:
                ball.rect.center = event.pos'''
            
    screen.blit(ball.image,ball.rect)
    pygame.display.update()
    screen.fill([255,255,255])
    clock.tick(30)
pygame.quit()

Exercise 3:

import pygame

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 or self.rect.bottom > height:
            self.speed[1] = -self.speed[1]

pygame.init()
pygame.key.set_repeat(100,50)
clock = pygame.time.Clock()
size = width,height = 640,480
screen = pygame.display.set_mode(size)
screen.fill([255,255,255])
ball = Ball("ball.png",[320,240],[2,-2])
pygame.display.update()

held_down = False
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:#鼠标按键按下
            held_down = True
        elif event.type == pygame.MOUSEBUTTONUP:#鼠标按键松开
            held_down = False
        elif event.type == pygame.MOUSEMOTION:#鼠标移动
            if held_down:
                ball.rect.center = event.pos
        
            
    screen.blit(ball.image,ball.rect)
    pygame.display.update()
    screen.fill([255,255,255])
    clock.tick(30)
pygame.quit()

Exercise 4:

import pygame

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 or self.rect.bottom > height:
            self.speed[1] = -self.speed[1]

pygame.init()
pygame.key.set_repeat(100,50)
clock = pygame.time.Clock()
size = width,height = 640,480
screen = pygame.display.set_mode(size)
screen.fill([255,255,255])
ball = Ball("ball.png",[320,240],[2,-2])
pygame.display.update()
pygame.time.set_timer(pygame.USEREVENT,1000)
direction = 1
held_down = False
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.USEREVENT:
            ball.rect.centery = ball.rect.centery + (30*direction)
            if ball.rect.top < 0 or ball.rect.bottom > height:
                direction = -direction
            
    screen.blit(ball.image,ball.rect)
    pygame.display.update()
    screen.fill([255,255,255])
    clock.tick(30)
pygame.quit()

Homework:

import pygame
import random

class Ball_bat(pygame.sprite.Sprite):
    def __init__(self,image_file,location):
        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

pygame.init()
clock = pygame.time.Clock()
pygame.time.set_timer(pygame.USEREVENT,200)
size = width,height = 640,480
screen = pygame.display.set_mode(size)
screen.fill([255,255,255])
ball = Ball_bat("desk_ball.png",[320,0])
bat = Ball_bat("bat.png",[320,460])
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
            if event.key == pygame.K_LEFT:
                bat.rect.centerx -= 10
        elif event.type == pygame.USEREVENT:
            ball.rect.centery += 30
            if ball.rect.bottom > height:
                done = True
    if pygame.sprite.collide_rect(ball,bat):
        ball.rect.left,ball.rect.top = random.randint(0,600),0
    screen.blit(bat.image,bat.rect)
    if not done:
        screen.blit(ball.image,ball.rect)
    pygame.display.update()
    screen.fill([255,255,255])
    clock.tick(30)
pygame.quit()

dahan1999

1 Comment

Leave a Reply

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

Related Posts