Python v2.7版。皮加梅。像素之间的大小差异图像加载以及图片.fi

2024-04-27 00:01:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在pythonv2.7上制作一个简单的游戏(类似于danmaku射击游戏)

我对这部分有意见

import os
from pygame import *

MOVE_SPEED = 3
WIDTH = 10
HEIGHT = 10
COLOR = "#F52525"

class Player(sprite.Sprite):
    def __init__(self, x, y):
        sprite.Sprite.__init__(self)
        self.xvel = 0
        self.yvel = 0
        self.startX = x
        self.startY = y
        self.image = Surface((WIDTH,HEIGHT))
        #self.image.fill(Color(COLOR))
        self.image = image.load("images/player.png")
        self.rect = Rect(x, y, WIDTH, HEIGHT)

““播放器.png“大小是40x60,但我需要在实际大小10x10矩形的图像中心,这将与游戏逻辑(与选项显示或不)。你知道吗


Tags: fromimageimportself游戏pnginitos
3条回答

您需要在blit中使用rect参数:

screen.blit(image, dest, (x_crop, y_crop, width, height))

如果您想使用中心,您应该定义图像函数blit(screen, dest)

screen.blit(self.image, dest, self.rect)

您可能知道每个精灵由一个image(绘制在曲面上)和一个rect对象组成(设置image的位置和碰撞检测区域)。你知道吗

您可以在player类中创建一个新的rect对象

self.collideRect =  pygame.rect.Rect((0, 0), (10, 10))

…并将其位置设置为

self.collideRect.center = self.rect.center

每次更改player的位置时,也必须调用此行,因此self.collideRectrect对象会随着屏幕上的player移动“。你知道吗

要测试点(例如鼠标坐标)是否在self.collideRect内,请调用

if self.collideRect.collidepoint(x, y) == True:
    print("You clicked on me!")

我希望这有帮助:)

所以我想你想缩小图像的比例? 没有

编辑

您将需要偏移图形和冲突rect

       (x,y)          drawing
|\     | 
| \   |     (x+15,y+25)  rect
|     |       (10,10)
|      | 
             (40,60)

所以基本上你希望碰撞发生在图像内部。 https://www.pygame.org/docs/ref/sprite.html#pygame.sprite.collide_rect

def interact(something, player):
   # note both Sprites must have a rect attributes
   return pygame.sprite.collide_rect(something,player)

相关问题 更多 >