Page 1 of 1

Introduction to pygame : Part-1

#1 atik97  Icon User is offline

  • ???
  • member icon

Reputation: 143
  • View blog
  • Posts: 715
  • Joined: 16-September 08

Posted 28 January 2009 - 11:10 AM

Game development with python

Python provides an environment which is so favourable for efficient programming because of it’s simple syntax, rapid development, enriched library, open source code and platform-independent nature. Same way for game development, python has some highly enriched library. In this tutorial we will see how to develop a game with pygame.

Pygame

Pygame is a cross-platform library designed to make it easy to write multimedia software such as games in python. By installing pygame, you can have access to an environment which is favouriable for rapid development of 2d and 3d games. Pygame requires the Python language and SDL multimedia library.

Installing Pygame

First of all you need to have python installed on your system. Then you need to install pygame. You can download pygame from it’s website http://www.pygame.org

Develop your first game with pygame

Now we have pygame installed on our system. So, we can move forward to develop our first game with pygame. We will start with a simple game which do nothing than bouncing a ball when it runs. Let’s start our journey-

First you need to import pygame. We also need to import sys module because we need to use functionality of it.

import sys
import pygame
pygame.init()



Here we have imported all the available pygame modules and initializes these modules. Now we need to create a graphical window.

size=width, height=450, 280
speed=[1,1]
color=1,1,1
screen=pygame.display.set_mode(size)



Now we need to load our image, which is a ball. The supported formats include jpg, png, tga and gif. Here is how to load the ball-

ball=pygame.image.load(“ball.gif”)



Pygame comes with object type named rect, which represents a rectangular area. We will create a variable with this object.

ballAction=ball.get_rect()



This time we will use infinite loop which will move the ball and check the user input. If QUIT event is occured, the program will be exit.

while True:
	  for event in pygame.event.get():
		  if event.type == pygame.QUIT: sys.exit()

	  ballAction = ballAction.move(speed)
	  if ballAction.left < 0 or ballAction.right > width:
	   	speed[0] = -speed[0]
	  if ballAction.top < 0 or ballAction.bottom > height:
		  speed[1] = -speed[1]

	  screen.fill(color)
	  screen.blit(ball, ballAction)
	  pygame.display.flip()



Here we have specified the current speed to the variable ballAction to move it. The ball is moved inside the screen and when it is moved to outside the screen, we have just reversed the direction. Then we have filled the screen with a black RGB color to erase the screen. This is done because to animate the movement of ball. Next we have drawn the ball image into the screen by ‘screen.blit()’ method. Finally we have updated the visible display by the method ‘pygame.display.flip()’.

Thats all. Don’t forget to put the image “ball.gif” in the same directory of your code.
So, take a look at the code-

import sys
import pygame
pygame.init()
size=width, height=450, 280
speed=[1,1]
color=1,1,1
screen=pygame.display.set_mode(size)
ball=pygame.image.load(“ball.gif”)
ballAction=ball.get_rect()
while True:
	  for event in pygame.event.get():
		  if event.type == pygame.QUIT: sys.exit()

	  ballAction = ballAction.move(speed)
	  if ballAction.left < 0 or ballAction.right > width:
	   	speed[0] = -speed[0]
	  if ballAction.top < 0 or ballAction.bottom > height:
		  speed[1] = -speed[1]

	  screen.fill(color)
	  screen.blit(ball, ballAction)
	  pygame.display.flip()



Hope you will enjoy it.

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Introduction to pygame : Part-1

#2 Auzzie  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 43
  • View blog
  • Posts: 573
  • Joined: 20-January 09

Posted 01 February 2009 - 04:13 AM

Thank you for this look into pygame, it is really giving me a start on python game making. I hope to see more, i do have one question though, what would be the best way to get the ball to randomly bounce around the window?
Was This Post Helpful? 0
  • +
  • -

#3 Metal Koala  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 13-August 09

Posted 13 August 2009 - 09:09 PM

i cannot seem to be able to make the ball it always gives me a error

This post has been edited by Metal Koala: 13 August 2009 - 09:10 PM

Was This Post Helpful? 0
  • +
  • -

#4 HealyHQ  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-July 09

Posted 13 February 2010 - 10:56 AM

View PostAuzzie, on 01 February 2009 - 03:13 AM, said:

Thank you for this look into pygame, it is really giving me a start on python game making. I hope to see more, i do have one question though, what would be the best way to get the ball to randomly bounce around the window?


Don't know if it's the "best" way, but it works. :)

#!/usr/bin/env python

import pygame, sys

pygame.init()

screen = pygame.display.set_mode([640,480])
screen.fill([255,255,255])
my_ball = pygame.image.load("beach_ball.png")
x = 50
y = 50
x_speed = 8
y_speed = 8

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

    pygame.time.delay(20)
    pygame.draw.rect(screen, [255,255,255], [x,y,90,90], 0)

    x += x_speed
    y += y_speed
    # if ball exits screen width or height
    # it bounces to opposite direction! 
    if x > screen.get_width() - 90 or x < 0:
        x_speed = - x_speed
    if y > screen.get_height() - 90 or y < 0:
        y_speed = - y_speed

    screen.blit(my_ball, [x,y])
    pygame.display.flip()


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1