Tags » PyGame

Changing window color using arrow keys Pygame Python

This is a code snippet which you can insert in your code and it would work very effectively.


from pygame.locals import *
import pygame
import sys

pygame.init()

x = 0
COLOR = (x, x, x)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

window = pygame.display.set_mode((400, 460))
window.fill(COLOR)

FONT  = pygame.font.Font('freesansbold.ttf', 20)
INS1  = FONT.render("Left arrow = more white", True, WHITE, BLACK)
INS2  = FONT.render("Right arrow = more black", True, WHITE, BLACK)

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

		if event.type == pygame.KEYDOWN:
			if event.key == K_LEFT:
				x_var += 15 # Using 15 because it is a divisor of 255
			if event.key == K_RIGHT:
				x_var -= 15

	x += x_var

	if x > 255:
		x = 255
	elif x < 0:
		x = 0

	window.fill((x, x, x))
	window.blit(INS1, (0, 400))
	window.blit(INS2, (0, 440))
	pygame.display.update()

Playing Sounds in Pygame Python

This is a small code block that plays two different sounds when the start and the end buttons are clicked.

from pygame.locals import *
import pygame
import sys

pygame.init()
pygame.mixer.init()

# Color
WHITE = (255, 255, 255)
GREEN = (  0, 255,   0)
RED   = (255,   0,   0)

# Sounds
beep1 = pygame.mixer.Sound("(your sound name).wav") # Give your sound name
beep2 = pygame.mixer.Sound("(your sound name).wav") # Give your sound name

# Mouse X & Y
mx = 0
my = 0

window = pygame.display.set_mode((200, 100))

FONT  = pygame.font.Font('freesansbold.ttf', 20)
start = FONT.render("START", True, WHITE, GREEN)
end   = FONT.render(  "END", True, WHITE,   RED)

pygame.draw.rect(window, GREEN, [  0, 0, 100, 100])
pygame.draw.rect(window,   RED, )

window.blit(start, ( 20, 20))
window.blit(end  , (120, 20))

while True:
	mx, my = pygame.mouse.get_pos() # To get the mouse position
	for event in pygame.event.get():
		if event.type == QUIT:
			pygame.quit()
			sys.exit()

	if mx > 0 and mx  100 and mx <= 200 and event.type == pygame.MOUSEBUTTONDOWN:
beep1.stop()
beep2.play()

	pygame.display.update()

Memory Puzzle in Pygame Python

This is a memory puzzle in pygame and python.

Python File

# Memory Puzzle
# By Al Sweigart al@inventwithpython.com
# http://inventwithpython.com/pygame
# Released under a "Simplified BSD" license

import random, pygame, sys
from pygame.locals import *

FPS = 30 # frames per second, the general speed of the program
WINDOWWIDTH = 640 # size of window's width in pixels
WINDOWHEIGHT = 480 # size of windows' height in pixels
REVEALSPEED = 8 # speed boxes' sliding reveals and covers
BOXSIZE = 40 # size of box height & width in pixels
GAPSIZE = 10 # size of gap between boxes in pixels
BOARDWIDTH = 10 # number of columns of icons
BOARDHEIGHT = 7 # number of rows of icons
assert (BOARDWIDTH * BOARDHEIGHT) % 2 == 0, 'Board needs to have an even number of boxes for pairs of matches.'
XMARGIN = int((WINDOWWIDTH - (BOARDWIDTH * (BOXSIZE + GAPSIZE))) / 2)
YMARGIN = int((WINDOWHEIGHT - (BOARDHEIGHT * (BOXSIZE + GAPSIZE))) / 2)

#            R    G    B
GRAY     = (100, 100, 100)
NAVYBLUE = ( 60,  60, 100)
WHITE    = (255, 255, 255)
RED      = (255,   0,   0)
GREEN    = (  0, 255,   0)
BLUE     = (  0,   0, 255)
YELLOW   = (255, 255,   0)
ORANGE   = (255, 128,   0)
PURPLE   = (255,   0, 255)
CYAN     = (  0, 255, 255)

BGCOLOR = NAVYBLUE
LIGHTBGCOLOR = GRAY
BOXCOLOR = WHITE
HIGHLIGHTCOLOR = BLUE

DONUT = 'donut'
SQUARE = 'square'
DIAMOND = 'diamond'
LINES = 'lines'
OVAL = 'oval'

ALLCOLORS = (RED, GREEN, BLUE, YELLOW, ORANGE, PURPLE, CYAN)
ALLSHAPES = (DONUT, SQUARE, DIAMOND, LINES, OVAL)
assert len(ALLCOLORS) * len(ALLSHAPES) * 2 >= BOARDWIDTH * BOARDHEIGHT, "Board is too big for the number of shapes/colors defined."

def main():
    global FPSCLOCK, DISPLAYSURF
    pygame.init()
    FPSCLOCK = pygame.time.Clock()
    DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))

    mousex = 0 # used to store x coordinate of mouse event
    mousey = 0 # used to store y coordinate of mouse event
    pygame.display.set_caption('Memory Game')

    mainBoard = getRandomizedBoard()
    revealedBoxes = generateRevealedBoxesData(False)

    firstSelection = None # stores the (x, y) of the first box clicked. 983 more words

Sprite Animation in Pygame Python 3

This is a simple program which makes you understand the basic concepts of the pygame library and how to use it to make animations.

Note: You can download all the images used in this program… 130 more words

Sprite Animation in Pygame Python 2.7

This is a simple program which makes you understand the basic concepts of the pygame library and how to use it to make animations.

Note: You can download all the images used in this program… 111 more words

Images for Sprite Animation in Pygame Python

Both the images you need for the sprite animation program.

Note: You can find the sprite animation program here.