I'm making a constellation that starts from (0,0). The previous line has to disappear after a two second delay and when the left is clicked within the two second delay, white circles should appear. I don't know why my timer isn't working and I'm not sure how to make the line disappear. Also the circles don't appear. This is my code
from pygame import *
import random
init()
size = width, height = 700, 700
screen = display.set_mode(size)
button = 0
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0,0,255)
WHITE = (255,255,255)
colors = (RED,GREEN,BLUE)
time.set_timer(USEREVENT, 2000)
mx = 0
my = 0
def drawScene(screen, button):
if button == 1:
draw.circle(screen,RED,(mx,my), 5)
draw.line(screen,RED,(mx,my),(lx,ly),2)
draw.circle(screen,RED,(lx,ly), 5)
display.flip()
if button == 3:
draw.line(screen,random.choice(colors),(mx,my),(lx,ly),2)
draw.circle(screen,random.choice(colors),(lx,ly), 5)
display.flip()
running = True
myClock = time.Clock()
start = time.get_ticks()
# Game Loop
while running:
lx = mx
ly = my
for evnt in event.get(): # checks all events that happen
if evnt.type == QUIT:
running = False
if evnt.type == MOUSEBUTTONDOWN:
mx,my = evnt.pos
button = evnt.button
cx,cy = mouse.get_pos()
draw.circle(screen,WHITE,(cx,cy),5)
if evnt.type == USEREVENT:
my_event = event.Event(USEREVENT)
time.set_timer(my_event , 2000)
drawScene(screen, button)
myClock.tick(60) # waits long enough to have 60 fps
quit()
I hope this is what you were wanting and the comments are clear.
from pygame import *
import random
init()
size = width, height = 700, 700
screen = display.set_mode(size)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
WHITE = (255, 255, 255)
colors = (RED, GREEN, BLUE)
time.set_timer(USEREVENT, 2000)
mx = 0
my = 0
def drawScene(screen, button, prev_point, new_point):
if button == 1:
draw.circle(screen, RED, prev_point, 5)
draw.circle(screen, WHITE, new_point, 5)
draw.line (screen, RED, prev_point, new_point, 2)
display.flip()
if button == 3:
draw.line (screen, random.choice(colors), prev_point, new_point,2)
draw.circle(screen, random.choice(colors), new_point, 5)
display.flip()
running = True
myClock = time.Clock()
prev_render = time.get_ticks() #time in ticks that last render occured
prev_point = (mx, my) #where previous line ended
#function for when to re draw the scene
#ternary if. if override == True, then return true. Else return if time since last update > 2secs
rerender = lambda time_since_update, override: (time_since_update>2000, True)[override]
# Game Loop
while running:
override = False
for evnt in event.get(): # checks all events that happen
if evnt.type == QUIT:
running = False
if evnt.type == MOUSEBUTTONDOWN:
override = True #force window to rerender
new_point = evnt.pos #(mx, my)
button = evnt.button
#rerender window only if necessary. (personal preference)
updated_at = time.get_ticks()
dt = updated_at-prev_render #time difference
if(rerender(dt, override)):
screen.fill(BLACK)
drawScene(screen, button, prev_point, new_point)
prev_point = new_point
prev_render = updated_at
display.update()
myClock.tick(60) # waits long enough to have 60 fps
quit()
This is not a complete solution, but I'll give you a general idea how how to start
Create a list of times and a list of lines:
lines = []
Get the current time in the main application loop:
current_time = pygame.time.get_ticks()
When the mouse is clicked then compute the time when the line has to disappear (current_time + 2000) and compute a random color.
Add a Dictionary with the start point, the end point, the time when the line has to disappear and the color to the list of lines.
If the list of lines is empty, then the start point of the line is (0, 0) else the start point is the end point of the last line in the list:
if event.type == pygame.MOUSEBUTTONDOWN:
disappear_time = current_time + 2000
line_color = random.choice(colors)
prev_pt = (0, 0) if len(lines) == 0 else lines[-1]['end']
lines.append({'start': prev_pt, 'end': event.pos, 'time': disappear_time, 'color': line_color})
When the current time exceeds the time which is stored in time, then remove a point and a time form the lists of lines:
if len(times) > 0 and current_time > times[0]:
del lines[0]
Draw the lines by and circles in a loop:
screen.fill(BLACK)
for li in lines:
pygame.draw.line(screen, li['color'], li['start'], li['end'], 2)
pygame.draw.circle(screen, li['color'], li['start'], 5)
pygame.draw.circle(screen, li['color'], li['end'], 5)
See the example:
import pygame
import random
pygame.init()
size = width, height = 700, 700
screen = pygame.display.set_mode(size)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0,0,255)
WHITE = (255,255,255)
colors = (RED,GREEN,BLUE)
lines = []
running = True
myClock = pygame.time.Clock()
while running:
current_time = pygame.time.get_ticks()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
disappear_time = current_time + 2000
line_color = random.choice(colors)
prev_pt = (0, 0) if len(lines) == 0 else lines[-1]['end']
lines.append({'start': prev_pt, 'end': event.pos, 'time': disappear_time, 'color': line_color})
if len(lines) > 0 and current_time > lines[0]['time']:
del lines[0]
screen.fill(BLACK)
for li in lines:
pygame.draw.line(screen, li['color'], li['start'], li['end'], 2)
pygame.draw.circle(screen, li['color'], li['start'], 5)
pygame.draw.circle(screen, li['color'], li['end'], 5)
pygame.display.flip()
myClock.tick(60)
quit()
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 want to make some sort of typing game in python using pygame. So, if the key pressed character is the same as the character in the word, it should return true... Is there any way to do this in python?
For example:
the word is "cat", if the user presses the key 'c', then it returns true... and so on for the rest of the characters.
here's my main.py file
from time import sleep
import pygame
import random
import winsound
from words import Words
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
BLUE = ( 0, 0, 255)
GREEN = ( 0, 255, 0)
RED = (255, 0, 0)
pygame.init()
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
done = False
clock = pygame.time.Clock()
screen = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
w1 = Words(screen) #making a single word (for now) to see if typing works
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
screen.fill(WHITE)
w1.draw()
#attempting to write code here to compare word and user input
pygame.display.flip()
clock.tick(60)
pygame.init()
exit()
here's my words.py file
from random_words import RandomWords
import pygame
import random
from queue import *
rw = RandomWords()
class Words():
def __init__(self, screen):
self.screen = screen
self.x_point = 400
self.y_point = 400
self.word = rw.random_word() #generates a random word
self.queue = Queue() #was hoping to use the queue so that if the user types the char correctly in the right order, then the letter would change color or something (but that's further down the line)
for c in self.word: #iterate through randomized word..
self.queue.put(c) #add each char in randomized word to queue, for typing reasons
def getY(self):
return self.y_point
def draw(self):
#creates a new object
myfont = pygame.font.SysFont('Comic Sans MS' ,30)
#creates a new surface with text drawn on it
textsurface = myfont.render(self.word, False, (0,0,0))
self.screen.blit(textsurface,(self.x_point,self.y_point))
Event KEYDOWN has event.unicode, event.key, event.mod
You can compare
if event.type == pygame.KEYDOWN:
if event.unicode == "a":
or even
if event.type == pygame.KEYDOWN:
if event.unicode.lower() == "a":
to check "a" and "A"
To check char in word
if event.type == pygame.KEYDOWN:
if event.unicode.lower() in your_word.lower():
Example code use event.unicode to render text with pressed keys.
BTW: It is not some Entry widget so it doesn't delete char when you press backspace.
import pygame
# --- constants ---
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
BLUE = ( 0, 0, 255)
GREEN = ( 0, 255, 0)
RED = (255, 0, 0)
SCREEN_WIDTH = 300
SCREEN_HEIGHT = 200
FPS = 5 # `FPS = 25` is enough for human eye to see animation.
# If your program don't use animation
# then `FPS = 5` or even `FPS = 1` can be enough
# --- main ---
# - init -
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
screen_rect = screen.get_rect()
# - objects -
font = pygame.font.SysFont(None, 30)
text = ""
text_image = font.render(text, True, GREEN)
text_rect = text_image.get_rect() # get current size
text_rect.center = screen_rect.center # center on screen
# - mainloop -
clock = pygame.time.Clock()
done = False
while not done:
# - events -
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
text += event.unicode
text_image = font.render(text, True, GREEN)
text_rect = text_image.get_rect() # get current size
text_rect.center = screen_rect.center # center on screen
# - draws -
screen.fill(BLACK)
screen.blit(text_image, text_rect)
pygame.display.flip()
clock.tick(FPS)
# - end -
pygame.quit() # <-- quit(), not init()
My collision code is really faulty because the red 'bullets' have to hit the exact middle of the player to run the game over screen. I need help refactoring the expression so if the red 'bullets' hit the player anywhere it will run my game over code (The game over code is near the end before pygame.quit())
import pygame
import random
BLACK = (0,0,0)
WHITE = (255,255,255)
GREEN = (0,255,0)
RED = (255,0,0)
BLUE = (0,0,255)
pygame.init()
size = (700,700)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Dodger")
done = False
clock = pygame.time.Clock()
def resetpositions():
global bulletrect1, bulletrect2, bulletrect3, bulletrect4, bulletrect5, circlerect
bulletrect1 = pygame.rect.Rect((350, 0, 20, 20))
bulletrect2 = pygame.rect.Rect((175, 0, 20, 20))
bulletrect3 = pygame.rect.Rect((525, 0, 20, 20))
bulletrect4 = pygame.rect.Rect((525, 0, 20, 20))
bulletrect5 = pygame.rect.Rect((525, 0, 20, 20))
circlerect = pygame.rect.Rect((350, 600, 20, 20))
resetpositions()
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN:
circlerect.x += 5
bulletrect1.y += 1
bulletrect2.y += 2
bulletrect3.y += 3
bulletrect4.y += 4
bulletrect5.y += 5
screen.fill(BLACK)
pygame.draw.circle(screen, GREEN, (circlerect.center), 15)
pygame.draw.circle(screen, RED, (bulletrect1.center), 20)
pygame.draw.circle(screen, RED, (bulletrect2.center), 20)
pygame.draw.circle(screen, RED, (bulletrect3.center), 20)
pygame.draw.circle(screen, RED, (bulletrect4.center), 20)
pygame.draw.circle(screen, RED, (bulletrect5.center), 20)
if bulletrect1.y == 800:
bulletrect1.y = 0
bulletrect1.x = random.randint(20,680)
if bulletrect2.y == 800:
bulletrect2.y = 0
bulletrect2.x = random.randint(20,680)
if bulletrect3.y == 800:
bulletrect3.y = 0
bulletrect3.x = random.randint(20,680)
if bulletrect4.y == 800:
bulletrect4.y = 0
bulletrect4.x = random.randint(20,680)
if bulletrect5.y == 800:
bulletrect5.y = 0
bulletrect5.x = random.randint(20,680)
if circlerect.x == 685:
circlerect.x = 15
if circlerect.collidelist((bulletrect1, bulletrect2, bulletrect3, bulletrect4, bulletrect5)) == 0:
screen.fill(BLACK)
font = pygame.font.SysFont('Calibri',40,True,False)
text = font.render("GAME OVER",True,RED)
screen.blit(text,[250,350])
pygame.display.update()
pygame.time.delay(3000)
resetpositions()
pygame.display.flip()
clock.tick(300)
pygame.quit()
pygame.Rect.collidelist returns the index of the colliding rect in the list, that means you only check if the circlerect collides with the bulletrect at index 0. It returns -1 if no rects in the list collide, so if it doesn't return -1, one of the rects has collided with the player and the game is over:
if circlerect.collidelist((bulletrect1, bulletrect2, bulletrect3, bulletrect4, bulletrect5)) != -1:
# Game over.
The following line of code, I have borrowed as an example to use. What it does is it takes a string and prints it out in pygame, and the line will follow wherever the mouse goes. I simply was wondering how to change the string line for a draw.circle, I keep getting an error. Cheers! Code Below, the string in question is the one saying, "the last button pressed is".
from pygame import *
init()
size = width, height = 800, 600
screen = display.set_mode(size)
button = 0
BLACK = (0, 0, 0)
RED = (255, 255, 255)
font = font.SysFont("Times New Roman",30)
def drawScene(screen, mx, my, button):
draw.rect(screen, BLACK, (0, 0, width, height))
# Draw circle if the left mouse button is down.
string = "The last button pressed is " + str(button) + "."
text = font.render(string, 1, RED)
size = font.size(string)
screen.blit(text, Rect(mx, my, size[0], size[1]))
display.flip()
running = True
myClock = time.Clock()
mx = my = 0
# Game Loop
while running:
for evnt in event.get(): # checks all events that happen
if evnt.type == QUIT:
running = False
if evnt.type == MOUSEBUTTONDOWN:
mx, my = evnt.pos
button = evnt.button
if evnt.type == MOUSEMOTION:
mx, my = evnt.pos
drawScene(screen, mx, my, button)
myClock.tick(60) # waits long enough to have 60 fps
quit()
This will draw a 5 pixel radius circle at mx, my:
pygame.draw.circle(screen, BLACK, (mx, my), 5)
I am working on a reaction game using Pygame and have run into a few difficulties with the timer. Currently how this code functions, is that when you start the program, you have to hit "a" for the countdown timer to start. As the letter appears on the screen you hit the corresponding keyboard key, which updates the score. The two things I would like to change have to do with the timer. I would like for the timer to autostart without having to hit the "a" button and I would like for the score to not continue to tally once the time has expired.
You will see some of my previous attempts commented out. In all of the versions where I have tried to implement the auto timer, the timer rapidly ticks off, rather than waiting a second in between counting off.
import pygame
from pygame.locals import *
import os, sys
import random
import time
time_a = 20
a_on = False
n1 = time.time()
letter = ["c", "b"]
pygame.init()
pygame.display.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("Reaction Game")
background = pygame.Surface(screen.get_size())
rect = background.fill((0, 0, 0))
clock = pygame.time.Clock()
score = 0
font = pygame.font.SysFont("monospace", 36)
scoreupdate = font.render(format(score), 1, (255, 255, 255))
x = (random.choice(letter))
xupdate = font.render(format(x), 1, (255, 255, 255))
print x
#mainloop
while True:
clock.tick(30)
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
if event.type == USEREVENT:
if time_a > 0:
time_a -= 1
else:
break
#else:
#pygame.time.set_timer(USEREVENT, 0)
elif event.type == KEYDOWN:
if event.key == K_a:
if not a_on:
# Set for 1 second (1000 milliseconds)
pygame.time.set_timer(USEREVENT, 1000)
a_on = True
else:
# The other one should turn on immediately
pygame.time.set_timer(USEREVENT, 0)
pygame.time.set_timer(USEREVENT+1, 1000)
if x == "b" and event.key == K_b:
n3 = time.time()-n1
print n3
score += 1
n1 = time.time()
scoreupdate = font.render(format(score), 1, (255, 255, 255))
print "B button Hit"
print score
x = (random.choice(letter))
xupdate = font.render(format(x), 1, (255, 255, 255))
print x
if x == "c" and event.key == K_c:
n3 = time.time()-n1
print n3
score += 1
scoreupdate = font.render(format(score), 1, (255, 255, 255))
n1 = time.time()
print "C button Hit"
print score
x = (random.choice(letter))
xupdate = font.render(format(x), 1, (255, 255, 255))
print x
if event.key == K_PAUSE or event.key == K_p:
#pause both timers
pygame.time.set_timer(USEREVENT+1, 0)
pygame.time.set_timer(USEREVENT, 0)
#pygame.time.set_timer(USEREVENT, 1000)
#a_on = True
# Format time into minutes:seconds
#pygame.time.set_timer(USEREVENT, 1000)
time_a_str = "%d:%02d" % (int(time_a/60),int(time_a%60))
time_a_txt = font.render(time_a_str, 1, (255, 255, 255))
score_txt = scoreupdate
letter_txt = xupdate
letter_rect = letter_txt.get_rect()
letter_rect.center = (160, 30)
score_rect = score_txt.get_rect()
score_rect.center = (30, 30)
time_a_rect = time_a_txt.get_rect()
time_a_rect.center = (310, 310)
screen.blit(background, rect)
screen.blit(xupdate, letter_rect)
screen.blit(scoreupdate, score_rect)
screen.blit(time_a_txt, time_a_rect)
pygame.display.update()
#end mainloop
I would look at this example of a timer. It should have what you need.