how would I go about making the rects be evenly placed throughout the grid while still being scalable. I want to cover the whole window with them and increase how many there are as you scale them down, my intention is to make it look like its zooming out.
import pygame
import sys
width, height = 750, 750
window = pygame.display.set_mode((width, height))
scale = 1
scroll_scale = .05
while True:
graph_size = 50 * scale
graph_count = width / graph_size
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 4:
scale += scroll_scale
elif event.button == 5 and scale > 0:
scale -= scroll_scale
window.fill((255,255,255))
pygame.draw.rect(window, (0,255,0),(0, 0, graph_size, graph_size))
pygame.draw.rect(window, (0,255,0),(0 + graph_size, 0, graph_size, graph_size))
pygame.draw.rect(window, (0,255,0),(0 + graph_size * 2, 0, graph_size, graph_size))
pygame.display.update()
I know there must be a way to do this without drawing all of the rects separately but I just cant seem to find it
Use 2 nested loops and the built-in range function:
import pygame
import sys
width, height = 750, 750
window = pygame.display.set_mode((width, height))
scale = 1
scroll_scale = .05
while True:
graph_size = 50 * scale
graph_count = width / graph_size
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 4:
scale += scroll_scale
elif event.button == 5 and scale > 0:
scale -= scroll_scale
window.fill((255,255,255))
size = round(graph_size)
for x in range(0, width + size, size):
for y in range(0, height + size, size):
c = (0,255,0) if (((x + y) // size) % 2) == 0 else (127, 127, 127)
pygame.draw.rect(window, c, (x, y, size, size))
pygame.display.update()
Related
I have this code
import pygame, random, sys
from pygame.locals import *
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0 , 0, 255)
WIDTH = 20
HEIGHT = 20
WIDTH1 = 30
HEIGHT1 = 30
MARGIN = 5
MARGIN1 = 10
array_width = 4
array_height = 8
grid = []
guess1 = 'white'
guess2 = 'yellow'
guess3 = 'blue'
guess4 = 'green'
guess5 = 'red'
guess6 = 'pink'
guess7 = 'purple'
guess8 = 'orange'
loop = ()
computerguess = []
# CREATING RANDOM COLOUR SEQUENCE TO GUESS
for i in range(4):
loop = random.randint(1,8)
if loop == 1:
computerguess.append(guess1)
if loop == 2:
computerguess.append(guess2)
if loop == 3:
computerguess.append(guess3)
if loop == 4:
computerguess.append(guess4)
if loop == 5:
computerguess.append(guess5)
if loop == 6:
computerguess.append(guess6)
if loop == 7:
computerguess.append(guess7)
if loop == 8:
computerguess.append(guess8)
print(computerguess)
for row in range(10):
grid.append([])
for column in range(10):
grid[row].append(0)
colors = [(0, 0, 0) for i in range(array_width * array_height)]
blocks = [False for i in range(array_width * array_height)]
indicator_grid = [[None for column in range(4)] for row in range(8)]
pygame.init()
# Set the HEIGHT and WIDTH of the screen
WINDOW_SIZE = [300, 300]
WINDOW_SIZE2 = [300, 300]
screen = pygame.display.set_mode(WINDOW_SIZE)
screen2 = pygame.display.set_mode(WINDOW_SIZE2)
# Set title of screen
pygame.display.set_caption("MASTERMIND GAME")
done = False
clock = pygame.time.Clock()
# -------- Main Program Loop -----------
#TEXT BOX VARIABLES AND DATA
base_font = pygame.font.Font(None, 20)
user_text = ""
input_rect = pygame.Rect (10,250,100,50)
color_active = pygame.Color('lightskyblue3')
color_passive = pygame.Color('gray15')
color=color_passive
active = False
current_color = "white"
grid = [[current_color for column in range(4)] for row in range(8)]
#----MAIN PROGRAM LOOP----#
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
column = pos[0] // (WIDTH + MARGIN)
row = pos[1] // (HEIGHT + MARGIN)
# check if column and row are valid grid indices
if 0 <= row < array_height and 0 <= column < array_width:
try:
grid[row][column] = current_color
except:
print("invalid color")
# TEXT BOX CREATION AND USAGE
if event.type == pygame.MOUSEBUTTONDOWN:
if input_rect.collidepoint(event.pos):
active = True
else:
active = False
if event.type == pygame.KEYDOWN:
if active == True:
if event.key == pygame.K_BACKSPACE:
user_text = user_text[0:-1]
else:
user_text += event.unicode
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
user_text = user_text
if user_text != "":
current_color = user_text
user_text = ""
if active:
color = color_active
else:
color=color_passive
#TEXT FOR THE GUI TO MAKE IT EASIER FOR THE USER
screen.fill(0)
pygame.draw.rect(screen,color,input_rect,2)
text_surface= base_font.render(user_text,True,(255,255,255))
screen.blit(text_surface, (input_rect.x +5, input_rect.y + 5))
text1 = base_font.render("WORK BOTTOM TO TOP ", True, (0, 50, 255))
text2 = base_font.render('POSSIBLE CHOICES:', True, (250, 0, 0))
text3 = base_font.render('WHITE BLUE RED', True, (0, 128, 0))
text4 = base_font.render('GREEN ORANGE PINK', True, (0,128,0))
text5= base_font.render('PURPLE YELLOW', True, (0, 128, 0))
screen.blit(text1,(140, 200))
screen.blit(text2,(140, 220))
screen.blit(text3,(140, 240))
screen.blit(text4,(140, 260))
screen.blit(text5,(140, 280))
screen.blit(text5,(140, 280))
for row, gridrow in enumerate(grid):
for column, color in enumerate(gridrow):
pygame.draw.rect(screen,
color,
[(MARGIN + WIDTH) * column + MARGIN,
(MARGIN + HEIGHT) * row + MARGIN,
WIDTH,
HEIGHT])
if gridrow == computerguess:
text = base_font.render("Correct, you win!!!!!", True, (255, 128, 0))
screen.blit(text,
(10, 220 ))
x = 100 + (MARGIN + WIDTH) * column + MARGIN
y = (MARGIN + HEIGHT) * row + MARGIN
color = "grey"
pygame.draw.circle(screen, color, (x + WIDTH // 2, y + WIDTH // 2), WIDTH // 2)
for row, gridrow in enumerate(grid):
if computerguess[0] == gridrow[0]:
color = "red"
pygame.draw.circle(screen, color, (x + WIDTH // 2, y + WIDTH // 2), WIDTH // 2)
if computerguess[1] == gridrow[1]:
color = "purple"
pygame.draw.circle(screen, color, (x + WIDTH // 2, y + WIDTH // 2), WIDTH // 2)
if computerguess[2] == gridrow[2]:
color = "red"
pygame.draw.circle(screen, color, (x + WIDTH // 2, y + WIDTH // 2), WIDTH // 2)
if computerguess[3] == gridrow[3]:
color = "red"
pygame.draw.circle(screen, color, (x + WIDTH // 2, y + WIDTH // 2), WIDTH // 2)
clock.tick(60)
pygame.display.flip()
pygame.quit()
i would like to add a title screen, that says start in the form of a button and then would transfer them to the code to begin playing, but i'm not very sure how to do it, i have added a new window (WINDOW_SIZE2), but i am not sure where to go from there
Any help is much appreciated
Thanks
The implementation of a button is answered several times. For example Pygame mouse clicking detection, How do I detect if the mouse is hovering over a button? PyGame button class is not displaying the text or changing colour on hover or How can I add an image or icon to a button rectangle in Pygame? and myn more.
Create 2 scenes with 2 application loops. The first loop shows the title screen and waits for button 2 to be pressed. The 2nd loop is the game loop:
button_rect = pygame.Rect(x, y, width, height) # start button rectangle
abort = False
start = False
while not abort and not start:
for event in pygame.event.get():
if event.type == pygame.QUIT:
abort = True
if event.type == MOUSEBUTTONDOWN:
if button_rect.collidepoint(event.pos):
start = True
# draw title screen
# [...]
done = abort
while not done:
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# game
# [...]
Alternatively, you can combine both scenes in one loop.Add a variable game_state, implement event handling and draw the scenes depending on the value of game_state. Change the game_state when the start button is pressed:
game_state = 'title'
done = False
while not done:
# event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if game_state == 'title':
if event.type == MOUSEBUTTONDOWN:
if button_rect.collidepoint(event.pos):
game_state = 'game'
elif game_state == 'game':
# handle game events:
# [...]
# drawing
if game_state == 'title':
# draw title screen
# [...]
elif game_state == 'game':
# game
# [...]
I am trying to make an Algorithm Visualizer with pygame and I started by implementing Linear Search. The problem I am facing is that, in the visualization I am changing colour of the rectangle but that animation is repeating itself as it is inside the while loop. Here is the code:
I want that the animation should stop so that the viewer can actually see what happened. Further I am planning to add more algorithms.
ARRAY = [2, 10, 5, 8, 7, 3, 43, 54, 23, 1]
def main():
global SCREEN, CLOCK
pygame.init()
SCREEN = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
CLOCK = pygame.time.Clock()
num_font = pygame.font.SysFont("roboto", NUM_FONT_SIZE)
x = 3
y = 10
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE:
pygame.quit()
showArray(x, y, num_font)
linearSearch(x, y)
pygame.display.update()
CLOCK.tick(60)
def linearSearch(x, y):
num = 23
box = pygame.Rect(x, y, BOX_SIZE+5, BOX_SIZE)
for i in ARRAY:
if i == num:
pygame.draw.rect(SCREEN, RED, box, 1)
pygame.draw.rect(SCREEN, GREEN, box, 1)
else:
box.x += BOX_SIZE + 5
CLOCK.tick_busy_loop(10)
pygame.draw.rect(SCREEN, RED, box, 1)
pygame.display.update()
def showArray(x, y, num_font):
box = pygame.Rect(x, y, BOX_SIZE+5, BOX_SIZE)
for i in ARRAY:
box.x += BOX_SIZE + 5
pygame.draw.rect(SCREEN, WHITE, box, 1)
nums = num_font.render(str(i), True, WHITE)
SCREEN.blit(nums, (box.x + 5, box.y + 5))
Here is the image of the output
You have an application loop, so use it. Change the function linearSearch. The list index has to be an argument to the function, but remove the for loop has to be removed form the function:
def linearSearch(x, y, list_i):
i = ARRAY[list_i]
num = 23
box = pygame.Rect(x + (BOX_SIZE+5)*(list_i+1), y, (BOX_SIZE+5), BOX_SIZE)
color = GREEN if i == num else RED
pygame.draw.rect(SCREEN, color, box, 1)
Iterate through the list in the main application loop:
def main():
# [...]
list_i = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE:
pygame.quit()
showArray(x, y, num_font)
if list_i < len(ARRAY):
linearSearch(x, y, list_i)
list_i += 1
pygame.display.update()
CLOCK.tick(60)
I have tried to use .copy() and .move_ip() to duplicate the rectangle but all that does is move the original rectangle without creating two. I have also tried calling in pygame.rect.draw twice yet all it does is move the original image without leaving anything behind.
import pygame
pygame.init()
dis = pygame.display.set_mode((500, 500))
x = 0
y = 500-40
height = 40
width = 100
run = True
vel = 10
clock = pygame.time.Clock()
direction = 'Right'
ran = True
game = 0
def animate(ran, x, y, height, width, direction)#Pygasm:
if ran:
if x < vel:
direction = 'Right'
elif x > 500-width-vel:
direction = 'Left'
if direction == 'Right':
x += vel
elif direction == 'Left':
x -= vel
dssz = pygame.draw.rect(dis, (255, 0, 0), pygame.Rect(x, y, width, height))
return x, y, width, height, direction
while run:
for ev in pygame.event.get():
if ev.type == pygame.QUIT:
run = False
if ev.type == pygame.KEYDOWN:
if ev.key == pygame.K_UP:
game+=1
ran = False
if ev.key == pygame.K_DOWN:
game = 0
pygame.draw.rect(dis, (255,0,0), pygame.Rect(x,y,width,height))
y -= 40
ran = True
dis.fill((0, 0, 0))
keys = pygame.key.get_pressed()
x, y, height, width, direction = animate(ran, x, y, height, width, direction)
pygame.display.flip()
clock.tick(60)
pygame.quit()
Thats easy, use Rect.copy with a variable
myRect = pygame.Rect(0, 0, 20, 20)
myOtherRect = myRect.copy()
myOtherRect.x, myOtherRect.y = 20, 20
I've got two lists with 3 rects in each. Can I assign to variables selected1 and selected2 3 rects from each list and move these two rows of rects seperately up and down while keeping the gaps between them? I am beginner and don't know if such constructed code can handle this task.
import pygame
# === CONSTANS ===
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
blue = (0, 0, 255)
green = (0, 102, 0)
yellow = (255, 204, 0)
grid_color = (224, 224, 224)
width = 1200
height = 720
k = 10
screen = pygame.display.set_mode((width, height))
screen_rect = screen.get_rect()
# --- objects ---
G_x = 10 # stripe width
x = 2
stripes_x1 = []
stripes_x2 = []
G1_pos_x = 122
G2_pos_x = 367
G1_pos_y = 0
G2_pos_y = 0
G1_y = 60*x
G2_y = 75*x
G1_start = G1_pos_y + height - G1_y
G2_start = G2_pos_y + height - G2_y
a = 166
b = 222
for x in range(3):
gap = a*x
gap2 = b*x
stripes_x1.append(pygame.Rect(G1_pos_x, (G1_start - 6*k) - gap, G_x, G1_y))
stripes_x2.append(pygame.Rect(G2_pos_x, (G2_start - 6*k) - gap2, G_x, G2_y))
selected1 = None
selected2 = None
# --- mainloop ---
clock = pygame.time.Clock()
is_running = True
while is_running:
for event in pygame.event.get():
# --- global events ---
if event.type == pygame.QUIT:
is_running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
is_running = False
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
for i, r in enumerate(stripes_x1):
if r.collidepoint(event.pos):
selected1 = i
selected_offset_y = r.y - event.pos[1]
if event.type == pygame.MOUSEMOTION:
if selected1 is not None: # selected can be `0` so `is not None` is required
stripes_x1[selected1].y = event.pos[1] + selected_offset_y
if event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
selected1 = None
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
for j, r in enumerate(stripes_x2):
if r.collidepoint(event.pos):
selected2 = j
selected_offset_y = r.y - event.pos[1]
if event.type == pygame.MOUSEMOTION:
if selected2 is not None: # selected can be `0` so `is not None` is required
stripes_x2[selected2].y = event.pos[1] + selected_offset_y
if event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
selected2 = None
# --- objects events ---
'''
button.handle_event(event)
'''
# --- updates ---
# empty
# --- draws ---
screen.fill(white)
for i in range(width):
grid_x = k * i
grid_y = k * i
pygame.draw.line(screen, grid_color, (grid_x, 0), (grid_x, height), 1)
pygame.draw.line(screen, grid_color, (0, grid_y), (width, grid_y), 1)
pygame.draw.line(screen, black, (6 * k, height - 6 * k), (width - 6 * k, height - 6 * k), 3)
pygame.draw.line(screen, black, (6 * k, height - 6 * k), (6 * k, 0 * k), 3)
# draw rect
for r in stripes_x1:
pygame.draw.rect(screen, green, r)
for s in stripes_x2:
pygame.draw.rect(screen, green, s)
pygame.display.update()
clock.tick(60)
pygame.quit()
To move the rects simultaneously, you can put them in a list and, if one rect is selected, move them all with the help of the rel attribute of the MOUSEMOTION event (the relative position from the last mouse event). Here's a minimal example:
import sys
import pygame as pg
BLACK = pg.Color('black')
RECT_COLOR = pg.Color(78, 140, 200)
def main():
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
rect_list = [pg.Rect(100, 100+y, 20, 80) for y in range(0, 241, 120)]
selected = False
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
elif event.type == pg.MOUSEBUTTONDOWN:
for rect in rect_list:
if rect.collidepoint(event.pos):
selected = True
elif event.type == pg.MOUSEBUTTONUP:
selected = False
elif event.type == pg.MOUSEMOTION:
if selected:
for rect in rect_list:
rect.y += event.rel[1]
screen.fill(BLACK)
for rect in rect_list:
pg.draw.rect(screen, RECT_COLOR, rect)
pg.display.flip()
clock.tick(30)
if __name__ == '__main__':
pg.init()
main()
pg.quit()
sys.exit()
I've been trying to create a game. In the game there is a jet that would fly into the recursive background rectangles. The rectangles are created through an infinite loop so once it enters the loop rest of the functions don't work for example the object. My code is below and the problem arises in the main loop. I want the object to move with the recursive rectangles but it freezes when the rectangles start being drawn in the loop. PS help to fix this as I've tried almost every code sample out there to fix it. Thank you
EDIT: the main function of the game is to make the jet go through what seems like recursive like rectangles (there are two of them and the while loop makes it simultaneously move up and down giving the feeling that the jet is going into the screen.). But since the jet is drawn first, when the program enters the rectangle loop the jet freezes and wont be able to move. I want the jet to move while the background is also moving.
import pygame
import random
# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
Blue = (2,55,55)
black=(0,0,0)
end_it=False
def recursive_draw(x, y, width, height):
""" Recursive rectangle function. """
pygame.draw.rect(screen, WHITE,
[x, y, width, height],
1)
speed = [10,0]
rect_change_x = 10
rect_change_y = 10
# Is the rectangle wide enough to draw again?
if (width > 25):
# Scale down
x += width * .1
y += height * .1
width *= .8
height *= .8
# Recursively draw again
recursive_draw(x, y, width, height)
def recursive_draw2(x, y, width, height):
""" Recursive rectangle function. """
pygame.draw.rect(screen, Blue,
[x, y, width, height],
1)
speed = [10,0]
rect_change_x = 10
rect_change_y = 10
# Is the rectangle wide enough to draw again?
if (width > 25):
x += width * .1
y += height * .1
width *= .8
height *= .8
# Recursively draw again
recursive_draw2(x, y, width, height)
'''
def timer(self):
screen.fill(black)
myfont=pygame.font.SysFont("Britannic Bold", 40)
label2=myfont.render("Ready?", 1, (255, 0, 0))
screen.blit(label2, (350,250))
self =3
nlist=[]
for i in range (2):
score = myfont.render(str(self),1,(255,0,0))
screen.blit((score), (350,250))
self = self - 1
nlist.append(self)
pygame.display.flip()
'''
pygame.init()
#rectanglelist = [big()]
# Set the height and width of the screen
size = [700, 500]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("My Game")
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
USEREVENT = 0
pygame.time.set_timer(USEREVENT+1, 10)
milliseconds = 0
seconds = 0
start_it = False
while (end_it==False):
screen.fill(black)
myfont=pygame.font.SysFont("Britannic Bold", 40)
nlabel=myfont.render("Welcome to "+ " Jet shooter ", 1, (255, 0, 0))
label=myfont.render("Click on the mouse to start ", 1, (255, 0, 0))
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
end_it=True
screen.blit(nlabel,(200, 100))
screen.blit(label, (170,300))
pygame.display.flip()
while (start_it==False):
screen.fill(black)
myfont2=pygame.font.SysFont("Britannic Bold", 40)
label2=myfont2.render("Ready?", 1, (255, 0, 0))
screen.blit(label2, (300,250))
pygame.display.flip()
pygame.time.wait(3000)
start_it = True
fall = False
while (fall==False):
nlist = [3,2,1]
for i in (nlist):
screen.fill(black)
n = str(i)
myfont3=pygame.font.SysFont("Britannic Bold", 40)
score = myfont3.render(n,1,(255,0,0))
screen.blit((score), (350,250))
pygame.display.flip()
pygame.time.wait(1000)
screen.fill(black)
myfont4=pygame.font.SysFont("Britannic Bold", 40)
label4=myfont3.render("GOOO!!!", 1, (255, 0, 0))
screen.blit(label4, (300,250))
pygame.display.flip()
pygame.time.wait (1000)
fall = True
b = 0
flip = 1
a = 0
time = 100
x_speed = 0
y_speed = 0
x_coord = 320
y_coord = 400
image = pygame.image.load("spaceship.gif").convert()
# -------- Main Program Loop -----------
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# User pressed down on key
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_speed = -10
if event.key == pygame.K_RIGHT:
x_speed = 10
if event.key == pygame.K_UP:
y_speed = -10
if event.key == pygame.K_DOWN:
y_speed = 10
# User let go of key
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
x_speed = 0
if event.key == pygame.K_RIGHT:
x_speed = 0
if event.key == pygame.K_UP:
y_speed = 0
if event.key == pygame.K_DOWN:
y_speed = 0
x_coord += x_speed
y_coord += y_speed
# Set the screen background
screen.fill(BLACK)
screen.blit(image, [x_coord,y_coord])
pygame.display.flip()
clock.tick(60)
# ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT
# ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT
# Go ahead and update the screen with what we've drawn.
# Limit to 60 frames per second
# Be IDLE friendly. If you forget this line, the program will 'hang'
# on exit.
while a == 0 :
if flip == 1 :
recursive_draw(35,25,625,450)
recursive_draw2(0, 0, 700, 500)
flip = flip + 1
pygame.display.flip()
clock.tick(60)
if flip == 2 :
recursive_draw(0, 0, 700, 500)
recursive_draw2(35, 25, 625, 450)
flip = flip - 1
pygame.display.flip()
clock.tick(60)
pygame.quit()
As #Paul Rooney has stated, you'll want to use threads. Here's how you would
import thread
.
.
.
# Instead of calling the function as you are, run it on a new thread
thread.start_new_thread(drawRectanglesFunction, ())
From what I can tell is that you're drawing things in the incorrect order, and I believe that while loop is not needed. (while a == 0). Another thing is that you're flipping the display too often it is hard to keep track of what gets drawn first, and what gets drawn afterwards. My suggestion would be to quickly rewrite your program so that it looks something similar to this:
flip = 1
while not done:
##Catch and handle events()
screen.fill(BLACK)
if flip == 1 :
recursive_draw(35,25,625,450)
recursive_draw2(0, 0, 700, 500)
flip = flip + 1
elif flip == 2 :
recursive_draw(0, 0, 700, 500)
recursive_draw2(35, 25, 625, 450)
flip = flip - 1
screen.blit(image, [x_coord,y_coord])
pygame.display.flip()
clock.tick(60);
I hope this helps out a little.
(Edit: I did this on my own and got the program to run)