我正在用pygame做一个游戏。我使用“激光”作为玩家发射的投射物。我目前使用的系统是,当特定条件为真时,我将激光的颜色更改为红色。但是,这不适用于collidirect。当“激光”与实体碰撞时,Collidirect会立即“激活”。激光是在我的代码的最后一行绘制的。
代码如下:
import pygame
import sys
# Creating a loop to keep program running
while True:
# --- Event Processing and controls
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
spaceship_x_change = 10
elif event.key == pygame.K_LEFT:
spaceship_x_change = -10
elif event.key == pygame.K_UP:
red = (255, 0, 0)
elif event.type == pygame.KEYUP:
red = (0, 0, 0)
spaceship_x_change = 0
spaceship_x += spaceship_x_change
# Preventing the ship from going off the screen
if spaceship_x > display_width - 140:
spaceship_x -= 10
if spaceship_x < 1:
spaceship_x += 10
pygame.draw.rect(game_display, red, [spaceship_x + 69, 70, 4, 310])发布于 2021-01-11 22:22:37
这只是一个理论上的例子,因为代码片段本身并不起作用,所以我将向你展示一个如何实现移除对象的例子。
因此,如果你想在飞船到达屏幕边缘时将其移除,你可以按照下面的思路做一些事情。
import pygame
import sys
# Creating a loop to keep program running
draw_spaceship = True
while True:
# --- Event Processing and controls
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
spaceship_x_change = 10
elif event.key == pygame.K_LEFT:
spaceship_x_change = -10
elif event.key == pygame.K_UP:
red = (255, 0, 0)
elif event.type == pygame.KEYUP:
red = (0, 0, 0)
spaceship_x_change = 0
spaceship_x += spaceship_x_change
# Preventing the ship from going off the screen
if spaceship_x > display_width - 140:
draw_spaceship = False
elif spaceship_x < 1:
draw_spaceship = False
else:
# spaceship is within the screen bounds
draw_spaceship = True
if draw_spaceship:
# this doesnt actually draw the spaceship,
# but you would put the equivalent code here that did that.
pygame.draw.rect(game_display, red, [spaceship_x + 69, 70, 4, 310])所以你需要一个标志或一些你想要检查的条件,它将决定你是否绘制一个对象。
https://stackoverflow.com/questions/65667898
复制相似问题