我有一个使用python和pygame的代码,当满足某些条件时,它应该擦除屏幕并在中间打印“GAME OVER”字样。但是当条件得到满足时,与玩家发生碰撞的棋子就会直接通过并且不做任何事情。如何制作以便屏幕删除并打印出文字?
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()
bullet_x = 350
bullet_y = 0
bullet_x2 = 175
bullet_y2 = 0
bullet_x3 = 525
bullet_y3 = 0
circle_x = 350
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN:
circle_x += 5
bullet_y += 1
bullet_y2 += 2
bullet_y3 += 3
screen.fill(BLACK)
pygame.draw.circle(screen, GREEN, (circle_x,600), 15)
pygame.draw.circle(screen, RED, (bullet_x, bullet_y), 20)
pygame.draw.circle(screen, RED, (bullet_x2, bullet_y2), 20)
pygame.draw.circle(screen, RED, (bullet_x3, bullet_y3), 20)
if bullet_y == 800:
bullet_y = 0
bullet_x = random.randint(20, 680)
if bullet_y2 == 800:
bullet_y2 = 0
bullet_x2 = random.randint(20, 680)
if bullet_y3 == 800:
bullet_y3 = 0
bullet_x3 = random.randint(20, 680)
if circle_x == 685:
circle_x = 15
if bullet_y == 600 and bullet_x == circle_x or bullet_y2 == 600 and bullet_x2 == circle_x or bullet_y3 == 600 and bullet_x3 == circle_x:
screen.fill(BLACK)
font = pygame.font.SysFont('Calibri',40,True,False)
text = font.render("GAME OVER",True,RED)
screen.blit(text,[0,0])
pygame.display.flip()
clock.tick(300)
pygame.quit()
答案 0 :(得分:1)
将变量circle_y设置为600(circle_y = 600),在circle_x = 350的正下方。 然后将第50行更改为:
if (abs(bullet_y - circle_y) < 35 and abs(bullet_x - circle_x) < 35) or (abs(bullet_y2 - circle_y) < 35 and abs(bullet_x2 - circle_x) < 35) or abs(bullet_y3 - circle_y) < 35 and abs(bullet_x3 - circle_x) < 35:
这将解决您的碰撞问题。我可以建议你将这个表达重构为
do_circle_and_bullet_hit = abs(bullet_y - circle_y) < 35 and abs(bullet_x - circle_x) < 35
do_circle_and_bullet2_hit = abs(bullet_y2 - circle_y) < 35 and abs(bullet_x2 - circle_x) < 35
do_circle_and_bullet3_hit = abs(bullet_y3 - circle_y) < 35 and abs(bullet_x3 - circle_x) < 35
if do_circle_and_bullet_hit or do_circle_and_bullet2_hit or do_circle_and_bullet3_hit:
我们的想法是检查圆圈和每个子弹之间的距离。如果距离低于35,他们就会命中。
答案 1 :(得分:0)
我认为创建Pygame Rect Class以测试碰撞会有所帮助。它们是通过bulletrect = pygame.rect.Rect(xpos, ypos, xsize, ysize)
来完成的。稍后在代码中,您可以执行bulletrect.y +=
1,2或3来使其失效。执行bulletrect.x =
或bulletrect.y =
会更改圈子的x和y位置。要在屏幕上显示圈子,请将pygame.draw.circle(screen, RED, (bullet_x, bullet_y), 20)
或任何其他圈子更改为pygame.draw.circle(screen, RED, bulletrect.center, 20)
。要实际检查冲突,请使用colliderect方法:playerrect.colliderect(bulletrect)
。
代码是:
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()
bulletrect1 = pygame.rect.Rect((350, 0, 20, 20))
bulletrect2 = pygame.rect.Rect((175, 0, 20, 20))
bulletrect3 = pygame.rect.Rect((525, 0, 20, 20))
circlerect = pygame.rect.Rect((350, 600, 20, 20))
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN:
circle.rect.x += 5
bulletrect1.rect.y += 1
bulletrect2.rect.y2 += 2
bulletrect3.rect.y3 += 3
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)
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 circlerect.x == 685:
circlerect.x = 15
if [bulletrect.colliderect(circlerect) for bulletrect in (bulletrect1, bulletrect2, bulletrect3)]
screen.fill(BLACK)
font = pygame.font.SysFont('Calibri',40,True,False)
text = font.render("GAME OVER",True,RED)
screen.blit(text,[0,0])
pygame.display.flip()
clock.tick(300)
pygame.quit()
另一个了解pygame和pygame rects的好地方是Al Sweigarts free online book教python。 Chapter 2教授rects。
编辑:
最后一段代码并没有真正起作用,所以这里有一些新的和改进的代码:
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, 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))
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
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)
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 circlerect.x == 685:
circlerect.x = 15
if circlerect.collidelist((bulletrect1, bulletrect2, bulletrect3)) == 0:
screen.fill(BLACK)
font = pygame.font.SysFont('Calibri',40,True,False)
text = font.render("GAME OVER",True,RED)
screen.blit(text,[0,0])
pygame.display.update()
pygame.time.delay(3000)
resetpositions()
pygame.display.flip()
clock.tick(300)
我改变了所以现在它在屏幕上显示GAME OVER时调用pygame.display.update()。它也将等待三秒钟,因此GAME OVER不会立即消失。此外,它现在实际上工作。访问这两个网站,以清楚地了解pygame.rect.Rect的用途。它有很大的帮助。