how do i add this stopwatch code into my game?












2















i found this pygame program online, here is the code:



import sys, pygame, random

class Breakout():

def main(self):

xspeed_init = 6
yspeed_init = 6
max_lives = 5
bat_speed = 30
score = 0
bgcolour = 0x2F, 0x4F, 0x4F # darkslategrey
size = width, height = 640, 480

pygame.init()
screen = pygame.display.set_mode(size)
#screen = pygame.display.set_mode(size, pygame.FULLSCREEN)

bat = pygame.image.load("bat.png").convert()
batrect = bat.get_rect()

ball = pygame.image.load("ball.png").convert()
ball.set_colorkey((255, 255, 255))
ballrect = ball.get_rect()

pong = pygame.mixer.Sound('Blip_1-Surround-147.wav')
pong.set_volume(10)

wall = Wall()
wall.build_wall(width)

# Initialise ready for game loop
batrect = batrect.move((width / 2) - (batrect.right / 2), height - 20)
ballrect = ballrect.move(width / 2, height / 2)
xspeed = xspeed_init
yspeed = yspeed_init
lives = max_lives
clock = pygame.time.Clock()
pygame.key.set_repeat(1,30)
pygame.mouse.set_visible(0) # turn off mouse pointer

while 1:

# 60 frames per second
clock.tick(60)

# process key presses
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
sys.exit()
if event.key == pygame.K_LEFT:
batrect = batrect.move(-bat_speed, 0)
if (batrect.left < 0):
batrect.left = 0
if event.key == pygame.K_RIGHT:
batrect = batrect.move(bat_speed, 0)
if (batrect.right > width):
batrect.right = width

# check if bat has hit ball
if ballrect.bottom >= batrect.top and
ballrect.bottom <= batrect.bottom and
ballrect.right >= batrect.left and
ballrect.left <= batrect.right:
yspeed = -yspeed
pong.play(0)
offset = ballrect.center[0] - batrect.center[0]
# offset > 0 means ball has hit RHS of bat
# vary angle of ball depending on where ball hits bat
if offset > 0:
if offset > 30:
xspeed = 7
elif offset > 23:
xspeed = 6
elif offset > 17:
xspeed = 5
else:
if offset < -30:
xspeed = -7
elif offset < -23:
xspeed = -6
elif xspeed < -17:
xspeed = -5

# move bat/ball
ballrect = ballrect.move(xspeed, yspeed)
if ballrect.left < 0 or ballrect.right > width:
xspeed = -xspeed
pong.play(0)
if ballrect.top < 0:
yspeed = -yspeed
pong.play(0)

# check if ball has gone past bat - lose a life
if ballrect.top > height:
lives -= 1
# start a new ball
xspeed = xspeed_init
rand = random.random()
if random.random() > 0.5:
xspeed = -xspeed
yspeed = yspeed_init
ballrect.center = width * random.random(), height / 3
if lives == 0:
msg = pygame.font.Font(None,70).render("Game Over", True, (0,255,255), bgcolour)
msgrect = msg.get_rect()
msgrect = msgrect.move(width / 2 - (msgrect.center[0]), height / 3)
screen.blit(msg, msgrect)
pygame.display.flip()
# process key presses
# - ESC to quit
# - any other key to restart game
while 1:
restart = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
sys.exit()
if not (event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT):
restart = True
if restart:
screen.fill(bgcolour)
wall.build_wall(width)
lives = max_lives
score = 0
break

if xspeed < 0 and ballrect.left < 0:
xspeed = -xspeed
pong.play(0)

if xspeed > 0 and ballrect.right > width:
xspeed = -xspeed
pong.play(0)

# check if ball has hit wall
# if yes yhen delete brick and change ball direction
index = ballrect.collidelist(wall.brickrect)
if index != -1:
if ballrect.center[0] > wall.brickrect[index].right or
ballrect.center[0] < wall.brickrect[index].left:
xspeed = -xspeed
else:
yspeed = -yspeed
pong.play(0)
wall.brickrect[index:index + 1] =
score += 10

screen.fill(bgcolour)
scoretext = pygame.font.Font(None,40).render(str(score), True, (0,255,255), bgcolour)
scoretextrect = scoretext.get_rect()
scoretextrect = scoretextrect.move(width - scoretextrect.right, 0)
screen.blit(scoretext, scoretextrect)

for i in range(0, len(wall.brickrect)):
screen.blit(wall.brick, wall.brickrect[i])

# if wall completely gone then rebuild it
if wall.brickrect == :
wall.build_wall(width)
xspeed = xspeed_init
yspeed = yspeed_init
ballrect.center = width / 2, height / 3

screen.blit(ball, ballrect)
screen.blit(bat, batrect)
pygame.display.flip()

class Wall():

def __init__(self):
self.brick = pygame.image.load("brick.png").convert()
brickrect = self.brick.get_rect()
self.bricklength = brickrect.right - brickrect.left
self.brickheight = brickrect.bottom - brickrect.top

def build_wall(self, width):
xpos = 0
ypos = 60
adj = 0
self.brickrect =
for i in range (0, 52):
if xpos > width:
if adj == 0:
adj = self.bricklength / 2
else:
adj = 0
xpos = -adj
ypos += self.brickheight

self.brickrect.append(self.brick.get_rect())
self.brickrect[i] = self.brickrect[i].move(xpos, ypos)
xpos = xpos + self.bricklength

if __name__ == '__main__':
br = Breakout()
br.main()


The code works fine but how do i add a stopwatch code into this code? I have a stopwatch code which looks like this:



import pygame
import pygame.freetype

def main():
pygame.init()
screen=pygame.display.set_mode((400, 300))
clock=pygame.time.Clock()
font=pygame.freetype.SysFont(None, 34)
font.origin=True
while True:
for e in pygame.event.get():
if e.type == pygame.QUIT: return
screen.fill(pygame.Color('grey12'))
ticks=pygame.time.get_ticks()
millis=ticks%1000
seconds=int(ticks/1000 % 60)
minutes=int(ticks/60000 % 24)
out='{minutes:02d}:{seconds:02d}:{millis}'.format(minutes=minutes, millis=millis, seconds=seconds)
font.render_to(screen, (100, 100), out, pygame.Color('dodgerblue'))
pygame.display.flip()
clock.tick(60)

if __name__ == '__main__': main()


Where should i add this code into the main game so the player can know how long had he/she played the game. I am still new to this python program. Still learning.










share|improve this question

























  • When do you want the stopwatch code to run?

    – Azazel
    Nov 24 '18 at 4:37











  • When the game starts it will display the score and time

    – David Lee
    Nov 24 '18 at 4:39











  • You want it to run inside br.main() ?

    – Azazel
    Nov 24 '18 at 4:41











  • When the game start the timer will run and display on the game

    – David Lee
    Nov 24 '18 at 4:52













  • Are you familar with import? or read how-to-import-other-python-files

    – stovfl
    Nov 24 '18 at 13:11
















2















i found this pygame program online, here is the code:



import sys, pygame, random

class Breakout():

def main(self):

xspeed_init = 6
yspeed_init = 6
max_lives = 5
bat_speed = 30
score = 0
bgcolour = 0x2F, 0x4F, 0x4F # darkslategrey
size = width, height = 640, 480

pygame.init()
screen = pygame.display.set_mode(size)
#screen = pygame.display.set_mode(size, pygame.FULLSCREEN)

bat = pygame.image.load("bat.png").convert()
batrect = bat.get_rect()

ball = pygame.image.load("ball.png").convert()
ball.set_colorkey((255, 255, 255))
ballrect = ball.get_rect()

pong = pygame.mixer.Sound('Blip_1-Surround-147.wav')
pong.set_volume(10)

wall = Wall()
wall.build_wall(width)

# Initialise ready for game loop
batrect = batrect.move((width / 2) - (batrect.right / 2), height - 20)
ballrect = ballrect.move(width / 2, height / 2)
xspeed = xspeed_init
yspeed = yspeed_init
lives = max_lives
clock = pygame.time.Clock()
pygame.key.set_repeat(1,30)
pygame.mouse.set_visible(0) # turn off mouse pointer

while 1:

# 60 frames per second
clock.tick(60)

# process key presses
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
sys.exit()
if event.key == pygame.K_LEFT:
batrect = batrect.move(-bat_speed, 0)
if (batrect.left < 0):
batrect.left = 0
if event.key == pygame.K_RIGHT:
batrect = batrect.move(bat_speed, 0)
if (batrect.right > width):
batrect.right = width

# check if bat has hit ball
if ballrect.bottom >= batrect.top and
ballrect.bottom <= batrect.bottom and
ballrect.right >= batrect.left and
ballrect.left <= batrect.right:
yspeed = -yspeed
pong.play(0)
offset = ballrect.center[0] - batrect.center[0]
# offset > 0 means ball has hit RHS of bat
# vary angle of ball depending on where ball hits bat
if offset > 0:
if offset > 30:
xspeed = 7
elif offset > 23:
xspeed = 6
elif offset > 17:
xspeed = 5
else:
if offset < -30:
xspeed = -7
elif offset < -23:
xspeed = -6
elif xspeed < -17:
xspeed = -5

# move bat/ball
ballrect = ballrect.move(xspeed, yspeed)
if ballrect.left < 0 or ballrect.right > width:
xspeed = -xspeed
pong.play(0)
if ballrect.top < 0:
yspeed = -yspeed
pong.play(0)

# check if ball has gone past bat - lose a life
if ballrect.top > height:
lives -= 1
# start a new ball
xspeed = xspeed_init
rand = random.random()
if random.random() > 0.5:
xspeed = -xspeed
yspeed = yspeed_init
ballrect.center = width * random.random(), height / 3
if lives == 0:
msg = pygame.font.Font(None,70).render("Game Over", True, (0,255,255), bgcolour)
msgrect = msg.get_rect()
msgrect = msgrect.move(width / 2 - (msgrect.center[0]), height / 3)
screen.blit(msg, msgrect)
pygame.display.flip()
# process key presses
# - ESC to quit
# - any other key to restart game
while 1:
restart = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
sys.exit()
if not (event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT):
restart = True
if restart:
screen.fill(bgcolour)
wall.build_wall(width)
lives = max_lives
score = 0
break

if xspeed < 0 and ballrect.left < 0:
xspeed = -xspeed
pong.play(0)

if xspeed > 0 and ballrect.right > width:
xspeed = -xspeed
pong.play(0)

# check if ball has hit wall
# if yes yhen delete brick and change ball direction
index = ballrect.collidelist(wall.brickrect)
if index != -1:
if ballrect.center[0] > wall.brickrect[index].right or
ballrect.center[0] < wall.brickrect[index].left:
xspeed = -xspeed
else:
yspeed = -yspeed
pong.play(0)
wall.brickrect[index:index + 1] =
score += 10

screen.fill(bgcolour)
scoretext = pygame.font.Font(None,40).render(str(score), True, (0,255,255), bgcolour)
scoretextrect = scoretext.get_rect()
scoretextrect = scoretextrect.move(width - scoretextrect.right, 0)
screen.blit(scoretext, scoretextrect)

for i in range(0, len(wall.brickrect)):
screen.blit(wall.brick, wall.brickrect[i])

# if wall completely gone then rebuild it
if wall.brickrect == :
wall.build_wall(width)
xspeed = xspeed_init
yspeed = yspeed_init
ballrect.center = width / 2, height / 3

screen.blit(ball, ballrect)
screen.blit(bat, batrect)
pygame.display.flip()

class Wall():

def __init__(self):
self.brick = pygame.image.load("brick.png").convert()
brickrect = self.brick.get_rect()
self.bricklength = brickrect.right - brickrect.left
self.brickheight = brickrect.bottom - brickrect.top

def build_wall(self, width):
xpos = 0
ypos = 60
adj = 0
self.brickrect =
for i in range (0, 52):
if xpos > width:
if adj == 0:
adj = self.bricklength / 2
else:
adj = 0
xpos = -adj
ypos += self.brickheight

self.brickrect.append(self.brick.get_rect())
self.brickrect[i] = self.brickrect[i].move(xpos, ypos)
xpos = xpos + self.bricklength

if __name__ == '__main__':
br = Breakout()
br.main()


The code works fine but how do i add a stopwatch code into this code? I have a stopwatch code which looks like this:



import pygame
import pygame.freetype

def main():
pygame.init()
screen=pygame.display.set_mode((400, 300))
clock=pygame.time.Clock()
font=pygame.freetype.SysFont(None, 34)
font.origin=True
while True:
for e in pygame.event.get():
if e.type == pygame.QUIT: return
screen.fill(pygame.Color('grey12'))
ticks=pygame.time.get_ticks()
millis=ticks%1000
seconds=int(ticks/1000 % 60)
minutes=int(ticks/60000 % 24)
out='{minutes:02d}:{seconds:02d}:{millis}'.format(minutes=minutes, millis=millis, seconds=seconds)
font.render_to(screen, (100, 100), out, pygame.Color('dodgerblue'))
pygame.display.flip()
clock.tick(60)

if __name__ == '__main__': main()


Where should i add this code into the main game so the player can know how long had he/she played the game. I am still new to this python program. Still learning.










share|improve this question

























  • When do you want the stopwatch code to run?

    – Azazel
    Nov 24 '18 at 4:37











  • When the game starts it will display the score and time

    – David Lee
    Nov 24 '18 at 4:39











  • You want it to run inside br.main() ?

    – Azazel
    Nov 24 '18 at 4:41











  • When the game start the timer will run and display on the game

    – David Lee
    Nov 24 '18 at 4:52













  • Are you familar with import? or read how-to-import-other-python-files

    – stovfl
    Nov 24 '18 at 13:11














2












2








2








i found this pygame program online, here is the code:



import sys, pygame, random

class Breakout():

def main(self):

xspeed_init = 6
yspeed_init = 6
max_lives = 5
bat_speed = 30
score = 0
bgcolour = 0x2F, 0x4F, 0x4F # darkslategrey
size = width, height = 640, 480

pygame.init()
screen = pygame.display.set_mode(size)
#screen = pygame.display.set_mode(size, pygame.FULLSCREEN)

bat = pygame.image.load("bat.png").convert()
batrect = bat.get_rect()

ball = pygame.image.load("ball.png").convert()
ball.set_colorkey((255, 255, 255))
ballrect = ball.get_rect()

pong = pygame.mixer.Sound('Blip_1-Surround-147.wav')
pong.set_volume(10)

wall = Wall()
wall.build_wall(width)

# Initialise ready for game loop
batrect = batrect.move((width / 2) - (batrect.right / 2), height - 20)
ballrect = ballrect.move(width / 2, height / 2)
xspeed = xspeed_init
yspeed = yspeed_init
lives = max_lives
clock = pygame.time.Clock()
pygame.key.set_repeat(1,30)
pygame.mouse.set_visible(0) # turn off mouse pointer

while 1:

# 60 frames per second
clock.tick(60)

# process key presses
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
sys.exit()
if event.key == pygame.K_LEFT:
batrect = batrect.move(-bat_speed, 0)
if (batrect.left < 0):
batrect.left = 0
if event.key == pygame.K_RIGHT:
batrect = batrect.move(bat_speed, 0)
if (batrect.right > width):
batrect.right = width

# check if bat has hit ball
if ballrect.bottom >= batrect.top and
ballrect.bottom <= batrect.bottom and
ballrect.right >= batrect.left and
ballrect.left <= batrect.right:
yspeed = -yspeed
pong.play(0)
offset = ballrect.center[0] - batrect.center[0]
# offset > 0 means ball has hit RHS of bat
# vary angle of ball depending on where ball hits bat
if offset > 0:
if offset > 30:
xspeed = 7
elif offset > 23:
xspeed = 6
elif offset > 17:
xspeed = 5
else:
if offset < -30:
xspeed = -7
elif offset < -23:
xspeed = -6
elif xspeed < -17:
xspeed = -5

# move bat/ball
ballrect = ballrect.move(xspeed, yspeed)
if ballrect.left < 0 or ballrect.right > width:
xspeed = -xspeed
pong.play(0)
if ballrect.top < 0:
yspeed = -yspeed
pong.play(0)

# check if ball has gone past bat - lose a life
if ballrect.top > height:
lives -= 1
# start a new ball
xspeed = xspeed_init
rand = random.random()
if random.random() > 0.5:
xspeed = -xspeed
yspeed = yspeed_init
ballrect.center = width * random.random(), height / 3
if lives == 0:
msg = pygame.font.Font(None,70).render("Game Over", True, (0,255,255), bgcolour)
msgrect = msg.get_rect()
msgrect = msgrect.move(width / 2 - (msgrect.center[0]), height / 3)
screen.blit(msg, msgrect)
pygame.display.flip()
# process key presses
# - ESC to quit
# - any other key to restart game
while 1:
restart = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
sys.exit()
if not (event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT):
restart = True
if restart:
screen.fill(bgcolour)
wall.build_wall(width)
lives = max_lives
score = 0
break

if xspeed < 0 and ballrect.left < 0:
xspeed = -xspeed
pong.play(0)

if xspeed > 0 and ballrect.right > width:
xspeed = -xspeed
pong.play(0)

# check if ball has hit wall
# if yes yhen delete brick and change ball direction
index = ballrect.collidelist(wall.brickrect)
if index != -1:
if ballrect.center[0] > wall.brickrect[index].right or
ballrect.center[0] < wall.brickrect[index].left:
xspeed = -xspeed
else:
yspeed = -yspeed
pong.play(0)
wall.brickrect[index:index + 1] =
score += 10

screen.fill(bgcolour)
scoretext = pygame.font.Font(None,40).render(str(score), True, (0,255,255), bgcolour)
scoretextrect = scoretext.get_rect()
scoretextrect = scoretextrect.move(width - scoretextrect.right, 0)
screen.blit(scoretext, scoretextrect)

for i in range(0, len(wall.brickrect)):
screen.blit(wall.brick, wall.brickrect[i])

# if wall completely gone then rebuild it
if wall.brickrect == :
wall.build_wall(width)
xspeed = xspeed_init
yspeed = yspeed_init
ballrect.center = width / 2, height / 3

screen.blit(ball, ballrect)
screen.blit(bat, batrect)
pygame.display.flip()

class Wall():

def __init__(self):
self.brick = pygame.image.load("brick.png").convert()
brickrect = self.brick.get_rect()
self.bricklength = brickrect.right - brickrect.left
self.brickheight = brickrect.bottom - brickrect.top

def build_wall(self, width):
xpos = 0
ypos = 60
adj = 0
self.brickrect =
for i in range (0, 52):
if xpos > width:
if adj == 0:
adj = self.bricklength / 2
else:
adj = 0
xpos = -adj
ypos += self.brickheight

self.brickrect.append(self.brick.get_rect())
self.brickrect[i] = self.brickrect[i].move(xpos, ypos)
xpos = xpos + self.bricklength

if __name__ == '__main__':
br = Breakout()
br.main()


The code works fine but how do i add a stopwatch code into this code? I have a stopwatch code which looks like this:



import pygame
import pygame.freetype

def main():
pygame.init()
screen=pygame.display.set_mode((400, 300))
clock=pygame.time.Clock()
font=pygame.freetype.SysFont(None, 34)
font.origin=True
while True:
for e in pygame.event.get():
if e.type == pygame.QUIT: return
screen.fill(pygame.Color('grey12'))
ticks=pygame.time.get_ticks()
millis=ticks%1000
seconds=int(ticks/1000 % 60)
minutes=int(ticks/60000 % 24)
out='{minutes:02d}:{seconds:02d}:{millis}'.format(minutes=minutes, millis=millis, seconds=seconds)
font.render_to(screen, (100, 100), out, pygame.Color('dodgerblue'))
pygame.display.flip()
clock.tick(60)

if __name__ == '__main__': main()


Where should i add this code into the main game so the player can know how long had he/she played the game. I am still new to this python program. Still learning.










share|improve this question
















i found this pygame program online, here is the code:



import sys, pygame, random

class Breakout():

def main(self):

xspeed_init = 6
yspeed_init = 6
max_lives = 5
bat_speed = 30
score = 0
bgcolour = 0x2F, 0x4F, 0x4F # darkslategrey
size = width, height = 640, 480

pygame.init()
screen = pygame.display.set_mode(size)
#screen = pygame.display.set_mode(size, pygame.FULLSCREEN)

bat = pygame.image.load("bat.png").convert()
batrect = bat.get_rect()

ball = pygame.image.load("ball.png").convert()
ball.set_colorkey((255, 255, 255))
ballrect = ball.get_rect()

pong = pygame.mixer.Sound('Blip_1-Surround-147.wav')
pong.set_volume(10)

wall = Wall()
wall.build_wall(width)

# Initialise ready for game loop
batrect = batrect.move((width / 2) - (batrect.right / 2), height - 20)
ballrect = ballrect.move(width / 2, height / 2)
xspeed = xspeed_init
yspeed = yspeed_init
lives = max_lives
clock = pygame.time.Clock()
pygame.key.set_repeat(1,30)
pygame.mouse.set_visible(0) # turn off mouse pointer

while 1:

# 60 frames per second
clock.tick(60)

# process key presses
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
sys.exit()
if event.key == pygame.K_LEFT:
batrect = batrect.move(-bat_speed, 0)
if (batrect.left < 0):
batrect.left = 0
if event.key == pygame.K_RIGHT:
batrect = batrect.move(bat_speed, 0)
if (batrect.right > width):
batrect.right = width

# check if bat has hit ball
if ballrect.bottom >= batrect.top and
ballrect.bottom <= batrect.bottom and
ballrect.right >= batrect.left and
ballrect.left <= batrect.right:
yspeed = -yspeed
pong.play(0)
offset = ballrect.center[0] - batrect.center[0]
# offset > 0 means ball has hit RHS of bat
# vary angle of ball depending on where ball hits bat
if offset > 0:
if offset > 30:
xspeed = 7
elif offset > 23:
xspeed = 6
elif offset > 17:
xspeed = 5
else:
if offset < -30:
xspeed = -7
elif offset < -23:
xspeed = -6
elif xspeed < -17:
xspeed = -5

# move bat/ball
ballrect = ballrect.move(xspeed, yspeed)
if ballrect.left < 0 or ballrect.right > width:
xspeed = -xspeed
pong.play(0)
if ballrect.top < 0:
yspeed = -yspeed
pong.play(0)

# check if ball has gone past bat - lose a life
if ballrect.top > height:
lives -= 1
# start a new ball
xspeed = xspeed_init
rand = random.random()
if random.random() > 0.5:
xspeed = -xspeed
yspeed = yspeed_init
ballrect.center = width * random.random(), height / 3
if lives == 0:
msg = pygame.font.Font(None,70).render("Game Over", True, (0,255,255), bgcolour)
msgrect = msg.get_rect()
msgrect = msgrect.move(width / 2 - (msgrect.center[0]), height / 3)
screen.blit(msg, msgrect)
pygame.display.flip()
# process key presses
# - ESC to quit
# - any other key to restart game
while 1:
restart = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
sys.exit()
if not (event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT):
restart = True
if restart:
screen.fill(bgcolour)
wall.build_wall(width)
lives = max_lives
score = 0
break

if xspeed < 0 and ballrect.left < 0:
xspeed = -xspeed
pong.play(0)

if xspeed > 0 and ballrect.right > width:
xspeed = -xspeed
pong.play(0)

# check if ball has hit wall
# if yes yhen delete brick and change ball direction
index = ballrect.collidelist(wall.brickrect)
if index != -1:
if ballrect.center[0] > wall.brickrect[index].right or
ballrect.center[0] < wall.brickrect[index].left:
xspeed = -xspeed
else:
yspeed = -yspeed
pong.play(0)
wall.brickrect[index:index + 1] =
score += 10

screen.fill(bgcolour)
scoretext = pygame.font.Font(None,40).render(str(score), True, (0,255,255), bgcolour)
scoretextrect = scoretext.get_rect()
scoretextrect = scoretextrect.move(width - scoretextrect.right, 0)
screen.blit(scoretext, scoretextrect)

for i in range(0, len(wall.brickrect)):
screen.blit(wall.brick, wall.brickrect[i])

# if wall completely gone then rebuild it
if wall.brickrect == :
wall.build_wall(width)
xspeed = xspeed_init
yspeed = yspeed_init
ballrect.center = width / 2, height / 3

screen.blit(ball, ballrect)
screen.blit(bat, batrect)
pygame.display.flip()

class Wall():

def __init__(self):
self.brick = pygame.image.load("brick.png").convert()
brickrect = self.brick.get_rect()
self.bricklength = brickrect.right - brickrect.left
self.brickheight = brickrect.bottom - brickrect.top

def build_wall(self, width):
xpos = 0
ypos = 60
adj = 0
self.brickrect =
for i in range (0, 52):
if xpos > width:
if adj == 0:
adj = self.bricklength / 2
else:
adj = 0
xpos = -adj
ypos += self.brickheight

self.brickrect.append(self.brick.get_rect())
self.brickrect[i] = self.brickrect[i].move(xpos, ypos)
xpos = xpos + self.bricklength

if __name__ == '__main__':
br = Breakout()
br.main()


The code works fine but how do i add a stopwatch code into this code? I have a stopwatch code which looks like this:



import pygame
import pygame.freetype

def main():
pygame.init()
screen=pygame.display.set_mode((400, 300))
clock=pygame.time.Clock()
font=pygame.freetype.SysFont(None, 34)
font.origin=True
while True:
for e in pygame.event.get():
if e.type == pygame.QUIT: return
screen.fill(pygame.Color('grey12'))
ticks=pygame.time.get_ticks()
millis=ticks%1000
seconds=int(ticks/1000 % 60)
minutes=int(ticks/60000 % 24)
out='{minutes:02d}:{seconds:02d}:{millis}'.format(minutes=minutes, millis=millis, seconds=seconds)
font.render_to(screen, (100, 100), out, pygame.Color('dodgerblue'))
pygame.display.flip()
clock.tick(60)

if __name__ == '__main__': main()


Where should i add this code into the main game so the player can know how long had he/she played the game. I am still new to this python program. Still learning.







python pygame






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 7:34









Azazel

878




878










asked Nov 24 '18 at 4:33









David LeeDavid Lee

214




214













  • When do you want the stopwatch code to run?

    – Azazel
    Nov 24 '18 at 4:37











  • When the game starts it will display the score and time

    – David Lee
    Nov 24 '18 at 4:39











  • You want it to run inside br.main() ?

    – Azazel
    Nov 24 '18 at 4:41











  • When the game start the timer will run and display on the game

    – David Lee
    Nov 24 '18 at 4:52













  • Are you familar with import? or read how-to-import-other-python-files

    – stovfl
    Nov 24 '18 at 13:11



















  • When do you want the stopwatch code to run?

    – Azazel
    Nov 24 '18 at 4:37











  • When the game starts it will display the score and time

    – David Lee
    Nov 24 '18 at 4:39











  • You want it to run inside br.main() ?

    – Azazel
    Nov 24 '18 at 4:41











  • When the game start the timer will run and display on the game

    – David Lee
    Nov 24 '18 at 4:52













  • Are you familar with import? or read how-to-import-other-python-files

    – stovfl
    Nov 24 '18 at 13:11

















When do you want the stopwatch code to run?

– Azazel
Nov 24 '18 at 4:37





When do you want the stopwatch code to run?

– Azazel
Nov 24 '18 at 4:37













When the game starts it will display the score and time

– David Lee
Nov 24 '18 at 4:39





When the game starts it will display the score and time

– David Lee
Nov 24 '18 at 4:39













You want it to run inside br.main() ?

– Azazel
Nov 24 '18 at 4:41





You want it to run inside br.main() ?

– Azazel
Nov 24 '18 at 4:41













When the game start the timer will run and display on the game

– David Lee
Nov 24 '18 at 4:52







When the game start the timer will run and display on the game

– David Lee
Nov 24 '18 at 4:52















Are you familar with import? or read how-to-import-other-python-files

– stovfl
Nov 24 '18 at 13:11





Are you familar with import? or read how-to-import-other-python-files

– stovfl
Nov 24 '18 at 13:11












1 Answer
1






active

oldest

votes


















0














You can just insert the timer into the main method of the Breakout class.



import pygame.freetype


font = pygame.freetype.SysFont(None, 34)

class Breakout():

def main(self):
# Other code omitted ...
while 1:
clock.tick(60)
for event in pygame.event.get():
# ...

ticks = pygame.time.get_ticks()
millis = ticks%1000
seconds = int(ticks/1000 % 60)
minutes = int(ticks/60000 % 24)
out = f'{minutes:02d}:{seconds:02d}:{millis}'

# In the drawing section before pygame.display.flip gets called.
font.render_to(screen, (10, 30), out, pygame.Color('dodgerblue'))
pygame.display.flip()


If you want to reset the timer after the game is over, check out one of these timers: Countdown timer in Pygame






share|improve this answer


























  • it says font is not defined

    – David Lee
    Nov 24 '18 at 16:02











  • You have to define the font object.

    – skrx
    Nov 24 '18 at 16:03











  • i defined like this: font=pygame.freetype.SysFont(None, 34) at the main and it shows up error

    – David Lee
    Nov 24 '18 at 16:04











  • Define it in the global scope or above the while loop in the main function. And please post the traceback if it still doesn't work.

    – skrx
    Nov 24 '18 at 16:05













  • okay it's working now, thank you. I will learn more python code

    – David Lee
    Nov 24 '18 at 16:08











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53455151%2fhow-do-i-add-this-stopwatch-code-into-my-game%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You can just insert the timer into the main method of the Breakout class.



import pygame.freetype


font = pygame.freetype.SysFont(None, 34)

class Breakout():

def main(self):
# Other code omitted ...
while 1:
clock.tick(60)
for event in pygame.event.get():
# ...

ticks = pygame.time.get_ticks()
millis = ticks%1000
seconds = int(ticks/1000 % 60)
minutes = int(ticks/60000 % 24)
out = f'{minutes:02d}:{seconds:02d}:{millis}'

# In the drawing section before pygame.display.flip gets called.
font.render_to(screen, (10, 30), out, pygame.Color('dodgerblue'))
pygame.display.flip()


If you want to reset the timer after the game is over, check out one of these timers: Countdown timer in Pygame






share|improve this answer


























  • it says font is not defined

    – David Lee
    Nov 24 '18 at 16:02











  • You have to define the font object.

    – skrx
    Nov 24 '18 at 16:03











  • i defined like this: font=pygame.freetype.SysFont(None, 34) at the main and it shows up error

    – David Lee
    Nov 24 '18 at 16:04











  • Define it in the global scope or above the while loop in the main function. And please post the traceback if it still doesn't work.

    – skrx
    Nov 24 '18 at 16:05













  • okay it's working now, thank you. I will learn more python code

    – David Lee
    Nov 24 '18 at 16:08
















0














You can just insert the timer into the main method of the Breakout class.



import pygame.freetype


font = pygame.freetype.SysFont(None, 34)

class Breakout():

def main(self):
# Other code omitted ...
while 1:
clock.tick(60)
for event in pygame.event.get():
# ...

ticks = pygame.time.get_ticks()
millis = ticks%1000
seconds = int(ticks/1000 % 60)
minutes = int(ticks/60000 % 24)
out = f'{minutes:02d}:{seconds:02d}:{millis}'

# In the drawing section before pygame.display.flip gets called.
font.render_to(screen, (10, 30), out, pygame.Color('dodgerblue'))
pygame.display.flip()


If you want to reset the timer after the game is over, check out one of these timers: Countdown timer in Pygame






share|improve this answer


























  • it says font is not defined

    – David Lee
    Nov 24 '18 at 16:02











  • You have to define the font object.

    – skrx
    Nov 24 '18 at 16:03











  • i defined like this: font=pygame.freetype.SysFont(None, 34) at the main and it shows up error

    – David Lee
    Nov 24 '18 at 16:04











  • Define it in the global scope or above the while loop in the main function. And please post the traceback if it still doesn't work.

    – skrx
    Nov 24 '18 at 16:05













  • okay it's working now, thank you. I will learn more python code

    – David Lee
    Nov 24 '18 at 16:08














0












0








0







You can just insert the timer into the main method of the Breakout class.



import pygame.freetype


font = pygame.freetype.SysFont(None, 34)

class Breakout():

def main(self):
# Other code omitted ...
while 1:
clock.tick(60)
for event in pygame.event.get():
# ...

ticks = pygame.time.get_ticks()
millis = ticks%1000
seconds = int(ticks/1000 % 60)
minutes = int(ticks/60000 % 24)
out = f'{minutes:02d}:{seconds:02d}:{millis}'

# In the drawing section before pygame.display.flip gets called.
font.render_to(screen, (10, 30), out, pygame.Color('dodgerblue'))
pygame.display.flip()


If you want to reset the timer after the game is over, check out one of these timers: Countdown timer in Pygame






share|improve this answer















You can just insert the timer into the main method of the Breakout class.



import pygame.freetype


font = pygame.freetype.SysFont(None, 34)

class Breakout():

def main(self):
# Other code omitted ...
while 1:
clock.tick(60)
for event in pygame.event.get():
# ...

ticks = pygame.time.get_ticks()
millis = ticks%1000
seconds = int(ticks/1000 % 60)
minutes = int(ticks/60000 % 24)
out = f'{minutes:02d}:{seconds:02d}:{millis}'

# In the drawing section before pygame.display.flip gets called.
font.render_to(screen, (10, 30), out, pygame.Color('dodgerblue'))
pygame.display.flip()


If you want to reset the timer after the game is over, check out one of these timers: Countdown timer in Pygame







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 24 '18 at 16:03

























answered Nov 24 '18 at 15:39









skrxskrx

15.6k31834




15.6k31834













  • it says font is not defined

    – David Lee
    Nov 24 '18 at 16:02











  • You have to define the font object.

    – skrx
    Nov 24 '18 at 16:03











  • i defined like this: font=pygame.freetype.SysFont(None, 34) at the main and it shows up error

    – David Lee
    Nov 24 '18 at 16:04











  • Define it in the global scope or above the while loop in the main function. And please post the traceback if it still doesn't work.

    – skrx
    Nov 24 '18 at 16:05













  • okay it's working now, thank you. I will learn more python code

    – David Lee
    Nov 24 '18 at 16:08



















  • it says font is not defined

    – David Lee
    Nov 24 '18 at 16:02











  • You have to define the font object.

    – skrx
    Nov 24 '18 at 16:03











  • i defined like this: font=pygame.freetype.SysFont(None, 34) at the main and it shows up error

    – David Lee
    Nov 24 '18 at 16:04











  • Define it in the global scope or above the while loop in the main function. And please post the traceback if it still doesn't work.

    – skrx
    Nov 24 '18 at 16:05













  • okay it's working now, thank you. I will learn more python code

    – David Lee
    Nov 24 '18 at 16:08

















it says font is not defined

– David Lee
Nov 24 '18 at 16:02





it says font is not defined

– David Lee
Nov 24 '18 at 16:02













You have to define the font object.

– skrx
Nov 24 '18 at 16:03





You have to define the font object.

– skrx
Nov 24 '18 at 16:03













i defined like this: font=pygame.freetype.SysFont(None, 34) at the main and it shows up error

– David Lee
Nov 24 '18 at 16:04





i defined like this: font=pygame.freetype.SysFont(None, 34) at the main and it shows up error

– David Lee
Nov 24 '18 at 16:04













Define it in the global scope or above the while loop in the main function. And please post the traceback if it still doesn't work.

– skrx
Nov 24 '18 at 16:05







Define it in the global scope or above the while loop in the main function. And please post the traceback if it still doesn't work.

– skrx
Nov 24 '18 at 16:05















okay it's working now, thank you. I will learn more python code

– David Lee
Nov 24 '18 at 16:08





okay it's working now, thank you. I will learn more python code

– David Lee
Nov 24 '18 at 16:08


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53455151%2fhow-do-i-add-this-stopwatch-code-into-my-game%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Berounka

Fiat S.p.A.

Type 'String' is not a subtype of type 'int' of 'index'