Pygame: Quick Start
I am going to share some Pygame examples in this post.
What is Pygame?
"Pygame is a set of Python modules designed for writing video games"
https://www.pygame.org/wiki/about
Install
$ pip install pygame
Play Pre-made Game
Note: turn off or down the volume before starting the game:
$ python -m pygame.examples.aliens
Hello, World
The following page has the simplest example that I have found so far:
Chess Board
I just wanted to show one more example. Running this code will show the chess board image shown at the top of this page.
import pygame, sys
from pygame.locals import *
num_squares = 8
s = 50 # square length
board_len = s * num_squares
pygame.init()
screen = pygame.display.set_mode((board_len, board_len))
for i in range(num_squares):
for j in range(num_squares):
if (i + j) % 2 == 0:
color = (255, 255, 255) # white
else:
color = (0, 0, 0) # black
pygame.draw.rect(screen, color, [s*j, s*i, s, s])
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
$ python chess.py
Comments
Post a Comment