将图像转换为矩形

时间:2013-09-16 11:26:01

标签: python pygame collision rect

这个脚本会产生一个跟随玩家的外星人,我只想知道如何让外星人(nPc)和玩家(mouse_c)成为一个矩形,所以我让外星人在成像者重叠时杀死玩家。任何帮助都会有所帮助

感谢

import pygame, sys, random, time, math
from pygame.locals import *
pygame.init()

bifl = 'screeing.jpg'
milf = 'character.png'
alien = 'alien_1.png'

screen = pygame.display.set_mode((640, 480))
background = pygame.image.load(bifl).convert()
mouse_c = pygame.image.load(milf).convert_alpha()
nPc = pygame.image.load(alien).convert_alpha()


mouse_c = pygame.Rect((10, 10))
nPc = pygame.Rect((10, 10))

x, y = 0, 0
movex, movey = 0, 0

z, w = random.randint(10, 480), random.randint(10, 640)
movez, movew = 0, 0

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    if event.type == KEYDOWN:
        if event.key == K_w:
            movey = -4
        elif event.key == K_s:
            movey = +4
        elif event.key == K_a:
            movex = -4
        elif event.key == K_d:
            movex = +4

    if event.type == KEYUP:
        if event.key == K_w:
            movey = 0
        elif event.key == K_s:
            movey = 0
        elif event.key == K_a:
            movex = 0
        elif event.key == K_d:
            movex = 0



    if w < x:
        movew =+ 0.4
    if w > x:
        movew =- 0.4
    if z < y:
        movez =+ 0.4
    if z > y:
        movez =- 0.4

    x += movex
    y += movey
    w += movew
    z += movez



    print('charecter pos: ' + str(x) + str(y))
    print('alien pos: ' + str(w) + str(z))
    chpos = x + y
    alpos = w + z
    print(alpos, chpos)


    screen.blit(background, (0, 0))
    screen.blit(mouse_c, (x, y))
    screen.blit(nPc, (w, z))

    pygame.display.update()

1 个答案:

答案 0 :(得分:1)

你可以使用Pygame Sprites

mouse_c = pygame.sprite.Sprite()
mouse_c.image = pygame.image.load(milf).convert_alpha()
mouse_c.rect = mouse_c.image.get_rect()
mouse_c.rect.move_ip(10,10)

nPc = pygame.sprite.Sprite()
nPc.image = pygame.image.load(milf).convert_alpha()
nPc.rect = nPc.image.get_rect()
nPc.rect.move_ip(10,10)

然后blit:

screen.blit(mouse_c.image, mouse_c.rect.topleft)
screen.blit(nPc.image, nPc.rect.topleft)