I have a problem where I want "GAME OVER" to appear when the playable rerctangle touches the boundaries, but it won't appear. I've tried several positions where I could include the screen.blit() command, but none of them work. The game seems to just ignore the command as everything else works perfectly fine.
import pygame, sys
from pygame.locals import *
from time import*
from random import*
size = (1300, 700)
screen = pygame.display.set_mode(size)
screen.fill(Color("blue"))
pygame.display.set_caption('Game')
pygame.init()
done = True
pygame.font.init()
myfont = pygame.font.SysFont('Arial Black', 30)
textsurface = myfont.render('GAME OVER', False, Color("green"))
x1 = 310
y1 = 650
x2 = 20
y2 = 20
re1 = 200
re2 = 0
re3 = 100
re4 = 700
fpsClock = pygame.time.Clock()
FPS = 1000
def setter():
screen.fill(Color("blue"))
pygame.draw.circle(screen, Color("yellow"), (circlx, circly), 20)
pygame.draw.rect(screen, Color('red'), (re1, re2, re3, re4))
def write():
screen.blit(textsurface, (300, 300))
sleep(0.5)
class Cube():
def __init__(self, x1, y1, x2, y2):
self.x1 = x1
self.y1 = y1
self.x2 = 20
self.y2 = 20
def cre(self):
pygame.draw.rect(screen, Color('green'), (self.x1, self.y1,
self.x2, self.y2))
def right(self):
setter()
self.x1 += 0.2
self.cre()
def left(self):
setter()
self.x1 -= 0.2
self.cre()
def up(self):
setter()
self.y1 -= 0.2
self.cre()
def down(self):
setter()
self.y1 += 0.2
self.cre()
def getx1(self):
return self.x1
def getx2(self):
return self.x1 + self.x2
def gety1(self):
return self.y1
def gety2(self):
return self.y1 + self.y2
def overlap():
if ((re1 <= cube.getx1() <= (re1 + re3)) and (re2 <= cube.gety1() <=
(re2 + re4))) or ((re1 <= cube.getx2() <= (re1 + re3)) and (re2 <=
cube.gety1() <= (re2 + re4))) or ((re1 <= cube.getx1() <= (re1 + re3)) and
(re2 <= cube.gety2() <= (re2 + re4))) or ((re1 <= cube.getx2() <= (re1 +
re3)) and (re2 <= cube.gety2() <= (re2 + re4))):
write()
#This is where it doesn't work.
pygame.quit()
sys.exit()
setter()
cube = Cube(x1, y1, x2, y2)
cube.cre()
while done == True:
keys = pygame.key.get_pressed()
if keys[K_RIGHT]:
overlap()
cube.right()
if keys[K_LEFT]:
overlap()
cube.left()
if keys[K_UP]:
overlap()
cube.up()
if keys[K_DOWN]:
overlap()
cube.down()
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = False
pygame.display.flip()
fpsClock.tick(FPS)
pygame.quit()
sys.exit()
the function setter()
is called immediately (e.g. in cube.right()
) after the function write()
was called in overlap()
. Since setter()
fills the entire window, the text is overwritten immediately and it is never shown.
Remove all the calls to the drawing operations like setter()
, write()
and self.cre()
from the entire code.
e.g.
class Cube():
def right(self):
# setter() <--- remove
self.x1 += 0.2
# self.cre() <--- remove
Change the function overlap
to a function which returns a state (True
or False
):
def overlap():
return ((re1 <= cube.getx1() <= (re1 + re3)) and (re2 <= cube.gety1() <=
(re2 + re4))) or ((re1 <= cube.getx2() <= (re1 + re3)) and (re2 <=
cube.gety1() <= (re2 + re4))) or ((re1 <= cube.getx1() <= (re1 + re3)) and
(re2 <= cube.gety2() <= (re2 + re4))) or ((re1 <= cube.getx2() <= (re1 +
re3)) and (re2 <= cube.gety2() <= (re2 + re4)))
Do all the drawing operations at the end of the main loop:
while done == True:
keys = pygame.key.get_pressed()
if keys[K_RIGHT]:
cube.right()
if keys[K_LEFT]:
cube.left()
if keys[K_UP]:
cube.up()
if keys[K_DOWN]:
cube.down()
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = False
isOverlapping = overlap()
setter()
cube.cre()
if isOverlapping:
write()
pygame.display.flip()
fpsClock.tick(FPS)