我遇到一个问题,当可播放的矩形碰到边界时,我希望出现“游戏结束”,但不会出现。我尝试了几个可以包含screen.blit()命令的位置,但是它们都不起作用。游戏似乎只是忽略了该命令,因为其他所有东西都运行良好。
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()
答案 0 :(得分:1)
在setter()
中调用函数cube.right()
之后,函数write()
立即被调用(例如,在overlap()
中)。由于setter()
会填满整个窗口,因此文本会立即被覆盖,并且永远不会显示。
从整个代码中删除所有对绘图操作的调用,例如setter()
,write()
和self.cre()
。
例如
class Cube():
def right(self):
# setter() <--- remove
self.x1 += 0.2
# self.cre() <--- remove
将函数overlap
更改为返回状态(True
或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)))
在主循环末尾执行所有绘制操作:
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)
答案 1 :(得分:0)
您需要在pygame.display.flip()
之后添加screen.blit(textsurface, (300, 300))
,这意味着38.行。希望对您有帮助。