简体   繁体   English

Pygame rect 不会更新

[英]Pygame rect will not update

I am building a game in pygame where there is a red target that moves up and down on the right side of the screen and a ship on the left side of the screen the moves up and down that fires bullets (which is just a blue rectangle) at the target.我正在 pygame 中构建一个游戏,其中有一个红色目标在屏幕右侧上下移动,屏幕左侧有一艘船上下移动发射子弹(这只是一个蓝色矩形) ) 目标。

My ship, and my target start in the center of each side of the screen and are properly moving.我的船和我的目标从屏幕两侧的中心开始,并且正在正确移动。 The problem that I have is when I 'fire' a bullet the bullet is getting drawn at the ship's original position, not where the ship has moved to on the screen.我遇到的问题是,当我“发射”一颗子弹时,子弹会被拉到飞船的原始位置,而不是飞船移动到屏幕上的位置。

I have the bullet's rect set to the ship image's rect outside of my while loop, but I would think that it would get updated as my ship moves up and down on the screen.我将子弹的矩形设置为 while 循环之外的船舶图像的矩形,但我认为它会随着我的船在屏幕上上下移动而更新。

import pygame
import pygame.sprite
import sys

screen_width = 1200
screen_height = 800
screen = pygame.display.set_mode((screen_width, screen_height))
screen_rect = screen.get_rect()

image = pygame.image.load('ship.bmp')
image_rect = image.get_rect()
image_rect.midleft = screen_rect.midleft

target_rect = pygame.Rect(400, 0, 100, 100)
target_color = (255, 0, 0)
target_rect.midright = screen_rect.midright
target_direction = 1

bullet_rect = pygame.Rect(0, 0, 15, 3)
bullet_rect.midright = image_rect.midright
bullet_color = (0, 0, 255)
fire_bullet = False


while True:

    screen.fill((0, 255, 0))

    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_q:
                sys.exit()

            # Move the ship up and down 
            elif event.key == pygame.K_UP:
                image_rect.y -= 45
            elif event.key == pygame.K_DOWN:
                image_rect.y += 45                
            # Active bullet fired 
            elif event.key == pygame.K_SPACE:
                fire_bullet = True

        elif event.type == pygame.QUIT:
                sys.exit()

    # Move the bullet across the screen if fired
    if fire_bullet:
        screen.fill(bullet_color, bullet_rect)
        bullet_rect.x += 1

    # Move the Target up and down
    target_rect.y += target_direction
    if target_rect.bottom >= screen_height:
        target_direction *= -1
    elif target_rect.top <= 0:
        target_direction *= -1


    screen.fill(target_color, target_rect)
    screen.blit(image, image_rect)
    pygame.display.flip()

I have the bullet's rect set to the ship image's rect outside of my while loop, but I would think that it would get updated as my ship moves up and down on the screen.我将子弹的矩形设置为 while 循环之外的船舶图像的矩形,但我认为它会随着我的船在屏幕上上下移动而更新。

That's not it.不是这个。 You just set the position of the rect, there is not automatic update.你只是设置矩形的位置,没有自动更新。 You have to write the code to keep them in sync.您必须编写代码以使它们保持同步。
But even simpler, in your case you could create the bullet rect when you shot.但更简单的是,在您的情况下,您可以在射击时创建子弹矩形。 Move the bullet creation in the loop like this:像这样在循环中移动子弹创建:

elif event.key == pygame.K_SPACE:
    bullet_rect = pygame.Rect(0, 0, 15, 3)
    bullet_rect.midright = image_rect.midright
    bullet_color = (0, 0, 255)
    fire_bullet = True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM