Don't like ads? PRO users don't see any ads ;-)
Guest

tutorial 20-21 mejorado

By: a guest on Jun 22nd, 2012  |  syntax: Python  |  size: 1.95 KB  |  hits: 23  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import pygame
  2. pygame.init()
  3. pantalla=pygame.display.set_mode((800, 600))
  4. reloj1=pygame.time.Clock()
  5. imagen1=pygame.image.load("imagen.png")
  6. salir=False
  7. #posicion
  8. (x,y)=(100,100)
  9. #velocidad
  10. vel_x=0
  11. vel_y=0
  12. #rectangulo
  13. r1=pygame.rect.Rect(500,300,50,100)
  14. r2=pygame.rect.Rect(70,300,50,100)
  15. #sprite
  16. sprite1=pygame.sprite.Sprite() #inicialisamos un sprite
  17. sprite1.image=imagen1 #cargamos una imagen al sprite
  18. sprite1.rect=imagen1.get_rect() #una forma de obtener las dimensiones de la imagen devuelve el ancho y el alto
  19. sprite1.rect.top=140 #posicion del srpite eje y
  20. sprite1.rect.left=200 #posiciion del sprite eje x
  21.  
  22. while salir!=True:
  23.     for event in pygame.event.get():
  24.         if event.type==pygame.QUIT:
  25.             salir=True
  26.         if event.type==pygame.KEYDOWN:
  27.             if event.key==pygame.K_LEFT:
  28.                 vel_x-=10
  29.             if event.key==pygame.K_RIGHT:
  30.                 vel_x+=10
  31.             if event.key==pygame.K_UP:
  32.                 vel_y=-10
  33.             if event.key==pygame.K_DOWN:
  34.                 vel_y=+10          
  35.         if event.type==pygame.KEYUP:
  36.             if event.key==pygame.K_LEFT:
  37.                 vel_x=0
  38.             if event.key==pygame.K_RIGHT:
  39.                 vel_x=0
  40.             if event.key==pygame.K_UP:
  41.                 vel_y=0
  42.             if event.key==pygame.K_DOWN:
  43.                 vel_y=0
  44.     x+=vel_x
  45.     y=-vel_x
  46.     viejo_x=sprite1.rect.left #posicion en el eje x guardada
  47.     viejo_y=sprite1.rect.top
  48.     sprite1.rect.move_ip(vel_x,vel_y) #mueve el sprite
  49.     if sprite1.rect.colliderect(r1) or sprite1.rect.colliderect(r2): #manejo colision
  50.         sprite1.rect.left=viejo_x
  51.         sprite1.rect.top=viejo_y
  52.     reloj1.tick(15)  
  53.     pantalla.fill((255,255,255))
  54.     pygame.draw.rect(pantalla, (0,200,0), r1) #dibujo el rectangulo
  55.     pygame.draw.rect(pantalla,(0,200,0),r2)
  56.     pantalla.blit(sprite1.image,sprite1.rect) #dibujo el srpite
  57.     pygame.display.update()
  58. pygame.quit