pygame surface.blit()方法的问题

潘女士 2018-08-03 10:13:49
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,飞机就不能移动到最右边了,这是为什么呢。。。
...全文
2424 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Andy(chao) 2020-05-28
  • 打赏
  • 举报
回复
image原本就带有参数,是不是原本的image不对称导致不能移动到最右边。
print_5 2020-03-02
  • 打赏
  • 举报
回复
经过测试发现:.screen.blit(self.image,(10,10),self.rect2)
图片 x坐标y坐标矩形
for_grasp 2018-08-04
  • 打赏
  • 举报
回复 1
你好!
self.rect 引用了 self.image图片的长和宽,即self.rect.width == self.image.width(矩形大小等于图片初始化的),
对self.rect.width改变之后,这个绘图矩形也变了,可能是由于图像是从右边开始缩减或增大的,而它的边界没有在界面上体现出来。

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧