-->

Welcome to our Coding with python Page!!! hier you find various code with PHP, Python, AI, Cyber, etc ... Electricity, Energy, Nuclear Power

Showing posts with label pygame. Show all posts
Showing posts with label pygame. Show all posts

Tuesday, 3 July 2018

Python | How to install PyGame for Python 3


PyGame is a set of python modules designed for writing games. It uses the SDL library to allow you to create games and multimedia programs in Python. PyGame is highly portable and runs on nearly every platform and operating system. The SDL library, or Simple DirectMedia Layer is a development library written in C that provides low-level access to audio, keyboard, mouse, joystick and graphics hardware. PyGame is what allows us to use SDL with Python.

Python install
If you don’t already have Python3 installed download it from http://www.python.org/downloads/
Make sure you pick the correct installer for your computer, e.g. Windows x86 or Windows x64 or Mac, etc.
When running the installer it should place the installation in the default location, for Python3 version 3.4 on Windows this is:
C:\python34
On Mac OSX it is:
/usr/local/bin/python
On Windows, make sure Python is added to the system path by opening the command prompt (ÿ+ R, ‘cmd’) and type
set path=%path%;C:\python34
Where 34 is equal to your Python 3 version number.
Check the Python 3 install
To check that Python has installed correctly open the command prompt or terminal and simply type
python
If this causes an error, then Python has not installed correctly. If it does not, you should see a new python prompt that looks like this:
>>> 
To exit the Python prompt, press Ctrl + z, then press the Enter key. Then exit the command prompt or terminal.
If you chose you can install a Python IDE or Python editor at this point, before continuing on.
PyGame install
When Python is installed and confirmed to be working you can install PyGame.
For Windows installs:
You can download PyGame from http://www.pygame.org/download.shtml. The version of PyGame to use depends on your version of Python. For example, if you have Python 3.4 installed then the PyGame installer name you need to download will include ‘py3.2’
http://www.htmlpublish.com/newTestDocStorage/DocStorage/ae212c5255bc4891beef0dd44fdd4ce6/How-to-install-PyGame-for-Python-3%5B1%5D_images/How-to-install-PyGame-for-Python-3%5B1%5D2x1.jpg
For example for Windows:
pygame-1.9.2a0.win32-py3.2.msi
If your Python version is more recent than any mentioned by the installers available on the PyGame website you should check http://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame for a later installer build. Again, make sure the version you download matches your operating system e.g. Windows x86 or Windows x64 or Mac OSX.
On Windows, run the installer and allow it to install PyGame in it’s default directory.
On Mac OSX:
Create and add the following to ~/.bash_profile:
# Homebrew binaries now take precedence over Apple defaults
export PATH=/usr/local/bin:$PATH
Install Apple Xcode command line tools:
xcode-select --install
Install XQuartz: http://xquartz.macosforge.org/landing/
Install homebrew:
ruby -e "$(curl –fsSLhttps://raw.github.com/Homebrew/homebrew/go/install)"
Install Python3 "proper" and packages we’ll need for installing PyGame from bitbucket:
brew install python3 hg sdl sdl_image sdl_mixer sdl_ttf portmidi
Install PyGame:
pip3 install hg+http://bitbucket.org/pygame/pygame
Restart the Mac for XQuartz changes
Check the PyGame install
Open a terminal or command prompt again (remember how you did this previously in the “Check the Python 3 install” section). At the Python prompt type:
import pygame
print(pygame.ver)
And then press the Enter key. This should then print the PyGame version number. If it does not or typing either of the above lines produces an error then PyGame is not correctly installed.
When everything is properly installed and working OK, you can go ahead and check out the PyGame Hello World Programme sheet.

Python | Installing the Windows 64-bit Version of Pygame

UPDATE: Apparently, this is no longer necessary. As of February, 2017, you can just run pip install pygame and it will install the right version of pygame for your environment. 
I’m not a game developer, but I’ve been playing with Pygame lately and have been impressed. I plan to use it in one of our Python courses to teach object-oriented programming. I use 64-bit Windows 8, so I have the 64-bit version of Python 3.4. Most of our students are unlikely to be game developers, but because games have obvious visible objects, they provide a nice framework for teaching OOP.
Unfortunately, Pygame doesn’t have an official 64-bit installer, so they recommend you use the 32-bit version of Python. So, I installed the 32-bit version of Python alongside the 64-bit version I already had, but that was a pain as I had to constantly make sure I was running the right one. It also made the 32-bit version the default.
Luckily, Christoph Gohlke of the University of California, Irvine has made a bunch of 64-bit binaries available for Python extension packages, including one for pygame.
The installation file is a wheel file, which can be pretty tricky to install. The documentation is a bit weak, so I’ve laid it out here.
Note that these instructions assume you already have a 64-bit version of Python installed.
To install the Windows 64-bit version of Pygame, which is the only version that will work with the 64-bit version of Python, follow these steps:
  1. Make sure that your Path variable includes both your Python directory and the Scripts directory:
    add-python-to-path
    (Click image to enlarge)
  2. Go to http://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame
  3. Click pygame 1.9.2a0 cp34 none win_amd64.whl (for Python 3.4):
    pygame-64-download
  4. Save the file locally. Do not rename the file as the file name structure is important.
    pygame_whl
  5. Wheel (.whl) files are installed using pip and wheel. So before installing the wheel file, you need to make sure you have an up-to-date version of pip and of wheel. To do this:
    1. Click the Windows icon and select Run:
      windows-run
    2. Type “cmd” in the Run prompt and press OK:
      cmd
    3. PIP is used to unpack wheel files. At the prompt, type “python -m ensurepip”:
      ensurepip
    4. If it tells you the requirement is already satisfied, run the upgrade command: “python –m ensurepip –upgrade”:
      ensurepip-upgrade
    5. Install wheel. At the command prompt, type “pip install wheel”:
      install-wheel
    6. If it tells you the requirement is already satisfied, run the upgrade command: “pip install wheel –upgrade”:
      upgrade-wheel
  6. Now you’re ready to install pygame 1.9.2a0 cp34 none win_amd64.whl. At the command line, make sure you’re in the same directory as the .whl file and then type “pip install pygame-1.9.2a0-cp34-none-win_amd64.whl”:

    If you get an error saying the file name does not exists, it’s likely because you’re running the pip install command from a different directory. Either use navigate to the correct directory (reference) and run the command again or run the command using a full absolute path (e.g., “pip install c:\users\nat\pygame-1.9.2a0-cp34-none-win_amd64.whl”).
Have fun making games with pygame!

Python | Flappy Bird game clone for a beginners' programming class

I'll soon begin teaching a beginners' programming class. It's voluntary, so I thought I'd make it interesting by teaching Python programming and then introduce the kids to Pygame, so that they can make their own games. To try out Pygame (I've never used it before) and to see exactly how easy it is to make a game, I made a clone of Flappy Bird.

What do you think? Could this be made simpler/shorter? Is there anything in there that I shouldn't teach my students?
#!/usr/bin/env python3

"""Flappy Bird, implemented using Pygame."""

import math
import os
from random import randint

import pygame
from pygame.locals import *


FPS = 60
EVENT_NEWPIPE = USEREVENT + 1  # custom event
PIPE_ADD_INTERVAL = 3000       # milliseconds
FRAME_ANIMATION_WIDTH = 3      # pixels per frame
FRAME_BIRD_DROP_HEIGHT = 3     # pixels per frame
FRAME_BIRD_JUMP_HEIGHT = 5     # pixels per frame
BIRD_JUMP_STEPS = 20           # see get_frame_jump_height docstring
WIN_WIDTH = 284 * 2            # BG image size: 284x512 px; tiled twice
WIN_HEIGHT = 512
PIPE_WIDTH = 80
PIPE_PIECE_HEIGHT = BIRD_WIDTH = BIRD_HEIGHT = 32


class PipePair:
    """Represents an obstacle.

    A PipePair has a top and a bottom pipe, and only between them can
    the bird pass -- if it collides with either part, the game is over.

    Attributes:
    x: The PipePair's X position.  Note that there is no y attribute,
        as it will only ever be 0.
    surface: A pygame.Surface which can be blitted to the main surface
        to display the PipePair.
    top_pieces: The number of pieces, including the end piece, in the
        top pipe.
    bottom_pieces: The number of pieces, including the end piece, in
        the bottom pipe.
    """

    def __init__(self, surface, top_pieces, bottom_pieces):
        """Initialises a new PipePair with the given arguments.

        The new PipePair will automatically be assigned an x attribute of
        WIN_WIDTH.

        Arguments:
        surface: A pygame.Surface which can be blitted to the main
            surface to display the PipePair.  You are responsible for
            converting it, if desired.
        top_pieces: The number of pieces, including the end piece, which
            make up the top pipe.
        bottom_pieces: The number of pieces, including the end piece,
            which make up the bottom pipe.
        """
        self.x = WIN_WIDTH
        self.surface = surface
        self.top_pieces = top_pieces
        self.bottom_pieces = bottom_pieces
        self.score_counted = False

    @property
    def top_height_px(self):
        """Get the top pipe's height, in pixels."""
        return self.top_pieces * PIPE_PIECE_HEIGHT

    @property
    def bottom_height_px(self):
        """Get the bottom pipe's height, in pixels."""
        return self.bottom_pieces * PIPE_PIECE_HEIGHT

    def is_bird_collision(self, bird_position):
        """Get whether the bird crashed into a pipe in this PipePair.

        Arguments:
        bird_position: The bird's position on screen, as a tuple in
            the form (X, Y).
        """
        bx, by = bird_position
        in_x_range = bx + BIRD_WIDTH > self.x and bx < self.x + PIPE_WIDTH
        in_y_range = (by < self.top_height_px or
                      by + BIRD_HEIGHT > WIN_HEIGHT - self.bottom_height_px)
        return in_x_range and in_y_range


def load_images():
    """Load all images required by the game and return a dict of them.

    The returned dict has the following keys:
    background: The game's background image.
    bird-wingup: An image of the bird with its wing pointing upward.
        Use this and bird-wingdown to create a flapping bird.
    bird-wingdown: An image of the bird with its wing pointing downward.
        Use this and bird-wingup to create a flapping bird.
    pipe-end: An image of a pipe's end piece (the slightly wider bit).
        Use this and pipe-body to make pipes.
    pipe-body: An image of a slice of a pipe's body.  Use this and
        pipe-body to make pipes.
    """

    def load_image(img_file_name):
        """Return the loaded pygame image with the specified file name.

        This function looks for images in the game's images folder
        (./images/).  All images are converted before being returned to
        speed up blitting.

        Arguments:
        img_file_name: The file name (including its extension, e.g.
            '.png') of the required image, without a file path.
        """
        file_name = os.path.join('.', 'images', img_file_name)
        img = pygame.image.load(file_name)
        # converting all images before use speeds up blitting
        img.convert()
        return img

    return {'background': load_image('background.png'),
            'pipe-end': load_image('pipe_end.png'),
            'pipe-body': load_image('pipe_body.png'),            
            # images for animating the flapping bird -- animated GIFs are
            # not supported in pygame
            'bird-wingup': load_image('bird_wing_up.png'),
            'bird-wingdown': load_image('bird_wing_down.png')}


def get_frame_jump_height(jump_step):
    """Calculate how high the bird should jump in a particular frame.

    This function uses the cosine function to achieve a smooth jump:
    In the first and last few frames, the bird jumps very little, in the
    middle of the jump, it jumps a lot.
    After a completed jump, the bird will have jumped
    FRAME_BIRD_JUMP_HEIGHT * BIRD_JUMP_STEPS pixels high, thus jumping,
    on average, FRAME_BIRD_JUMP_HEIGHT pixels every step.

    Arguments:
    jump_step: Which frame of the jump this is, where one complete jump
        consists of BIRD_JUMP_STEPS frames.
    """
    frac_jump_done = jump_step / float(BIRD_JUMP_STEPS)
    return (1 - math.cos(frac_jump_done * math.pi)) * FRAME_BIRD_JUMP_HEIGHT


def random_pipe_pair(pipe_end_img, pipe_body_img):
    """Return a PipePair with pipes of random height.

    The returned PipePair's surface will contain one bottom-up pipe
    and one top-down pipe.  The pipes will have a distance of
    BIRD_HEIGHT*3.
    Both passed images are assumed to have a size of (PIPE_WIDTH,
    PIPE_PIECE_HEIGHT).

    Arguments:
    pipe_end_img: The image to use to represent a pipe's endpiece.
    pipe_body_img: The image to use to represent one horizontal slice
        of a pipe's body.
    """
    surface = pygame.Surface((PIPE_WIDTH, WIN_HEIGHT), SRCALPHA)
    surface.convert()   # speeds up blitting
    surface.fill((0, 0, 0, 0))
    max_pipe_body_pieces = int(
        (WIN_HEIGHT -            # fill window from top to bottom
        3 * BIRD_HEIGHT -        # make room for bird to fit through
        3 * PIPE_PIECE_HEIGHT) / # 2 end pieces and 1 body piece for top pipe
        PIPE_PIECE_HEIGHT        # to get number of pipe pieces
    )
    bottom_pipe_pieces = randint(1, max_pipe_body_pieces)
    top_pipe_pieces = max_pipe_body_pieces - bottom_pipe_pieces
    # bottom pipe
    for i in range(1, bottom_pipe_pieces + 1):
        surface.blit(pipe_body_img, (0, WIN_HEIGHT - i*PIPE_PIECE_HEIGHT))
    bottom_pipe_end_y = WIN_HEIGHT - bottom_pipe_pieces*PIPE_PIECE_HEIGHT
    surface.blit(pipe_end_img, (0, bottom_pipe_end_y - PIPE_PIECE_HEIGHT))
    # top pipe
    for i in range(top_pipe_pieces):
        surface.blit(pipe_body_img, (0, i * PIPE_PIECE_HEIGHT))
    top_pipe_end_y = top_pipe_pieces * PIPE_PIECE_HEIGHT
    surface.blit(pipe_end_img, (0, top_pipe_end_y))
    # compensate for added end pieces
    top_pipe_pieces += 1
    bottom_pipe_pieces += 1
    return PipePair(surface, top_pipe_pieces, bottom_pipe_pieces)


def main():
    """The application's entry point.

    If someone executes this module (instead of importing it, for
    example), this function is called.
    """

    pygame.init()

    display_surface = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
    pygame.display.set_caption('Pygame Flappy Bird')

    clock = pygame.time.Clock()

    score_font = pygame.font.SysFont(None, 32, bold=True)  # default font

    # the bird stays in the same x position, so BIRD_X is a constant
    BIRD_X = 50
    bird_y = int(WIN_HEIGHT/2 - BIRD_HEIGHT/2)  # center bird on screen

    images = load_images()

    # timer for adding new pipes
    pygame.time.set_timer(EVENT_NEWPIPE, PIPE_ADD_INTERVAL)
    pipes = []

    steps_to_jump = 2
    score = 0
    done = paused = False
    while not done:
        for e in pygame.event.get():
            if e.type == QUIT or (e.type == KEYUP and e.key == K_ESCAPE):
                done = True
                break
            elif e.type == KEYUP and e.key in (K_PAUSE, K_p):
                paused = not paused
            elif e.type == MOUSEBUTTONUP or (e.type == KEYUP and
                    e.key in (K_UP, K_RETURN, K_SPACE)):
                steps_to_jump = BIRD_JUMP_STEPS
            elif e.type == EVENT_NEWPIPE:
                pp = random_pipe_pair(images['pipe-end'], images['pipe-body'])
                pipes.append(pp)

        clock.tick(FPS)
        if paused:
            continue  # don't draw anything

        for x in (0, WIN_WIDTH / 2):
            display_surface.blit(images['background'], (x, 0))

        for p in pipes:
            p.x -= FRAME_ANIMATION_WIDTH
            if p.x <= -PIPE_WIDTH:  # PipePair is off screen
                pipes.remove(p)
            else:
                display_surface.blit(p.surface, (p.x, 0))

        # calculate position of jumping bird
        if steps_to_jump > 0:
            bird_y -= get_frame_jump_height(BIRD_JUMP_STEPS - steps_to_jump)
            steps_to_jump -= 1
        else:
            bird_y += FRAME_BIRD_DROP_HEIGHT

        # because pygame doesn't support animated GIFs, we have to
        # animate the flapping bird ourselves
        if pygame.time.get_ticks() % 500 >= 250:
            display_surface.blit(images['bird-wingup'], (BIRD_X, bird_y))
        else:
            display_surface.blit(images['bird-wingdown'], (BIRD_X, bird_y))

        # update and display score
        for p in pipes:
            if p.x + PIPE_WIDTH < BIRD_X and not p.score_counted:
                score += 1
                p.score_counted = True

        score_surface = score_font.render(str(score), True, (255, 255, 255))
        score_x = WIN_WIDTH/2 - score_surface.get_width()/2
        display_surface.blit(score_surface, (score_x, PIPE_PIECE_HEIGHT))

        pygame.display.update()

        # check for collisions
        pipe_collisions = [p.is_bird_collision((BIRD_X, bird_y)) for p in pipes]
        if (0 >= bird_y or bird_y >= WIN_HEIGHT - BIRD_HEIGHT or
                True in pipe_collisions):
            print('You crashed! Score: %i' % score)
            break
    pygame.quit()


if __name__ == '__main__':
    # If this module had been imported, __name__ would be 'flappybird'.
    # It was executed (e.g. by double-clicking the file), so call main.
    main()

Rank

seo