Pygame - 射击子弹

时间:2014-11-17 01:14:25

标签: python python-3.x pygame 2d

我的代码有问题,我开始制作2D坦克游戏,问题是来自坦克位置的射击子弹..这是我的代码,检查出来,我试图计算它像3-4个小时..我希望有人知道该怎么做,谢谢!:)

顺便说一句,抱歉令人毛骨悚然的代码,我是Pygame的新手:)

import pygame

pygame.init()

#---WINDOW----
display_width = 1000
display_height = 600

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('powpypow')

black = (0,0,0)
white = (255,255,255)
red = (255,0,0)




background = pygame.image.load('background.png')
tank1 = pygame.image.load('tank1.png')
tankfire = pygame.image.load('tankfire.png')
shot = pygame.image.load('shot.png')

clock = pygame.time.Clock()

def tank(x,y):
    gameDisplay.blit(tank1, (x,y))

x = (display_width * 0.10) 
y = (display_height * 0.58)

x_change = 0

tank_width = 73 


#---GAME LOOP----

gameExit = False


while not gameExit:

    for event in pygame.event.get(): 
        if event.type == pygame.QUIT:
            gameExit = True

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a:
                x_change = -10
            elif event.key == pygame.K_d:
                x_change = 10    
            elif event.key == pygame.K_SPACE:
                tank1 = pygame.image.load('tankfire.png')


        if event.type == pygame.KEYUP:
            if event.key == pygame.K_a or event.key == pygame.K_d or event.key == pygame.K_SPACE:
                tank1 = pygame.image.load('tank1.png')
                x_change = 0 

    if x >= display_width - tank_width or x <= 0:
        x = 0
    if x > display_width / 2:
        x = 0 

    x += x_change
    gameDisplay.blit(background, (0,0))
    tank(x,y)

    pygame.display.update()
    clock.tick(60)

pygame.quit()
quit()

1 个答案:

答案 0 :(得分:2)

如果您想要渲染项目符号,则需要在它们在屏幕上移动时跟踪它们。这将需要某种列表或一组子弹,您可以一次一个地迭代和绘制。

我建议您查看一个教程,以帮助您切换到在组中使用Sprite,而不是单独将每个图像blit到屏幕上。您会发现跟踪每个对象并调整其位置要容易得多。

Pygame网站上的Chimp教程非常好:https://www.pygame.org/docs/tut/chimp/ChimpLineByLine.html