我如何使用边缘检测

时间:2015-04-09 03:30:54

标签: python pygame

所以我正在尝试为边缘编写代码,但我似乎无法弄明白。

因此,当我的船到达边缘时,它假设转向180度。对不起,我只学习python大约一个月左右,它是我的第一语言

import pygame,sys, random
pygame.init()

class Ship(pygame.sprite.Sprite):
    movepersec = 0
    dx = 0
    dy = 0
    direction = ""
    imgarray = {}

    def __init__(self, imgarr, rect, speed, xpos, ypos, direct):
        pygame.sprite.Sprite.__init__(self)

        self.imgarray = imgarr
        self.rect = rect
        self.movepersec = speed
        self.dx = xpos
        self.dy = ypos
        self.rect.centerx = xpos
        self.rect.centery = ypos
        self.direction = direct
        self.image = self.imgarray[self.direction]

    def update(self,secs):
        #set the direction and calculate number of pixels to move
        movePix = self.movepersec*secs
        if self.direction == 'N': self.dy -= movePix
        if self.direction == 'S': self.dy += movePix
        if self.direction == 'E': self.dx += movePix
        if self.direction == 'W': self.dx -= movePix



        #move the image react
        self.rect.centerx = self.dx
        self.rect.centery = self.dy

        #set the image
        self.image = self.imgarray[self.direction]

    def setDirection(self, direct):
        self.direction = direct

    def setSpeed(self,speed):
        self.movepersec = speed

        ##Main Program##
        #setup data
background = pygame.image.load('sea.jpg')
#size = background.get_size()
width = int(1023)
height = int(682)
screen = pygame.display.set_mode((width, height))
clock = pygame.time.Clock()
imgarray = {}
imgarray['N']=pygame.image.load('shipNorth.png')
imgarray['S']=pygame.image.load('shipSouth.png')
imgarray['E']=pygame.image.load('shipEast.png')
imgarray['W']=pygame.image.load('shipWest.png')
imgrect = imgarray ['N'].get_rect()
shipHw = 172
shipHh = 108
shipVw = 108
shipVh = 172

movepersec = 150
keydownflag = False
allSpriteGroup = pygame.sprite.Group()

    #make a ship object and add it to the group
shipX = Ship(imgarray,imgrect,movepersec,150,150,'E')
allSpriteGroup.add(shipX)


    #display the background
screen.blit(background,(0,0))

#start game loop
while True:
    secs = clock.tick(30)/1000.0

    #get events
    for event in pygame.event.get():
        if event.type==pygame.QUIT: sys.exit(0)

        if event.type==pygame.KEYDOWN:
            keydownflag = True

            if event.key==pygame.K_LEFT:
                    shipX.setDirection('W')
            if event.key==pygame.K_RIGHT:
                    shipX.setDirection('E')
            if event.key==pygame.K_UP:
                    shipX.setDirection('N')
            if event.key==pygame.K_DOWN:
                    shipX.setDirection('S')
        if event.type==pygame.KEYUP:
            keydownflag = False


    if keydownflag:
        #update sprites
        allSpriteGroup.update(secs)

    #clear the sprite backgrounds
    allSpriteGroup.clear(screen,background)

        #draw sprites
    allSpriteGroup.draw(screen)

        #flip display
    pygame.display.flip()

1 个答案:

答案 0 :(得分:0)

一种简单的方法是简单地使用contains()来检查你的船的矩形是否仍在屏幕的矩形中。如果不是,你知道你必须转身。

以下是一个例子:

def update(self, secs, screen_rect):

    #set the direction and calculate number of pixels to move
    movePix = self.movepersec*secs

    def f():
        if self.direction == 'N': return (0, -movePix)
        if self.direction == 'S': return (0, movePix)
        if self.direction == 'W': return (-movePix, 0)
        if self.direction == 'E': return (movePix, 0)

    #check if we would leave the screen
    if not screen_rect.contains(self.rect.move(f())):
        # we're leaving the screen, so change direction
        change_dirs = {'N': 'S', 'S': 'N', 'E': 'W', 'W': 'E'}
        self.direction = change_dirs(self.direction)

    #move the image react
    self.rect.move_ip(f())

    #set the image
    self.image = self.imgarray[self.direction]

调用update时,您也必须通过屏幕矩形。

allSpriteGroup.update(secs, screen.get_rect())