Introduction: Breakout RaspberryPi

This instructable is video game of breakout on RaspberryPi.

It is easy to make this video game.

Let's make and fun!!

Step 1: Materials

1. RaspberryPi (I used RasberryPi rev2)

※include keyboard,mause,monitor,SDcard

2. Tact switch × 4

3. Electrical resistor × 4

4. Some jumper wires

5. Breadboard

Step 2: Making Controllor

Firstly,you will make a controllor of this video game.

There are 4 buttons to operate video game in this controllor.

Please prepare breadboard,tactswiches,electrical resistores, and some jumperwires.

This is a circuit diagram.

Step 3: Programing

Next is programing of breakout.

1. Start up RaspberryPi and IDLE(python 2.7)

2. Copy and paste this code.

<p>import RPi.GPIO as GPIO<br>import pygame
from pygame.locals import*
from sys import exit
import time</p><p>class blockC(object):</p><p>    def __init__(self,name,image,pos):</p><p>        self.name = name
        self.alive = True
        self.image =image
        self.speed = 0.
        self.location = pos</p><p>    def render(self,surface):</p><p>        if self.alive:
            x,y = self.location
            w,h = self.image.get_size()
            surface.blit(self.image,(x,y))</p><p>    def hit(self):
        
        self.alive = False</p><p>pygame.init()</p><p>#Circle_picture
radius=6
display = (radius*2,radius*2)
color=(0,0,0)
pos = (radius,radius)</p><p>screen=pygame.display.set_mode(display,0,32)
screen.fill((255,255,255))</p><p>pygame.draw.circle(screen,color,pos,radius)</p><p>pygame.image.save(screen,"circle.bmp")</p><p>#board_picture
height=6
length=80
color=(0,0,0)</p><p>screen=pygame.display.set_mode((length,height),0,32)
screen.fill((color))</p><p>pygame.image.save(screen,"board.bmp")</p><p>#block_picture
height=20
length=40
color=(0,0,0)</p><p>screen=pygame.display.set_mode((length,height),0,32)
screen.fill((color))</p><p>pygame.image.save(screen,"block.bmp")</p><p>#picture
board_image_filename ='board.bmp'
circle_image_filename = 'circle.bmp'
block_image_filename = 'block.bmp'</p><p>#GPIOsetup
GPIO.setmode(GPIO.BCM)
GPIO.setup(22, GPIO.IN)
GPIO.setup(23, GPIO.IN)
GPIO.setup(24, GPIO.IN)
GPIO.setup(25, GPIO.IN)</p><p>#pygame,window,font,clock.etc
window=pygame.display.set_mode((840,480))
pygame.display.set_caption("block_game")
font = pygame.font.SysFont(None,80)
font2 = pygame.font.SysFont(None,40)</p><p>board = pygame.image.load(board_image_filename).convert()
circle= pygame.image.load(circle_image_filename).convert_alpha()
block= pygame.image.load(block_image_filename).convert_alpha()
end = font.render("GAME OVER",True,(0,0,255))
complete = font.render("Complete!!",True,(255,100,0))
start = font.render("Push the start button",True,(255,100,20))
point = font2.render("SCORE",True,(255,255,255))
again1 = font2.render("Push the restart button",True,(60,210,16))
again2 = font2.render("if you would like to play again.",True,(60,210,16))</p><p>clock = pygame.time.Clock()</p><p>#roop1
while 1:
    #board_init
    board_pos = 320.</p><p>    #block_init
    blocks = {}
    block_pos_y = 30.
    i = 1
    for num2 in range(1,6):
        block_pos_x = 45
        for num in range(1,12):
            blocks[i] = blockC(i,block,(block_pos_x,block_pos_y))
            i += 1
            block_pos_x += 50
        block_pos_y += 30
    alive_blocks = len(blocks)</p><p>    #start_init
    S = 1
    score = 0</p><p>    #cicle_init
    circle_pos_x,circle_pos_y = 0.,0.
    speed_x,speed_y = 0.,0.</p><p>    #roop2
    while alive_blocks >= 1:
        for event in pygame.event.get():
            if event.type == QUIT:
                exit()</p><p>        #board
        if GPIO.input(22):
            board_pos += 10.
        elif GPIO.input(23):
            board_pos -= 10.        
        if board_pos > 560:
            board_pos = 560.
        elif board_pos < 0:
            board_pos = 0.</p><p>        #circle
        time_passed = clock.tick(40)
        time_passed_seconds = time_passed / 1000.0
        circle_pos_x += speed_x * time_passed_seconds
        circle_pos_y += speed_y * time_passed_seconds
        if circle_pos_x > 640 - circle.get_width():
            speed_x = -speed_x
            circle_pos_x = 640 - circle.get_width()
        elif circle_pos_x < 0:
            speed_x = -speed_x
            circle_pos_x = 0.    
        if circle_pos_y > 480 - circle.get_height():
            break
        elif circle_pos_y < 0:
            speed_y = -speed_y
            circle_pos_y = 0.</p><p>        #start
        if S:
            circle_pos_x,circle_pos_y = (board_pos+(board.get_width()/2)-5),430.
            speed_x,speed_y = 0.,0.
            if GPIO.input(24):
                S=0
                speed_x,speed_y = 1.,-170.
        else:
            #refrect_circle_board
            if 440<=(circle_pos_y+circle.get_height())<=446:
                if (board_pos-circle.get_width()/2) <= circle_pos_x < (board_pos+16) or (board_pos+64) < circle_pos_x <= (board_pos+board.get_width()-circle.get_width()/2):
                    speed_x = (speed_x/abs(speed_x))*193
                    speed_y = -200
                elif (board_pos+16) <= circle_pos_x < (board_pos+32) or (board_pos+48) < circle_pos_x <= (board_pos+64):
                    speed_x = (speed_x/abs(speed_x))*173
                    speed_y = -170
                elif (board_pos+32) <= circle_pos_x <= (board_pos+48):
                    speed_x = (speed_x/abs(speed_x))*133
                    speed_y = -170</p><p>            #reflect_circle_block and delete
            offset = 3
            for k in range(1,56):
                if blocks[k].alive:
                    x,y = blocks[k].location
                    if (x-offset) < (circle_pos_x+circle.get_width()) < (x+offset) and (circle_pos_y-block.get_height()-offset)</p><p>        #display
        window.fill(pygame.Color(255,255,255))
        pygame.draw.rect(window,(0,0,0),(640,0,200,480))
        window.blit(point,(690,200))
        score2 = font2.render("%i"%(score),True,(255,255,255))
        window.blit(score2,(690,250))
        window.blit(circle,(circle_pos_x,circle_pos_y))
        window.blit(board,(board_pos,440))
        if S:
            window.blit(start,(40,200))
        for k in range(1,56):
            blocks[k].render(window)
        
        pygame.display.update()</p><p>    #result
    while not GPIO.input(25):
        if alive_blocks >= 1:
            window.blit(end,(150,200))
        else:
            window.blit(complete,(180,200))
        window.blit(again1,(50,300))
        window.blit(again2,(200,350))
        pygame.display.update()</p>

Step 4: Run and Fun!!

Please run that code.

How to play this video game:

Pin 22: Right button

Pin 23:Left button

Pin 24:Start button

Pin 25:Restart button

This is my website: lumenbolk.com

That's all. Thank you.

Comments

author
MsSweetSatisfaction made it! (author)2014-09-22

Interesting! Thanks for sharing!