我试图用pygame.draw.rect()绘制一个形状,当我按下一个键但是当另一个键或事件发生时形状不会停留,并且形状移动到船移动的地方但我只想移动当我拍摄它并留在那个地方,并且在我释放那个键之后形状不会停留在表面上
import pygame
pygame.init()
display_width = 800
display_hight = 600
colors = {"black": (0, 0, 0), "white": (255, 255, 255), "green": (0, 255, 0), "red": (255, 0, 0), "blue": (0, 0, 255)}
ship_width = 75
ship_hight = 72
gameDisplay = pygame.display.set_mode((display_width, display_hight))
pygame.display.set_caption('test')
clock = pygame.time.Clock()
shipimg = pygame.image.load('D:\\index.png')
def ship(x, y):
gameDisplay.blit(shipimg, (x, y))
def Game_Loop():
x = (display_width * 0.41)
y = (display_hight * 0.8)
x_change = 0
fire_start_y = y - ship_hight
fire_start_x = x + (ship_width / 2)
GameExit = False
while not GameExit:
if fire_start_y < 0:
fire_start_y = y - 30
for event in pygame.event.get():
if event.type == pygame.QUIT:
GameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
if event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
x += x_change
gameDisplay.fill(colors["white"])
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_x:
pygame.draw.rect(gameDisplay, colors["green"], [fire_start_x, fire_start_y, 2, 10])
ship(x, y)
if x <= 0 or x >= display_width - ship_width:
x -= x_change
fire_start_y -= 5
fire_start_x = x + (ship_width / 2)
pygame.display.flip()
clock.tick(60)
Game_Loop()
pygame.quit()
我尝试使用此代码来管理消失的问题,但它使情况变得更糟 因为我按了一次键后它就会被砍掉
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_x:
pygame.draw.rect(gameDisplay, colors["green"], [fire_start_x, fire_start_y, 2, 10])
fire_start_x = x + (ship_width / 2)
elif event.type == pygame.KEYUP:
if event.key == pygame.K_x:
pygame.draw.rect(gameDisplay, colors["green"], [fire_start_x, fire_start_y, 2, 10])
fire_start_x = x + (ship_width / 2)
答案 0 :(得分:0)
如果要在按住按钮时发射许多项目符号,请使用此代码。
if pygame.key.get_pressed[pygame.K_x] != 0:
# Add code to add Rect objects to a list.
# Add in code to add the Rect objects in intervals.
在上面的代码中,您必须添加代码,以便在游戏循环的每次交互期间不添加rect对象。如果你没有,那么每帧可以发射60发子弹或者每帧发射1发子弹,这可能是也可能不是太多。
如果您想为每个keydown事件触发一个子弹,请使用此代码。
if pygame.event.type == pygame.KEYDOWN:
if pygame.event.key == pygame.K_x:
# Add code to add Rect objects to a list.
在渲染游戏循环期间,您需要渲染rect对象。
for bullet in bullets:
# Blit the bullet to the screen
# Increment/Decrement the x/y values so they move.
当然,您需要处理以从此列表中删除项目符号。您还需要在碰撞检测中进行编码。
如果你解释得更多,更详细,我会补充答案。 希望这有帮助!
编辑:好的,您需要使用第一个代码块来按下&#34;按下&#34;射击子弹。将这些rect对象添加到列表中。if pygame.key.get_pressed[pygame.K_x] != 0:
bullets.append(pygame.Rect(x, y, 10, 3))
# Add in a time delay.
确保您创建项目符号列表。 x和y值将是您希望子弹相对于船舶发射的任何地方。
现在渲染它们。
for bullet in bullets:
window.blit(bullet, [bullet.x, bullet.y])
bullet.x += 3 # This will make the bullet go EAST.
移除掉掉屏幕的子弹。
for bullet in bullets:
if bullet.x < 0 or bullet.x + bullet.width > window_width:
bullets.remove(bullet)
# Add in some collision detection with other ships here.
希望这有帮助!
答案 1 :(得分:0)
谢谢你们,但我找到了我正在寻找的答案How to create bullets in pygame?
创建子弹列表,然后使用追加方法为我按下x键时弹出的每个子弹添加方向,如果它们不在我的游戏界面,则将其从列表中删除
这是我的代码现在的样子
bullets = []
bullet_width = 2
bullet_hight = 10
def Game_Loop():
x = (display_width * 0.41)
y = (display_hight * 0.8)
x_change = 0
bullet_y = y - (ship_hight/2)
GameExit = False
while not GameExit:
bullet_x = x + (ship_width / 2)
for event in pygame.event.get():
if event.type == pygame.QUIT:
GameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
if event.key == pygame.K_RIGHT:
x_change = 5
if event.key == pygame.K_x:
bullets.append([bullet_x,bullet_y])
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
x += x_change
gameDisplay.fill(colors["white"])
for bullet in bullets:
pygame.draw.rect(gameDisplay, colors["green"], (bullet[0], bullet[1], bullet_width, bullet_hight))
ship(x, y)
if x <= 0 or x >= display_width - ship_width:
x -= x_change
for bullet in bullets:
bullet[1] -= 5
if bullet[1] < 0:
bullets.remove(bullet)
pygame.display.flip()
clock.tick(60)