如果检测到一定数量的碰撞(或击中气球),我将尝试仅切换背景。碰撞发生在枪支和屏幕上的 7 个气球之间。
注意:碰撞效果非常好。
目前,会显示 7 个气球,每次我击中气球时它都会切换背景。应该发生的是,当您在第一个背景上击中 7 个气球中的 1 个时,它会更改为您在显示的 7 个气球中再次击中 1 个气球的第二个背景。当您在第二个背景上点击气球时,它会变回第一个。然后(回到第一个背景)你击中了 2 个气球(共 7 个),然后它变为第二个背景,你再次击中了 2 个气球,依此类推。 所以命中数应该是 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7; (总共显示 7 个气球),其中每对中的第一个数字是第一个背景中的命中数,第二个是新背景中的命中数。
这是改变背景的原因:
for balloon_list_index in num_balloon_list:
balloon_list_index += 1
bg_bool = not bg_bool
这是所需命中数的列表:
num_balloon_list = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7]
这是我的完整代码:
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
num_balloon_list = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7]
balloon_list_index = 0
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
global num_balloon_list, 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):
hit = True
for balloon_list_index in num_balloon_list:
balloon_list_index += 1
bg_bool = not bg_bool
hit_var = i
score += 2
scoretext = myfont.render("SCORE: " + str(score), 1, (0, 0, 0))
print(f'hit balloon: {i}')
break
# Vars #
x = 0
y = 250
velocity = 5
score = 0
hit = False
bg_bool = False
testvar1 = True
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)
if bg_bool == False:
screen.blit(bg, (0, 0))
elif bg_bool == True:
screen.blit(new_bg, (0, 0))
screen.blit(scoretext, (5, 10))
if hit == True:
r.shuffle(balloon_list)
hit = False
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 Replit
balloons/
1.png
2.png
3.png
4.png
5.png
6.png
7.png
bg.jpg
game-gun.png
new_bg.jpg
如何在一定次数的碰撞(击中气球)后更改背景?
附言I have asked a question that is a similar concept有一个很好的答案,但我没能在这个场景中实现它。
最佳答案
首先,您需要一个全局变量来跟踪点击次数。
num_balloons = 7
num_hits = 0 # Add this
当被击中时,更新气球的数量和被击中的次数。
然后,比较需要的命中次数,如果游戏还没有结束:
- 恢复气球的数量,
- 重置点击次数,
- 增加气球列表索引以获得下一个所需的命中数,并且
- 🎉 改变背景 🎉 .
def check_collisions(x, y):
# ...
global num_balloons, num_hits # Add this
for i in range(num_balloons):
# ...
if gun_mask.overlap(balloon_mask, offset):
hit = True
# for balloon_list_index in num_balloon_list: # Remove this
num_balloons -= 1 # Add this
num_hits += 1 # Add this
num_hits_needed = num_balloon_list[balloon_list_index] # Add this
game_end = balloon_list_index == len(num_balloon_list) - 1 # Add this
if num_hits == num_hits_needed and not game_end: # Add this
num_balloons += num_hits # Restore balloons # Add this
num_hits = 0 # and then reset hits # Add this
balloon_list_index += 1
bg_bool = not bg_bool
# ...
关于python - 在pygame中发生一定数量的碰撞后更改背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69340567/