如何在每次碰撞后显示正确数量的图像?
How to display the correct amount of images after each collision?
我试图在每次检测到碰撞时显示正确数量的气球(图像)。 请注意,碰撞效果非常好,但我无法弄清楚如何显示正确数量的气球。
碰撞发生在 7 个气球和一把枪之间。当气球被枪击中时,会出现一个较少的气球。应该发生的事情是当第一个背景上的 7 个气球中的一个被击中时,一个气球应该消失,并且一个新的背景应该出现并带有相同的 7 个气球。然后,在新的背景上,如果气球被击中,气球应该消失并回到第一个背景。然后再次在第一个背景上,这次应该出现 6 个气球,当一个气球被击中时,一个应该消失,然后新的背景也出现 6 个气球。这应该继续下去,直到所有的气球都消失。
所以气球的数量应该是7,7 6,6 5,5 4,4 3,3 2,2 1,1;每对中的第一个数字是第一个背景,第二个是新背景。
这是我的代码:
import pygame as pg
import random as r
import sys
pg.init()
bg = pg.image.load('bg.jpg')# Background Image #
bg = pg.transform.scale(bg, (688,387))
new_bg = pg.image.load('new_bg.jpg')
new_bg = pg.transform.scale(new_bg, (688,387))
radius = 30
diameter = 2 * radius
num_balloons = 7
bullets_colors_ls = []
iterator = -1
def create_balloons():
global balloon_list
global colors
for i in range(num_balloons):
while True:
candidate = r.randint(0, 500)
if all(abs(candidate-x) >= diameter for x in balloon_list):
break
balloon_list.append(candidate)
def draw_balloons(y):
for i in range(num_balloons):
screen.blit(colors[i], (balloon_list[i] , y-50))
def check_collisions(x, y):
global hit_var
global hit
global score
global scoretext
global bg_bool
for i in range(num_balloons):
gun_rect = gun.get_rect(topleft = (x,y))
gun_mask = pg.mask.from_surface(gun)
balloon_rect = colors[i].get_rect(topleft = (balloon_list[i], y-100))
balloon_mask = pg.mask.from_surface(colors[i])
offset = (balloon_rect.x - gun_rect.x), (balloon_rect.y - gun_rect.y)
if gun_mask.overlap(balloon_mask, offset):
bg_bool = True
hit_var = i
print(f'hit balloon: {i}')
colors[i].fill((0,0,0,0))
screen.fill((0,0,0,0))
# Vars #
x = 0
y = 250
velocity = 5
score = 0
bg_bool = False
clock = pg.time.Clock()
screen = pg.display.set_mode((688 ,387)) # Size of the screen #
caption = pg.display.set_caption("Remember") # Title of the window #
balloon_list = []
b1 = pg.image.load('balloons/1.png').convert_alpha()
b1 = pg.transform.scale(b1, (63,131))
b2 = pg.image.load('balloons/2.png').convert_alpha()
b2 = pg.transform.scale(b2, (63,131))
b3 = pg.image.load('balloons/3.png').convert_alpha()
b3 = pg.transform.scale(b3, (63,131))
b4 = pg.image.load('balloons/4.png').convert_alpha()
b4 = pg.transform.scale(b4, (63,131))
b5 = pg.image.load('balloons/5.png').convert_alpha()
b5 = pg.transform.scale(b5, (63,131))
b6 = pg.image.load('balloons/6.png').convert_alpha()
b6 = pg.transform.scale( b6, (63,131))
b7 = pg.image.load('balloons/7.png').convert_alpha()
b7 = pg.transform.scale(b7, (63,131))
colors = [b1, b2, b3, b4, b5, b6, b7]
gun = pg.image.load('game-gun.png').convert_alpha()
gun = pg.transform.scale(gun, (150,150))
create_balloons()
pg.display.flip() # Updating #
running = True # Game loop bool #
while running: # Game loop #
clock.tick(60)
#Background switching code#
if bg_bool == False:
screen.blit(bg, (0, 0))
elif bg_bool == True:
screen.blit(new_bg, (0,0))
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
sys.exit()
if event.type == pg.KEYDOWN:
if event.key == pg.K_ESCAPE:
running = False
if event.key == pg.K_SPACE:
check_collisions(x, y)
draw_balloons(y)
keys = pg.key.get_pressed()
x += keys[pg.K_RIGHT] - keys[pg.K_LEFT] * velocity
x -= keys[pg.K_LEFT] - keys[pg.K_RIGHT] * velocity
screen.blit(gun, (x, y))
pg.display.update()
您可以在这里下载所有图片:Download images on REPL
如何在每次切换背景时正确显示正确数量的气球?
感谢任何帮助,
谢谢
答案隐藏在问题中:
[...] the number of balloons should be like 7,7 6,6 5,5 4,4 3,3 2,2 1,1
创建一个列表,其中包含气球的数量和一个表示列表中当前索引的变量:
num_balloon_list = [7, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, 0]
balloon_list_index = 0
num_balloons = num_balloon_list[balloon_list_index]
如果检测到碰撞,递增 balloon_list_index
并从列表中获取新数字:
def check_collisions(x, y):
global hit_var
global hit
global score
global scoretext
global bg_bool
global num_balloons, balloon_list_index
for i in range(num_balloons):
gun_rect = gun.get_rect(topleft = (x,y))
gun_mask = pg.mask.from_surface(gun)
balloon_rect = colors[i].get_rect(topleft = (balloon_list[i], y-100))
balloon_mask = pg.mask.from_surface(colors[i])
offset = (balloon_rect.x - gun_rect.x), (balloon_rect.y - gun_rect.y)
if gun_mask.overlap(balloon_mask, offset):
bg_bool = not bg_bool
balloon_list_index += 1
if balloon_list_index < len(num_balloon_list):
num_balloons = num_balloon_list[balloon_list_index]
hit_var = i
print(f'hit balloon: {i}')
break
如果num_balloons
为0,则游戏结束。
我试图在每次检测到碰撞时显示正确数量的气球(图像)。 请注意,碰撞效果非常好,但我无法弄清楚如何显示正确数量的气球。
碰撞发生在 7 个气球和一把枪之间。当气球被枪击中时,会出现一个较少的气球。应该发生的事情是当第一个背景上的 7 个气球中的一个被击中时,一个气球应该消失,并且一个新的背景应该出现并带有相同的 7 个气球。然后,在新的背景上,如果气球被击中,气球应该消失并回到第一个背景。然后再次在第一个背景上,这次应该出现 6 个气球,当一个气球被击中时,一个应该消失,然后新的背景也出现 6 个气球。这应该继续下去,直到所有的气球都消失。
所以气球的数量应该是7,7 6,6 5,5 4,4 3,3 2,2 1,1;每对中的第一个数字是第一个背景,第二个是新背景。
这是我的代码:
import pygame as pg
import random as r
import sys
pg.init()
bg = pg.image.load('bg.jpg')# Background Image #
bg = pg.transform.scale(bg, (688,387))
new_bg = pg.image.load('new_bg.jpg')
new_bg = pg.transform.scale(new_bg, (688,387))
radius = 30
diameter = 2 * radius
num_balloons = 7
bullets_colors_ls = []
iterator = -1
def create_balloons():
global balloon_list
global colors
for i in range(num_balloons):
while True:
candidate = r.randint(0, 500)
if all(abs(candidate-x) >= diameter for x in balloon_list):
break
balloon_list.append(candidate)
def draw_balloons(y):
for i in range(num_balloons):
screen.blit(colors[i], (balloon_list[i] , y-50))
def check_collisions(x, y):
global hit_var
global hit
global score
global scoretext
global bg_bool
for i in range(num_balloons):
gun_rect = gun.get_rect(topleft = (x,y))
gun_mask = pg.mask.from_surface(gun)
balloon_rect = colors[i].get_rect(topleft = (balloon_list[i], y-100))
balloon_mask = pg.mask.from_surface(colors[i])
offset = (balloon_rect.x - gun_rect.x), (balloon_rect.y - gun_rect.y)
if gun_mask.overlap(balloon_mask, offset):
bg_bool = True
hit_var = i
print(f'hit balloon: {i}')
colors[i].fill((0,0,0,0))
screen.fill((0,0,0,0))
# Vars #
x = 0
y = 250
velocity = 5
score = 0
bg_bool = False
clock = pg.time.Clock()
screen = pg.display.set_mode((688 ,387)) # Size of the screen #
caption = pg.display.set_caption("Remember") # Title of the window #
balloon_list = []
b1 = pg.image.load('balloons/1.png').convert_alpha()
b1 = pg.transform.scale(b1, (63,131))
b2 = pg.image.load('balloons/2.png').convert_alpha()
b2 = pg.transform.scale(b2, (63,131))
b3 = pg.image.load('balloons/3.png').convert_alpha()
b3 = pg.transform.scale(b3, (63,131))
b4 = pg.image.load('balloons/4.png').convert_alpha()
b4 = pg.transform.scale(b4, (63,131))
b5 = pg.image.load('balloons/5.png').convert_alpha()
b5 = pg.transform.scale(b5, (63,131))
b6 = pg.image.load('balloons/6.png').convert_alpha()
b6 = pg.transform.scale( b6, (63,131))
b7 = pg.image.load('balloons/7.png').convert_alpha()
b7 = pg.transform.scale(b7, (63,131))
colors = [b1, b2, b3, b4, b5, b6, b7]
gun = pg.image.load('game-gun.png').convert_alpha()
gun = pg.transform.scale(gun, (150,150))
create_balloons()
pg.display.flip() # Updating #
running = True # Game loop bool #
while running: # Game loop #
clock.tick(60)
#Background switching code#
if bg_bool == False:
screen.blit(bg, (0, 0))
elif bg_bool == True:
screen.blit(new_bg, (0,0))
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
sys.exit()
if event.type == pg.KEYDOWN:
if event.key == pg.K_ESCAPE:
running = False
if event.key == pg.K_SPACE:
check_collisions(x, y)
draw_balloons(y)
keys = pg.key.get_pressed()
x += keys[pg.K_RIGHT] - keys[pg.K_LEFT] * velocity
x -= keys[pg.K_LEFT] - keys[pg.K_RIGHT] * velocity
screen.blit(gun, (x, y))
pg.display.update()
您可以在这里下载所有图片:Download images on REPL
如何在每次切换背景时正确显示正确数量的气球?
感谢任何帮助, 谢谢
答案隐藏在问题中:
[...] the number of balloons should be like 7,7 6,6 5,5 4,4 3,3 2,2 1,1
创建一个列表,其中包含气球的数量和一个表示列表中当前索引的变量:
num_balloon_list = [7, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, 0]
balloon_list_index = 0
num_balloons = num_balloon_list[balloon_list_index]
如果检测到碰撞,递增 balloon_list_index
并从列表中获取新数字:
def check_collisions(x, y):
global hit_var
global hit
global score
global scoretext
global bg_bool
global num_balloons, balloon_list_index
for i in range(num_balloons):
gun_rect = gun.get_rect(topleft = (x,y))
gun_mask = pg.mask.from_surface(gun)
balloon_rect = colors[i].get_rect(topleft = (balloon_list[i], y-100))
balloon_mask = pg.mask.from_surface(colors[i])
offset = (balloon_rect.x - gun_rect.x), (balloon_rect.y - gun_rect.y)
if gun_mask.overlap(balloon_mask, offset):
bg_bool = not bg_bool
balloon_list_index += 1
if balloon_list_index < len(num_balloon_list):
num_balloons = num_balloon_list[balloon_list_index]
hit_var = i
print(f'hit balloon: {i}')
break
如果num_balloons
为0,则游戏结束。