Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """ chase.py
- Chasing a sprite
- """
- import pygame
- import random
- import math
- pygame.init()
- global distance
- class ControlData:
- def __init__(self):
- self.moveX = 0
- self.moveY = 0
- class StateDefine():
- def __init__(self):
- self.currentState=0
- self.currentState2=0
- self.health = 100
- global fire
- class Missle(pygame.sprite.Sprite):
- def __init__(self,screen, (x, y), playerShip):
- pygame.sprite.Sprite.__init__(self)
- self.image = pygame.image.load("missle.png").convert()
- self.image = self.image.convert()
- tranColor = self.image.get_at((1, 1))
- self.image.set_colorkey(tranColor)
- self.rect = self.image.get_rect()
- self.missleX = float(x)
- self.missleY = float(y)
- self.ship = playerShip
- self.count = 0
- self.currentState2 = 0
- self.screen = screen
- def update(self):
- ##PatternMovement----------------
- keys = pygame.key.get_pressed()
- if keys[pygame.K_SPACE]:
- print 'fire'
- fire3=True
- self.count += 1
- if self.count < 600:
- self.caculateNextPosition()
- self.rect.center = (missleX, missleY)
- elif self.count < 600:
- self.caculateNextPositionEvade()
- self.rect.center = (missleX, missleY)
- else:
- self.count = 0
- def caculateNextPosition(self): ##Fastest Spaceship
- missleX = self.ship.rect.centerx
- missleY = self.ship.rect.centery
- keys = pygame.key.get_pressed()
- if missleY < self.missleY:
- missleY += 3
- elif missleY > self.missleY:
- missleY -= 3
- if missleX < self.missleX:
- missleX += 3
- elif missleX > self.missleX :
- missleX -= 3
- class Box(pygame.sprite.Sprite):
- def __init__(self,screen, (x, y), playerShip):
- pygame.sprite.Sprite.__init__(self)
- self.image = pygame.image.load("heart.png").convert()
- self.image = self.image.convert()
- tranColor = self.image.get_at((1, 1))
- self.image.set_colorkey(tranColor)
- self.rect = self.image.get_rect()
- self.boxX = float(x)
- self.boxY = float(y)
- self.ship = playerShip
- self.count = 0
- self.currentState2 = 0
- self.screen = screen
- ptu = ControlData()
- ptu.moveX = 1
- ptu.moveY = 1
- ptd = ControlData()
- ptd.moveX = 1
- ptd.moveY = -1
- self.pattern = [ptu, ptu, ptu, ptu, ptu, ptu, ptu, ptu, ptu, ptu,
- ptd, ptd, ptd, ptd, ptd, ptd, ptd, ptd, ptd, ptd,]
- self.patternIndex = 0
- def update(self):
- ##PatternMovement----------------
- posX=self.boxX
- posY=self.boxY
- cd=self.pattern[self.patternIndex]
- self.boxX+=cd.moveX
- self.boxY+=cd.moveY
- self.patternIndex += 1
- if self.patternIndex >= len(self.pattern):
- self.patternIndex = 0
- self.count += 1
- #So the spaceship won't go outside the screen
- if self.boxX > self.screen.get_width():
- self.boxX = 0
- if self.boxY < 0:
- self.boxY = self.screen.get_height()
- ##PatternMovement----------------
- if self.count < 600:
- self.caculateNextPosition()
- self.rect.center = (self.boxX, self.boxY)
- elif self.count < 600:
- self.caculateNextPositionEvade()
- self.rect.center = (self.boxX, self.boxY)
- else:
- self.count = 0
- def caculateNextPosition(self):
- boxX = self.ship.rect.centerx
- boxY = self.ship.rect.centery
- class EnemyShip(pygame.sprite.Sprite):
- def __init__(self,screen, (x, y), playerShip):
- pygame.sprite.Sprite.__init__(self)
- self.image = pygame.image.load("enemy.png").convert()
- self.image = self.image.convert()
- tranColor = self.image.get_at((1, 1))
- self.image.set_colorkey(tranColor)
- self.rect = self.image.get_rect()
- self.enemyX = float(x)
- self.enemyY = float(y)
- self.ship = playerShip
- self.count = 0
- self.currentState2 = 0
- self.screen = screen
- ptu = ControlData()
- ptu.moveX = 5
- #ptu.moveY = 5
- ptd = ControlData()
- ptd.moveX = 5
- # ptd.moveY = -5
- self.pattern = [ptu, ptu, ptu, ptu, ptu, ptu, ptu, ptu, ptu, ptu,
- ptd, ptd, ptd, ptd, ptd, ptd, ptd, ptd, ptd, ptd,]
- self.patternIndex = 0
- def update(self):
- ##PatternMovement----------------
- posX=self.enemyX
- posY=self.enemyY
- cd=self.pattern[self.patternIndex]
- self.enemyX+=cd.moveX
- self.enemyY+=cd.moveY
- self.patternIndex += 1
- if self.patternIndex >= len(self.pattern):
- self.patternIndex = 0
- self.count += 1
- x_component = self.ship.rect.centerx-self.enemyX
- y_component = self.ship.rect.centery-self.enemyY
- distance = math.hypot(x_component, y_component)##If player comes within radius of 100,intiate chase state
- #So the spaceship won't go outside the screen
- if self.enemyX > self.screen.get_width():
- self.enemyX = 0
- if self.enemyY < 0:
- self.enemyY = self.screen.get_height()
- ##PatternMovement----------------
- if distance > 200 and self.enemyY !=68:
- self.enemyY+=1
- if self.enemyY>self.screen.get_height():
- self.enemyY=68
- if distance < 200:
- self.currentState2 = 1
- elif distance > 200:
- self.currentState2 = 0
- if self.count < 600:
- self.caculateNextPosition()
- self.rect.center = (self.enemyX, self.enemyY)
- elif self.count < 600:
- self.caculateNextPositionEvade()
- self.rect.center = (self.enemyX, self.enemyY)
- else:
- self.count = 0
- def caculateNextPosition(self): ##Fastest Spaceship
- shipX = self.ship.rect.centerx
- shipY = self.ship.rect.centery
- if self.currentState2==1:
- if self.enemyY < shipY:
- self.enemyY += 3
- elif self.enemyY > shipY:
- self.enemyY -= 3
- if self.enemyX < shipX:
- self.enemyX += 3
- elif self.enemyX > shipX:
- self.enemyX -= 3
- elif self.currentState2==0:
- if self.enemyY < shipY:
- self.enemyY += 0
- elif self.enemyY > shipY:
- self.enemyY -= 0
- if self.enemyX < shipX:
- self.enemyX += 0
- elif self.enemyX > shipX:
- self.enemyX -= 0
- class EnemyShip2(pygame.sprite.Sprite):
- def __init__(self,screen, (x, y), playerShip):
- pygame.sprite.Sprite.__init__(self)
- self.image = pygame.image.load("enemy.png")
- self.image = self.image.convert()
- tranColor = self.image.get_at((1, 1))
- self.image.set_colorkey(tranColor)
- self.rect = self.image.get_rect()
- self.enemy2X = float(x)
- self.enemy2Y = float(y)
- self.ship = playerShip
- self.count = 0
- self.currentState = 0
- def states(self):
- 'Defining States'
- ## if self.currentState = 1
- ## elif self.currentState = 2
- # if self.EN2State == self.idle
- def update(self):
- test = False
- self.count += 1
- x_component = self.ship.rect.centerx-self.enemy2X
- y_component = self.ship.rect.centery-self.enemy2Y
- distance = math.hypot(x_component, y_component)
- if distance < 200:
- self.currentState = 1
- elif distance > 200:
- self.currentState = 0
- if self.count < 600:
- self.caculateNextPosition()
- self.rect.center = (self.enemy2X, self.enemy2Y)
- elif self.count < 600:
- self.caculateNextPositionEvade()
- self.rect.center = (self.enemy2X, self.enemy2Y)
- else:
- self.count = 0
- def caculateNextPosition(self):
- shipX = self.ship.rect.centerx
- shipY = self.ship.rect.centery
- if self.currentState==1:
- if self.enemy2Y < shipY:
- self.enemy2Y += 2
- elif self.enemy2Y > shipY:
- self.enemy2Y -= 2
- if self.enemy2X < shipX:
- self.enemy2X += 2
- elif self.enemy2X > shipX:
- self.enemy2X -= 2
- elif self.currentState==0:
- if self.enemy2Y < shipY:
- self.enemy2Y += 0
- elif self.enemy2Y > shipY:
- self.enemy2Y -= 0
- if self.enemy2X < shipX:
- self.enemy2X += 0
- elif self.enemy2X > shipX:
- self.enemy2X -= 0
- class PlayerShip(pygame.sprite.Sprite):
- def __init__(self, screen, (x, y)):
- pygame.sprite.Sprite.__init__(self)
- self.screen = screen
- self.image = pygame.image.load("player.png")
- self.image = self.image.convert()
- tranColor = self.image.get_at((1, 1))
- self.image.set_colorkey(tranColor)
- self.rect = self.image.get_rect()
- self.rect.center = (x, y)
- self.health = 100
- def update(self):
- x_pos =self.rect.centerx
- y_pos =self.rect.centery
- fire = False
- keys = pygame.key.get_pressed()
- if keys[pygame.K_LEFT]:
- self.rect.centerx -= 4
- if keys[pygame.K_RIGHT]:
- self.rect.centerx += 4
- if keys[pygame.K_UP]:
- self.rect.centery -= 4
- if keys[pygame.K_DOWN]:
- self.rect.centery += 4
- #if keys[pygame.K_SPACE]:
- # print 'fire'
- if self.rect.centerx > self.screen.get_width():
- self.rect.centerx = 0
- if self.rect.centerx < 0:
- self.rect.centerx = self.screen.get_width()
- if self.rect.centery > self.screen.get_height():
- self.rect.centery = 0
- if self.rect.centery < 0:
- self.rect.centery = self.screen.get_height()
- def main():
- screen = pygame.display.set_mode((1000, 620))
- pygame.display.set_caption("Space blaster")
- background = pygame.Surface(screen.get_size())
- background.fill((0, 0, 0))
- screen.blit(background, (0, 0))
- playerShip = PlayerShip(screen, (800,600))
- enemyShip1 = EnemyShip(screen,(100, 68), playerShip)
- enemyShip2 = EnemyShip2(screen,(300, 30),playerShip)
- Box1 = Box(screen,(100,40),playerShip)
- Missle1 = Missle(screen,(800,100),playerShip)
- enemySprites = pygame.sprite.Group(enemyShip1, enemyShip2)
- allSprites = pygame.sprite.Group(playerShip, enemyShip1, enemyShip2,Box1,Missle1)
- clock = pygame.time.Clock()
- b1 = "space_background.gif"
- back = pygame.image.load(b1).convert()
- screenHeight = back.get_rect().height
- h = 0
- keepGoing = True
- while keepGoing:
- clock.tick(60)
- if h >= screenHeight:
- h = 0
- screen.blit(back, (0,h))
- screen.blit(back, (0,h-screenHeight))
- h = h + 5 # faster move
- # Handle Events
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- keepGoing = False
- # Update the sprite
- # Clear the 'screen' of all of the sprites using the background surface
- allSprites.clear(screen, background)
- # Call all the sprites update methods
- allSprites.update()
- # Draw the sprites to the screen
- allSprites.draw(screen)
- # Update the full display Surface to the screen
- pygame.display.flip()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement