Basic Pygame Graphics - python

I am just starting to learn pygame graphics. I drew a circle in pygame and was wondering how I program it to change colors.
For example: it changes colors from blue to red.
I need it to keep changing colors until I close pygame and it would be nice if the colors gradually changed from one to another instead of an instant change? Any ideas how I could do this?
import pygame
pygame.init()
RED = (255, 0, 0)
BLUE = ( 0, 0,255)
BLACK = ( 0, 0, 0)
SIZE = (1000,1000)
screen = pygame.display.set_mode(SIZE)
pygame.draw.circle(screen,RED,(500,500),200)
pygame.display.flip()
pygame.time.wait(3000)
pygame.quit()

I shall progress from simple to harder, and more complex..
Simplest: A for loop that changes the color 3 times, simplest:
import pygame
pygame.init()
RED = (255,0,0)
BLUE = (0,0,255)
BLACK = (0,0,0)
SIZE = (1000,1000)
screen = pygame.display.set_mode(SIZE)
colors = (RED, BLACK, BLUE) # tho allow you to iterate over the colors
for c in colors:
pygame.draw.circle(screen,c,(500,500),200)
pygame.display.flip()
pygame.time.wait(1000)
pygame.quit()
Medium: Now an infinite loop, that ends when you close the window..
import pygame, itertools
RED = (255,0,0)
BLUE = (0,0,255)
BLACK = (0,0,0)
colors = (RED, BLACK, BLUE) # to allow you to iterate over the colors
SIZE = (1000,1000)
screen = pygame.display.set_mode(SIZE)
# to cycle through the colors
cycle = itertools.cycle(colors) # create an infinite series..
clock = pygame.time.Clock() # regulate fps
while True:
# handling events
for event in pygame.event.get():
if event.type == pygame.QUIT: # close window event
pygame.quit()
c = cycle.next()
pygame.draw.circle(screen,c,(500,500),200)
pygame.display.flip()
clock.tick(6) # run at maximum 6 frames per second
Hardest and most complex: This is the final one, the colors fade into the next one..
import pygame, itertools
def fade_into(c1, c2, n):
""" Give the next color to draw \n"""
"Args: c1,c2 => colors, n => int"
dif = [(c1[i]-c2[i])/float(n) for i in range(3)] # calculate the per-frame difference
return [c1[i]-dif[i] for i in range(3)] # subtract that difference
RED = (255,0,0)
BLUE = (0,0,255)
BLACK = (0,0,0)
FADE_SPEED = 80 # no of frames for shifting
colors = (RED, BLACK, BLUE) # to allow you to iterate over the colors
SIZE = (1000,1000)
screen = pygame.display.set_mode(SIZE)
# to cycle through the colors
cycle = itertools.cycle(colors)
## needed for fading
c_color = cycle.next() # RED current_color
n_color = cycle.next() # BLACK next_color
frames = FADE_SPEED
## --------------
clock = pygame.time.Clock() # regulate fps
while True:
# handling events
for event in pygame.event.get():
if event.type == pygame.QUIT: # close window event
pygame.quit()
c_color = fade_into(c_color, n_color, frames) # get next color
pygame.draw.circle(screen,map(int,c_color),(500,500),200)
pygame.display.flip()
frames -= 1
if frames == 0: # translation complete
frames = FADE_SPEED
n_color = cycle.next() # get next color
clock.tick(40) # run at maximum of 40 frames per second
If you have any doubts, please comment below..

Pygame does not have scene graph, so you need to redraw your shape in a loop and call display.flip() to update.

Related

How can I make my code more optimised by not drawing a rect on each pixel?

I am using the cv2.imread() function to make a a level editor in pygame that works with images, the only problem is that cv2.imread() gives me information on pixels thus I can only display one rect per pixel.This makes the code really slow and makes it impossible to work with it. I have tried storing all the values in a list but the problem is in the pygame rect function because it foes not have the ability to display all the pixels with collisions and all.
code:
import sys
import pygame
from pygame import *
import cv2
pygame.init()
screen = pygame.display.set_mode((388, 404))
lib = ['Map1.png']
Read = list(cv2.imread(lib[0]))
clock = pygame.time.Clock()
while True:
screen.fill((15, 15, 15))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
for i in range(len(Read)):#y coords
for j in range(len(Read[i])):#x coords
blue = list(Read[i][j])[0]
green = list(Read[i][j])[0]
red = list(Read[i][j])[0]
if blue > 240 and green > 240 and red > 240:
pygame.draw.rect(screen, (255,255,255), pygame.Rect(j, i, 32,32))
else:
pygame.draw.rect(screen, (0), pygame.Rect(j, i, 32, 32))
pygame.display.update()
clock.tick(120)
map1:
I recommend to use pygame.image.load and a pygame.mask.Mask:
Create a mask form the surface (pygame.mask.from_surface)
Invert the mask (invert())
Create a black and whit Surfcae from the mask (to_surface())
Scale up the black and white Surface (pygame.transform.scale)
import sys
import pygame
from pygame import *
pygame.init()
screen = pygame.display.set_mode((388, 404))
read_surf = pygame.image.load("Map1.png")
w, h = read_surf.get_size()
mask = pygame.mask.from_surface(read_surf)
# mask.invert() # optional
mask_surf = pygame.transform.scale(mask.to_surface(), (w*32, h*32))
clock = pygame.time.Clock()
while True:
screen.fill((15, 15, 15))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.blit(mask_surf, (0, 0))
pygame.display.update()
clock.tick(120)

Pygame drawing not showing in Pygame window

So I was testing out pygame and I wanted to draw a simple rectangle. There are no error messages when I run the code but the rectangle doesn't show up in the window. What I see is a blank white Pygame window pop up. Does anyone know why?
Currently using Python3 and Pygame 1.9.4 on my mac.
Here is my code,
import pygame
import pygame.font
pygame.init()
# Colours
BLACK = ( 0, 0, 0)
WHITE = (255,255,255)
GREEN = ( 0,255, 0)
RED = (255, 0, 0)
BLUE = ( 0, 0,255)
# Dimensions of screen
size = (400,500)
WIDTH = 500
HEIGHT = 400
screen = pygame.display.set_mode(size)
# Loop Switch
done = False
# Screen Update Speed (FPS)
clock = pygame.time.Clock()
# ------- Main Program Loop -------
while not done:
# --- Main Event Loop ---
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
pygame.draw.rect(screen,(78,203,245),(0,0,250,500),5)
screen.fill(GREEN)
pygame.display.flip()
#Setting FPS
clock.tick(60)
#Shutdown
pygame.quit()
You do not want to fill the screen with green every 60 ticks
To fix this, simply put screen.fill(GREEN) outside of the Main loop.
The only time you want screen.fill inside your while loop, is when your adding movement into your program.
I strongly suggest you make a function called draw and draw things outside of your while loop.
I have found the problem:
first of, Glitchd is correct, but you forget to update:
import pygame
import pygame.font
pygame.init()
# Colours
BLACK = ( 0, 0, 0)
WHITE = (255,255,255)
GREEN = ( 0,255, 0)
RED = (255, 0, 0)
BLUE = ( 0, 0,255)
# Dimensions of screen
size = (400,500)
WIDTH = 500
HEIGHT = 400
screen = pygame.display.set_mode(size)
# Loop Switch
done = False
# Screen Update Speed (FPS)
clock = pygame.time.Clock()
# ------- Main Program Loop -------
while not done:
# --- Main Event Loop ---
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
screen.fill(GREEN)
pygame.draw.rect(screen,(78,203,245),(0,0,250,500),5)
pygame.display.flip()
pygame.display.update()
#Setting FPS
clock.tick(60)
#Shutdown
pygame.quit()
U are drawing a shape and then covering it up with green, swap
pygame.draw.rect(screen,(78,203,245),(0,0,250,500),5)
screen.fill(GREEN)
Those 2 around
The problem is that you draw the shape and after that you 'fill' (cover) it with green, so try something like that:
screen.fill(GREEN) #first fill the screen with green
pygame.draw.rect(screen,(78,203,245),(0,0,250,500),5) #and after that draw the rectangle
The error is obvious as first you are drawing a shape then covering it with color . Your code is right but need some rearrangement.
screen.fill("your color") # First you should fill the screen with color
pygame.draw.rect(screen,(78,203,245),(0,0,250,500),5) # Then u should draw any shape
Each of the previously given answers fail to properly elaborate why this issue occurs. It is not about the order of drawing, filling operations; it is about your timing on calling the pygame.display.flip function, or shortly: updating the screen. Your code draws a rectangle, fills the screen with green, and then updates the screen. What it should have done instead is draw the rectangle, update the screen and then fill the screen with green. That way the screen is updated after you draw the rectangle before the screen is filled with green, therefore you can see it:
import pygame
import pygame.font
pygame.init()
# Colours
BLACK = ( 0, 0, 0)
WHITE = (255,255,255)
GREEN = ( 0,255, 0)
RED = (255, 0, 0)
BLUE = ( 0, 0,255)
# Dimensions of screen
size = (400,500)
WIDTH = 500
HEIGHT = 400
screen = pygame.display.set_mode(size)
# Loop Switch
done = False
# Screen Update Speed (FPS)
clock = pygame.time.Clock()
# ------- Main Program Loop -------
while not done:
# --- Main Event Loop ---
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
#In each case you draw the rectangle and then fill the screen with green
pygame.draw.rect(screen,(78,203,245),(0,0,250,500),5)
pygame.display.flip()
screen.fill(GREEN)
#Setting FPS
clock.tick(60)
#Shutdown
pygame.quit()
In a nutshell, you should update after you draw the rectangle.
you should add pygame.display.update() in the while not done loop. pygame.display.update updates the screen. You have this problem because you drew all of the drawings but did not update the screen.
You should first cover the screen with green and then draw your shape because otherwise it will get covered.

Pygame resizing a surface and window based on a rect size

I have a display area and a surface that is blitted on the display. On the surface is an image, in this case a rect. In the future it may be multiple rects or lines drawn on the surface keep that in mind.
I am trying to enlarge (by pressing x) the Rect named Sprite that is on SpriteSurface and SpriteSurface as well as the whole display window. The SpriteSurface image should be centered despite the resize. Currently the window will enlarge and the image stays centered, but if you uncomment the spritesizeX and Y lines the image gets larger but too big too fast and the window doesn't seem to enlarge big enough. Lowering the values shows that the offset of centering gets thrown off after the first resize. I feel like the solution should be relatively easy but im stumped. Any help would be appreciated.
Settings.py
spriteSizeX = 30
spriteSizeY = 30
SpHalfX = int(round(spriteSizeX / 2))
SpHalfY = int(round(spriteSizeY / 2))
multiplyer = 3
windowSizeX = int(round(spriteSizeX * multiplyer))
windowSizeY = int(round(spriteSizeY * multiplyer))
HalfWinX = int(round((windowSizeX / 2) - SpHalfX))
HalfWinY = int(round((windowSizeY / 2) - SpHalfY))
Orange = (238,154,0)
Gold = (255,215,0)
Black = (0,0,0)
Blue = (0,0,255)
Gray = (128,128,128)
DarkGray = (100,100,100)
Green = (0,128,0)
Lime = (0,255,0)
Purple = (128,0,128)
Red = (255,0,0)
Teal = (0,200, 128)
Yellow = (255,255,0)
White = (255,255,255)
run = True
SpriteCapture.py
#!/usr/local/bin/python3.6
import sys, pygame
from pygame.locals import *
from settings import *
pygame.init()
pygame.display.set_caption("Sprite Capture")
Screen = pygame.display.set_mode((windowSizeX, windowSizeY),RESIZABLE)
SpriteSurface = pygame.Surface((spriteSizeX,spriteSizeY))
Sprite = Rect(0,0,spriteSizeX,spriteSizeY)
while run == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if pygame.key.get_pressed()[pygame.K_s]:
pygame.image.save(SpriteSurface, 'img1.png')
run = False
if pygame.key.get_pressed()[pygame.K_q]:
run = False
if pygame.key.get_pressed()[pygame.K_z]:
#spriteSizeX += 10
#spriteSizeY += 10
windowSizeX += -10
windowSizeY += -10
HalfWinX = int(round(windowSizeX / 2 - SpHalfX))
HalfWinY = int(round(windowSizeY / 2 - SpHalfY))
Screen = pygame.display.set_mode((windowSizeX, windowSizeY),RESIZABLE)
SpriteSurface = pygame.Surface((spriteSizeX,spriteSizeY))
if pygame.key.get_pressed()[pygame.K_x]:
#spriteSizeX += 10
#spriteSizeY += 10
windowSizeX += 10
windowSizeY += 10
HalfWinX = int(round(windowSizeX / 2 - SpHalfX))
HalfWinY = int(round(windowSizeY / 2 - SpHalfY))
Screen = pygame.display.set_mode((windowSizeX, windowSizeY),RESIZABLE)
SpriteSurface = pygame.Surface((spriteSizeX,spriteSizeY))
Sprite = Sprite = Rect(0,0,spriteSizeX,spriteSizeY)
Screen.fill(Black)
pygame.draw.rect(SpriteSurface,Orange,Sprite)
Screen.blit(SpriteSurface, (HalfWinX,HalfWinY))
pygame.display.flip()
If you want to scale your surfaces or rects according to the screen size, you can define a zoom_factor variable which you can just increase when a key gets pressed and then use it to scale the window and the surfaces. Multiply it by the original screen width and height to scale the window, and also scale your surfaces with pygame.transform.rotozoom and pass the zoom_factor as the scale argument.
import sys
import pygame
from pygame.locals import *
width = 30
height = 30
multiplyer = 3
window_width = round(width * multiplyer)
window_height = round(height * multiplyer)
zoom_factor = 1
ORANGE = (238,154,0)
BLACK = (0,0,0)
pygame.init()
screen = pygame.display.set_mode((window_width, window_height), RESIZABLE)
screen_rect = screen.get_rect() # A rect with the size of the screen.
clock = pygame.time.Clock()
# Keep a reference to the original image to preserve the quality.
ORIG_SURFACE = pygame.Surface((width, height))
ORIG_SURFACE.fill(ORANGE)
surface = ORIG_SURFACE
# Center the rect on the screen's center.
rect = surface.get_rect(center=screen_rect.center)
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
run = False
elif event.key == pygame.K_z:
zoom_factor = round(zoom_factor-.1, 1)
# Scale the screen.
w, h = int(window_width*zoom_factor), int(window_height*zoom_factor)
screen = pygame.display.set_mode((w, h), RESIZABLE)
screen_rect = screen.get_rect() # Get a new rect.
# Scale the ORIG_SURFACE (the original won't be modified).
surface = pygame.transform.rotozoom(ORIG_SURFACE, 0, zoom_factor)
rect = surface.get_rect(center=screen_rect.center) # Get a new rect.
elif event.key == pygame.K_x:
zoom_factor = round(zoom_factor+.1, 1)
w, h = int(window_width*zoom_factor), int(window_height*zoom_factor)
screen = pygame.display.set_mode((w, h), RESIZABLE)
screen_rect = screen.get_rect()
surface = pygame.transform.rotozoom(ORIG_SURFACE, 0, zoom_factor)
rect = surface.get_rect(center=screen_rect.center)
# Note that the rect.w/screen_rect.w ratio is not perfectly constant.
print(zoom_factor, screen_rect.w, rect.w, rect.w/screen_rect.w)
screen.fill(BLACK)
screen.blit(surface, rect) # Blit the surface at the rect.topleft coords.
pygame.display.flip()
clock.tick(60)
Alternatively, you could just blit all of your surfaces onto a background surface, then scale this background with pygame.transform.rotozoom each frame and blit it onto the screen. However, scaling a big background surface each frame will be bad for the performance.

Drawing Two rects around two Surfaces in Pygame

I am trying two draw two surfaces, one on top of the other, each with a border around it. Both the top surface (topSurf) and the bottom surface (botSurf) appear on the screen.
When I attempt to draw a border around each surface with the pygame.draw.rect command, the rect on the top surface appears, but the rect on the bottom surface does not.
Why does this happen?
Here is the code:
import sys
import pygame
from pygame.locals import *
pygame.init()
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
WHITE = (255,255,255)
screen = pygame.display.set_mode((600,600))
#top surface
topRect = pygame.Rect(0,0,600,500)
topSurf = pygame.Surface((600,500))
topSurf = topSurf.convert()
topSurf.fill(BLUE)
#bottom surface
botSurf = pygame.Surface((600,100))
botRect = pygame.Rect(0,500,600,100)
botSurf = botSurf.convert()
botSurf.fill(GREEN)
#rectangle
pygame.draw.rect(topSurf,RED,topRect,10)
pygame.draw.rect(botSurf,WHITE,botRect,10)
### main game loop
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
screen.blit(topSurf,topRect)
screen.blit(botSurf,botRect)
pygame.display.flip()
Screenshot
You're using the botRect's coordinates to draw the white rect and that means you're drawing it at the surfaces local coordinates (0, 500), outside of the area of the botSurf. You have to draw the rect at (0, 0) first and then move the rect afterwards if you also want to use it as the blit position of the surface.
botSurf = pygame.Surface((600,100))
botSurf.fill(GREEN)
botRect = pygame.Rect(0,0,600,100)
pygame.draw.rect(botSurf,WHITE,botRect,10)
botRect.y = 500

How do I display text in a grid-like structure in Pygame?

I'm new to pygame and currently I'm working on creating a memory game where the computer displays boxes at random positions for like a second and then the user has to click on where he/she thinks those boxes are. It's kind of like this game:
However I'm not really sure how to make the computer display the boxes with like a letter or symbol e.g. 'T' or '%'. (I've already made the grid).
Could anyone please help? It would be really appreciated.
import pygame
size=[500,500]
pygame.init()
screen=pygame.display.set_mode(size)
# Colours
LIME = (0,255,0)
RED = (255, 0, 0)
BLACK = (0,0,0)
PINK = (255,102,178)
SALMON = (255,192,203)
WHITE = (255,255,255)
LIGHT_PINK = (255, 181, 197)
SKY_BLUE = (176, 226, 255)
screen.fill(BLACK)
# Width and Height of game box
width=50
height=50
# Margin between each cell
margin = 5
# Create a 2 dimensional array. A two dimesional
# array is simply a list of lists.
grid=[]
for row in range(20):
# Add an empty array that will hold each cell
# in this row
grid.append([])
for column in range(20):
grid[row].append(0) # Append a cell
# Set row 1, cell 5 to one. (Remember rows and
# column numbers start at zero.)
grid[1][5] = 1
# Set title of screen
pygame.display.set_caption("Spatial Recall")
#Loop until the user clicks the close button.
done=False
# Used to manage how fast the screen updates
clock=pygame.time.Clock()
# -------- Main Program Loop -----------
while done==False:
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done=True # Flag that we are done so we exit this loop
if event.type == pygame.MOUSEBUTTONDOWN:
# User clicks the mouse. Get the position
pos = pygame.mouse.get_pos()
# Change the x/y screen coordinates to grid coordinates
column=pos[0] // (width+margin)
row=pos[1] // (height+margin)
# Sete t hat location to zero
grid[row][column]=1
print("Click ",pos,"Grid coordinates: ",row,column)
# Draw the grid
for row in range(10):
for column in range(10):
color = LIGHT_PINK
if grid[row][column] == 1:
color = RED
pygame.draw.rect(screen,color,[(margin+width)*column+margin,(margin+height)*row+margin,width,height])
# Limit to 20 frames per second
clock.tick(20)
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
pygame.quit ()
In order to display text, you have to go through a series of steps. First you will want to get a font by using the command `pygame.font.Font(font name, size). For example:
arialfont=pygame.font.Font('arial', 12)
All available fonts can be gotten from the command pygame.font.get_fonts(). Remember to initialize pygame (pygame.init()) before any of this.
Next, you will have to use the Font.render(text, antialias, color, background=None). For example:
text=arialfont.render('Hello World!', True, (0, 0, 0))
This will return a surface. You can use it just like you would any other surface. Use text.get_rect() to get its rect, then reposition the rect to put it where you want it to be, and blit it to the window. If you don't know anything about surface objects, just ask me.
Here is a working code.
import pygame, sys
pygame.init()#never forget this line
window=pygame.display.set_mode((100, 100))
font=pygame.font.SysFont('arial', 40)
text=font.render('#', True, (0, 0, 0))
rect=text.get_rect()
window.fill((255, 255, 255))
window.blit(text, rect)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()

Categories

Resources