Так что я пытаюсь написать код для ребер, но я просто не могу понять, как это понять.Как использовать обнаружение кромок
Итак, когда мой корабль добирается до края, его можно поворачивать на 180 градусов. К сожалению я только учусь питоном в течение месяца или около того, и его моего первого языка
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()