Pygame surface.bilt方法的问题 10C

image就是一张图片
self.rect=self.image.get_rect()

self.screen.blit(self.image, self.rect)
众所周知,第二个参数用于指定绘制的位置,所以上面这种写法的第二个参数是rect,实际有4个值,于是我尝试了下面这么写也行
self.screen.blit(self.image, (self.rect.x, self.rect.y))

而经我测试,就算指定四个值也完全没有任何影响
self.screen.blit(self.image, (self.rect.x, self.rect.y, 400, 500))
self.screen.blit(self.image, (self.rect.x, self.rect.y, 400, 600))
绘制结果完全一样,说明后两个值根本没用

我的问题是,在我添加了一行self.rect.width = 100后,发现image在绘制时不能绘制到屏幕最右边的宽度为100的区域,这是为什么呢。。。

源码如下,一个打飞机小游戏
//ship.py
import pygame

class Ship():
def init(self, ai_settings, screen):
"""Initialize the ship, and set its starting position."""
self.screen = screen
self.ai_settings = ai_settings

    # Load the ship image, and get its rect.
    self.image = pygame.image.load('images/ship.bmp')
    self.rect = self.image.get_rect()
    self.screen_rect = screen.get_rect()

    # Start each new ship at the bottom center of the screen.
    self.rect.centerx = self.screen_rect.centerx
    self.rect.bottom = self.screen_rect.bottom

    # Store a decimal value for the ship's center.
    self.center = float(self.rect.centerx)

    # Movement flags.
    self.moving_right = False
    self.moving_left = False

def update(self):
    """Update the ship's position, based on movement flags."""
    # Update the ship's center value, not the rect.
    if self.moving_right and self.rect.right < self.screen_rect.right:
        self.center += self.ai_settings.ship_speed_factor
    if self.moving_left and self.rect.left > 0:
        self.center -= self.ai_settings.ship_speed_factor

    # Update rect object from self.center.
    self.rect.centerx = self.center

def blitme(self):
    """Draw the ship at its current location."""
    self.rect.w = 100
    self.screen.blit(self.image, self.rect)

//game_function.py
import sys
import pygame
from bullet import Bullet
def check_keydown_events(event, ai_settings, screen, ship, bullets):
"""Respond to keypresses."""
if event.key == pygame.K_RIGHT:
ship.moving_right = True
elif event.key == pygame.K_LEFT:
ship.moving_left = True
elif event.key == pygame.K_SPACE:
fire_bullet(ai_settings, screen, ship, bullets)

def check_keyup_events(event, ship):
        """Respond to key releases."""
        if event.key == pygame.K_RIGHT:
                ship.moving_right = False
        elif event.key == pygame.K_LEFT:
                ship.moving_left = False

def check_events(ai_settings, screen, ship, bullets):
        """Respond to keypresses and mouse events."""
        for event in pygame.event.get():
                if event.type == pygame.QUIT:
                        sys.exit()
                elif event.type == pygame.KEYDOWN:
                        check_keydown_events(event, ai_settings, screen, ship, bullets)
                elif event.type == pygame.KEYUP:
                        check_keyup_events(event, ship)

def fire_bullet(ai_settings, screen, ship, bullets):
        """Fire a bullet, if limit not reached yet."""
        # Create a new bullet, add to bullets group.
        if len(bullets) < ai_settings.bullets_allowed:
                new_bullet = Bullet(ai_settings, screen, ship)
                bullets.add(new_bullet)

def update_screen(ai_settings, screen, ship, bullets):
        """Update images on the screen, and flip to the new screen."""
        # Redraw the screen, each pass through the loop.
        screen.fill(ai_settings.bg_color)

        # Redraw all bullets, behind ship and aliens.
        for bullet in bullets.sprites():
                bullet.draw_bullet()
        ship.blitme()

        # Make the most recently drawn screen visible.
        pygame.display.flip()

def update_bullets(bullets):
        """Update position of bullets, and get rid of old bullets."""
        # Update bullet positions.
        bullets.update()

        # Get rid of bullets that have disappeared.
        for bullet in bullets.copy():
                if bullet.rect.bottom <= 0:
                        bullets.remove(bullet)

//主程序
import pygame
from pygame.sprite import Group

from settings import Settings
from ship import Ship
import game_functions as gf

def run_game():
        # Initialize pygame, settings, and screen object.
        pygame.init()
        ai_settings = Settings()
        screen = pygame.display.set_mode(
                (ai_settings.screen_width, ai_settings.screen_height))
        pygame.display.set_caption("Alien Invasion")

        # Set the background color.
        bg_color = (230, 230, 230)

        # Make a ship.
        ship = Ship(ai_settings, screen)
        # Make a group to store bullets in.
        bullets = Group()

        # Start the main loop for the game.
        while True:
                gf.check_events(ai_settings, screen, ship, bullets)
                ship.update()
                gf.update_bullets(bullets)
                gf.update_screen(ai_settings, screen, ship, bullets)

run_game()

添加了self.rect.w = 100,飞机就不能移动到最右边了,这是为什么呢。。。

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!