python 3.x - How do I get the screen to erase then draw something new in pygame? -


i have code uses python , pygame when conditions met supposed erase screen , print words "game over" in middle. when conditions met piece collides player passes right through , not anything. how make screen erases , words print?

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:     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 , bullet_x == circle_x or bullet_y2 == 600 , bullet_x2 == circle_x or bullet_y3 == 600 , 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() 

make variable circle_y , set 600 (circle_y = 600), right below circle_x = 350. change line 50 to:

if (abs(bullet_y - circle_y) < 35 , abs(bullet_x - circle_x) < 35) or (abs(bullet_y2 - circle_y) < 35 , abs(bullet_x2 - circle_x) < 35) or abs(bullet_y3 - circle_y) < 35 , abs(bullet_x3 - circle_x) < 35: 

this fix collision problem. may suggest refactor exepression

do_circle_and_bullet_hit = abs(bullet_y - circle_y) < 35 , abs(bullet_x - circle_x) < 35 do_circle_and_bullet2_hit = abs(bullet_y2 - circle_y) < 35 , abs(bullet_x2 - circle_x) < 35 do_circle_and_bullet3_hit = abs(bullet_y3 - circle_y) < 35 , abs(bullet_x3 - circle_x) < 35 if do_circle_and_bullet_hit or do_circle_and_bullet2_hit or do_circle_and_bullet3_hit: 

the idea check distance between circle , each of bullets. if distance below 35 hit.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -