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:

https://eng.libretexts.org/Bookshelves/Computer_Science/Book%3A_Making_Games_with_Python_and_Pygame_(Sweigart)/03%3A_Pygame_Basics/3.02%3A_Source_Code_for_Hello_World_with_Pygame


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

Popular posts from this blog

Minikube Installation for M1 Mac

Selenide: Quick Start

PyCharm: Quick Start