Advertisement
Guest User

nitrofurano

a guest
Jul 19th, 2009
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.71 KB | None | 0 0
  1. #! /usr/bin/python
  2. # using sprites_rgba.png from http://img17.imageshack.us/img17/3166/spritesrgba.png
  3. import sys, pygame, math, os, random
  4. from pygame.locals import *
  5. pygame.init()
  6. size=width,height=960,240;screen=pygame.display.set_mode(size);pygame.display.set_caption("multiplayer sprite test with collisions")
  7.  
  8. def rotate_point(pt,rect,angle):
  9.   # Maps coordinates from a given rectangle into a rotated rectangle.
  10.   # For example, the following should be approximately equivalent:
  11.   #   pygame.draw.circle(surface,pt,...)
  12.   #   result=pygame.transform.rotate(surface,angle)
  13.   #   pygame.draw.circle(result,rotate_point(pt,rect,angle)
  14.   #-------------------------------------------------------------------------
  15.   # Move the center to the origin, and find the corners
  16.   translated=rect.move(-pt[0],-pt[1])
  17.   corners   =[translated.topleft,translated.topright,translated.bottomleft,translated.bottomright]
  18.   #-------------------------------------------------------------------------    
  19.   # Rotate the corners
  20.   theta=math.radians(angle)
  21.   c,s=math.cos(theta),math.sin(theta)
  22.   xcoords=[];ycoords=[]
  23.   for x,y in corners:
  24.     xcoords.append(c*x+s*y);ycoords.append(c*y-s*x)
  25.   #-------------------------------------------------------------------------    
  26.   # Return the leftmost point and the topmost point
  27.   return(-min(xcoords),-min(ycoords))
  28.  
  29. spd=4;amnt=4;ampl=8;xpos=[0]*amnt;ypos=[0]*amnt;rotv=[0]*amnt;sprid=[];spridr=[]   #some arrays and variables
  30. for i in range (0,amnt,1):
  31.   xpos[i]=64+(128*i)+random.randint(0,32);ypos[i]=64+random.randint(0,32);rotv[i]=random.randint(0,359)
  32.  
  33. sprall=pygame.image.load("sprites_rgba.png")  #loading sprites
  34. for i in range (0,4,1):
  35.   spritetmp=sprall.subsurface(i*64,0,64,64);spriterecttmp=spritetmp.get_rect()
  36.   sprid.append(spritetmp);spridr.append(spriterecttmp)
  37.  
  38. rotincr=5
  39.  
  40. while 1:
  41.   key=pygame.key.get_pressed()  #checking pressed keys
  42.   if key[pygame.K_a]:xpos[0]-=spd
  43.   if key[pygame.K_d]:xpos[0]+=spd
  44.   if key[pygame.K_w]:ypos[0]-=spd
  45.   if key[pygame.K_s]:ypos[0]+=spd
  46.   if key[pygame.K_z]:rotv[0]+=rotincr
  47.   if key[pygame.K_x]:rotv[0]-=rotincr
  48.  
  49.   if key[pygame.K_f]:xpos[1]-=spd
  50.   if key[pygame.K_h]:xpos[1]+=spd
  51.   if key[pygame.K_t]:ypos[1]-=spd
  52.   if key[pygame.K_g]:ypos[1]+=spd
  53.   if key[pygame.K_v]:rotv[1]+=rotincr
  54.   if key[pygame.K_b]:rotv[1]-=rotincr
  55.  
  56.   if key[pygame.K_j]:xpos[2]-=spd
  57.   if key[pygame.K_l]:xpos[2]+=spd
  58.   if key[pygame.K_i]:ypos[2]-=spd
  59.   if key[pygame.K_k]:ypos[2]+=spd
  60.   if key[pygame.K_m]:rotv[2]+=rotincr
  61.   if key[pygame.K_COMMA]:rotv[2]-=rotincr
  62.  
  63.   if key[pygame.K_LEFT]: xpos[3]-=spd
  64.   if key[pygame.K_RIGHT]:xpos[3]+=spd
  65.   if key[pygame.K_UP]:   ypos[3]-=spd
  66.   if key[pygame.K_DOWN]: ypos[3]+=spd
  67.   if key[pygame.K_KP0]:  rotv[3]+=rotincr
  68.   if key[pygame.K_KP_PERIOD]:rotv[3]-=rotincr
  69.  
  70.   bgcolour=0x998877    #checking collisions
  71.   if spridr[0].colliderect(spridr[1]):bgcolour=0xAA5555
  72.   if spridr[0].colliderect(spridr[2]):bgcolour=0x55AA55
  73.   if spridr[0].colliderect(spridr[3]):bgcolour=0x5555AA
  74.   if spridr[1].colliderect(spridr[2]):bgcolour=0x55AAAA
  75.   if spridr[1].colliderect(spridr[3]):bgcolour=0xAA55AA
  76.   if spridr[2].colliderect(spridr[3]):bgcolour=0xAAAA55
  77.   screen.fill(bgcolour)
  78.  
  79.   for i in range (0,amnt,1):    #displaying sprites
  80.     spridr[i].centerx=xpos[i]
  81.     spridr[i].centery=ypos[i]
  82.  
  83.     #tmq=pygame.transform.rotate(sprid[i],rotv[i])  # <- rotate behaviour looks weird - center of rotation is not kept (...?)
  84.     #screen.blit(tmq,spridr[i])
  85.  
  86.     tmq=pygame.transform.rotate(sprid[i],rotv[i])
  87.     screen.blit(tmq,rotate_point((xpos[i],ypos[i]),spridr[i],rotv[i]))
  88.  
  89.   for event in pygame.event.get():  #praxis stuff
  90.     if event.type==pygame.QUIT:sys.exit()
  91.   pygame.display.flip();pygame.time.delay(1000/50)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement