Python pygame module usage example, pythonpygame

Source: Internet
Author: User

Python pygame module usage example, pythonpygame

The example in this article describes the usage of the pygame module in python and shares it with you for your reference. The specific method is as follows:

import pygame, sys from pygame.locals import *  #set up pygame pygame.init()  windowSurface = pygame.display.set_mode((500, 400), 0, 32) pygame.display.set_caption("hello, world")  BLACK = (0, 0, 0) WHITE = (255, 255, 255) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255)  basicFont = pygame.font.SysFont(None, 48) text = basicFont.render("Hello ,world", True, WHITE, BLUE) textRect = text.get_rect()  textRect.centerx = windowSurface.get_rect().centerx textRect.centery = windowSurface.get_rect().centery  windowSurface.fill(WHITE)  pygame.draw.polygon(windowSurface, GREEN, ((146, 0),  (291, 106), (236, 277), (56, 277), (0, 106)))   pygame.draw.line(windowSurface, BLUE, (60, 60), (120,  60), 4)  pygame.draw.line(windowSurface, BLUE, (120, 60), (60,  120))  pygame.draw.line(windowSurface, BLUE, (60, 120), (120,  120), 4)  pygame.draw.circle(windowSurface, BLUE, (300, 50), 20, 0)  pygame.draw.ellipse(windowSurface, RED, (300, 250, 40,  80), 1)   pygame.draw.rect(windowSurface, RED, (textRect.left - 20,  textRect.top - 20, textRect.width + 40, textRect.height + 40))  pixArray = pygame.PixelArray(windowSurface)  pixArray[480][380] = BLACK  del pixArray   windowSurface.blit(text, textRect)   pygame.display.update()  while True:    for event in pygame.event.get():      if event.type == QUIT:        pygame.quit()        sys.exit()  

The image output after running is as follows:

I hope this article will help you with Python programming.


How does Python import the pygame module?

Does the pygame you installed use py2.5?
Or is multiple python installed on your computer?
 
Use the pygame module in Python to implement genie Animation

Create a small surface, and then from the rectangle area selected by blit in the larger image to the small surface.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.