Advertisement
zeroblitzt

Pygame Physics

Feb 19th, 2013
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.60 KB | None | 0 0
  1. #pygame Test by KNovak
  2. import pygame, sys, os, math
  3. from pygame.locals import *
  4. pygame.init()
  5. pygame.key.set_repeat(1)
  6. fullscreen = False;
  7.  
  8. #startup settings
  9. for arg in sys.argv:
  10.     if arg == "-f":
  11.         fullscreen = True
  12.  
  13.  
  14. #Video Display information
  15. vidinfo = pygame.display.Info()
  16.  
  17. if fullscreen == True:
  18.     vidsize = (vidinfo.current_w, vidinfo.current_h)
  19.     window = pygame.display.set_mode(vidsize, pygame.FULLSCREEN)
  20. else:
  21.     vidsize = (640,480)
  22.     window = pygame.display.set_mode(vidsize)
  23. pygame.display.set_caption('KNova Test Window')
  24. screen = pygame.display.get_surface()
  25. background = pygame.Surface((screen.get_width(), screen.get_height()))
  26.  
  27. #colors
  28. WHITE = (255,255,255)
  29. BLUE = (0,0,150)
  30. GREEN = (0,150,0)
  31.  
  32. #text
  33. def drawText (text, color, xloc, yloc, size):
  34.     fontObj = pygame.font.Font('freesansbold.ttf', size)
  35.     textSurface = fontObj.render(text, True, color)
  36.     textRectObj = textSurface.get_rect()
  37.     textRectObj.center = (xloc, yloc)
  38.     return textSurface, textRectObj
  39.  
  40. #Sprite Class for the Ship Image
  41. class Ship(pygame.sprite.Sprite):
  42.     image = pygame.image.load("./resources/ship.png")
  43.     image = image.convert()
  44.    
  45.     #initialization function
  46.     def __init__(self, position=(100,100)):
  47.         pygame.sprite.Sprite.__init__(self,self.groups)
  48.         self.pos = position
  49.         self.orig_image = Ship.image
  50.         self.image = self.orig_image
  51.         self.rect = self.image.get_rect()
  52.         self.rect.center = self.pos
  53.         self.angle = 0
  54.         self.moveangle = 0
  55.         self.vx = 0
  56.         self.vy = 0
  57.         self.maxvel = 3
  58.    
  59.     #move function
  60.     def move(self, newpos):
  61.         oldx, oldy = self.pos
  62.         dx, dy = newpos
  63.         self.pos = (oldx - dx, oldy - dy)
  64.         self.rect.center = self.pos
  65.  
  66.        
  67.     #update function which will replace both move and rotate
  68.     def update(self):
  69.    
  70.         #handle rotations
  71.         if self.angle >= 360:
  72.             self.angle = 0
  73.         if self.angle <= -360:
  74.             self.angle = 0
  75.         loc = self.rect.center
  76.         self.image = pygame.transform.rotate(self.orig_image, self.angle)
  77.         self.rect = self.image.get_rect()
  78.         self.rect.center = loc
  79.        
  80.         #handle 2D movement
  81.         self.move(getDirection(self.angle, self.vx, self.vy))
  82.        
  83.         #boundaries
  84.         if self.pos[0] > vidsize[0]:
  85.             #right
  86.             self.vx = -self.vx
  87.         if self.pos[0] < 0:
  88.             #left
  89.             self.vx = -self.vx
  90.         if self.pos[1] > vidsize[1]:
  91.             #bottom
  92.             self.vy = -self.vy
  93.         if self.pos[1] < 0:
  94.             #top
  95.             self.vy = -self.vy
  96.        
  97.        
  98. spriteGroup = pygame.sprite.Group()
  99. Ship.groups = spriteGroup
  100. newShip = Ship()
  101.  
  102. #sprite group update
  103. def spriteUpdate(group, screen, background):
  104.     group.clear(screen, background)
  105.     group.update()
  106.     group.draw(screen)
  107.    
  108. #trig functions for updating position
  109. def getDirection(angle,velx, vely):
  110.     x = math.sin(math.radians(angle)) * velx
  111.     y = math.cos(math.radians(angle)) * vely
  112.     return (x,y)
  113.  
  114.  
  115. #get keyboard events
  116. def pyin(events):
  117.     for event in events:
  118.         if event.type == QUIT:
  119.             sys.exit(0)
  120.         if event.type == KEYDOWN:
  121.             key = pygame.key.get_pressed()
  122.             if (key[pygame.K_ESCAPE]):
  123.                 sys.exit(0)
  124.             if (key[K_UP]):
  125.                 newShip.vx += 0.5
  126.                 newShip.vy += 0.5
  127.                 if newShip.vx >= newShip.maxvel: newShip.vx = newShip.maxvel
  128.                 if newShip.vy >= newShip.maxvel: newShip.vy = newShip.maxvel
  129.  
  130.             if (key[K_DOWN]):
  131.                 newShip.vx += -0.5
  132.                 newShip.vy += -0.5
  133.                 if newShip.vx <= (0-newShip.maxvel): newShip.vx = (0-newShip.maxvel)
  134.                 if newShip.vy <= (0-newShip.maxvel): newShip.vy = (0-newShip.maxvel)
  135.  
  136.             if (key[K_LEFT]):
  137.                 newShip.angle += 4
  138.             if (key[K_RIGHT]):
  139.                 newShip.angle -= 4
  140.  
  141.  
  142. while True:
  143.     pyin(pygame.event.get())
  144. #   pykey(pygame.key.get_pressed())
  145.     pygame.display.update()
  146.     spriteUpdate(spriteGroup, screen, background)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement