Name: [17666] Kenny X
Member: 83 months
Authored: 5 videos
Description: A young Python programmer ...

Pygame tutorial - Gameplay [ID:1793] (3/3)

in series: Using Pygame for Games

video tutorial by Kenny X, added 02/10

(Showmedo is undergoing major changes. To report any problems viewing the videos please email us and include browser and OS specifics. Cheers - Kyran.)

In this tutorial, you will learn how to add actual gameplay in your game.

You will learn about MOUSEBUTTON and how to make mouse input. It will be an extension of the previous tutorial, where when you click the ball it will go faster and your score will increase.

#!/usr/bin/env python

import sys
import pygame
from pygame.locals import *

#---Our variables
black = (0,0,0)
size = (600, 400)
speed = [3,3]
score = 0

#---init pygame

clock = pygame.time.Clock()

screen = pygame.display.set_mode(size)

ball = pygame.image.load("ball.png").convert()
ballrect = ball.get_rect()

#---main loop
while True:
    for event in pygame.event.get():
        if event.type == MOUSEBUTTONDOWN:
            if ballrect.collidepoint(event.pos):
                score += 1
                if speed[0] > -1 or speed[1] > -1:
                    speed[0] += 1
                    speed[1] += 1
                    speed[0] -= 1
                    speed[1] -= 1
                score -= 1
        if event.type == QUIT:
    ballrect = ballrect.move(speed)
    #---check if the ball collides
    if ballrect.left < 0 or ballrect.right > size[0]:
        speed[0] = -speed[0]
    if < 0 or ballrect.bottom > size[1]:
        speed[1] = -speed[1]
    if pygame.font:
        font = pygame.font.Font(None, 26)
        text = font.render("%d Score" % score, True, (255, 0, 0))
        textpos = text.get_rect(centerx=screen.get_width()/2)
        screen.blit(text, textpos)
    screen.blit(ball, ballrect)

Got any questions?

Get answers in the ShowMeDo Learners Google Group.

Video statistics:

  • Video's rank shown in the most popular listing
  • Video plays: 36 (since July 30th)
  • Plays in last week: 0
  • Published: 78 months ago

Thank-yous, questions and comments

If this video tutorial was helpful please take some time to say thank-you to the authors for their hard work. Feel free to ask questions. Let the author know why their video tutorial was useful - what are you learning about? Did the video tutorial save you time? Would you like to see more?

You may also want to see our ShowMeDo Google Group to speak to our active users and authors.

Your email address will not be published.

Show some quick comments >>

All comments excluding tick-boxed quick-comments

good job. Good video.

4. women's mbt shoes shape up black size 6 Tue, 27 Jul 2010 07:01

Thanks for a nice and genuine video =)

3. canada goose jacket canada Mon, 01 Mar 2010 09:33

For change speed of the ball:

speed[0] += speed[0] / abs(speed[0])

speed[1] += speed[1] / abs(speed[1])

or in one line:

speed = [speed[0] + (speed[0] / abs(speed[0])), speed[0] + (speed[1] / abs(speed[1]))]

Because your code is:

1. long

2. wrong:

- [1,1] => [2,2]

- [-1, -1] => [-2, -2]

- [1, -1] => [2, 0]

- [-1, 1] => [0, 2]

But good video ^_^

Lovin' it. So much fun to be had, so little time...

Sorry about the bad audio syncing!

Showmedo is a peer-produced video-tutorials and screencasts site for free and open-source software (FOSS)- with the exception of some club videos, the large majority are free to watch and download.

how to help » about » faq »

Educating the Open-source Community With Showmedo

Although as important as the software it supports, education and documentation are relatively neglected in the Open-source world. Coders love to code, and explaining how best to use or improve the software tends to be deferred or even sidelined.

At Showmedo we believe the community can play a vital role here and also say thanks for the tools and software that make our lives easier. If you have a piece of software you love or a programming langugage you are enthusiastic about, why not make a screencast showing others how to use it? All the stuff you wish you'd been told, the tips, tricks, insights that would have saved you time and frustration.

Screencasting is easier than you think, and we're happy to help you. You can emailus for advice or just use some of the how-to screencasts on the site. This screencasting learning-pathis a good place to start.

Kudos and Thanks for Kenny

Basics of gedit

THX for your video
76 months ago

By the Same Author



Showmedo's development is fairly rapid and bugs will inevitably creep in. If you have any problems please drop us a line using the contact address below. Likewise, any suggestions for improvements to the site are gratefully received.