Python 7 Levels, L6, Unit 63, Table Tennis Game, I

Powered By EmbedPress

Last Homework:

import pygame
import random

class Ball(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.key.set_repeat(500,50)
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("desk_ball.png",[320,0])
bat = Ball("bat.png",[320,460])
screen.blit(ball.image,ball.rect)
pygame.display.update()

hid = 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
        elif event.type == pygame.USEREVENT:
            ball.rect.centery += 30
    if pygame.sprite.collide_rect(ball,bat):
        ball.rect.left,ball.rect.top = random.randint(0,600),0
    if not hid:  
        screen.blit(ball.image,ball.rect)
    screen.blit(bat.image,bat.rect)
    pygame.display.update()
    clock.tick(20)
    screen.fill([255,255,255])
pygame.quit()

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:
            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,0],[10,8])
bat = Ball("bat.png",[320,460],[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] = -ball.speed[1]
        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,[260,240])
    pygame.display.update()
    clock.tick(20)
    screen.fill([255,255,255])
pygame.quit()

dahan1999

1 Comment

Leave a Reply

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

Related Posts