Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

snorr

By: a guest on Mar 18th, 2013  |  syntax: None  |  size: 2.97 KB  |  hits: 26  |  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. #Jag importerar alla kommandon som ryms inom Pygame, samt vissa systemkommandon.
  2. import pygame, sys, os
  3. from pygame.locals import *
  4.  
  5. #Här importerar jag alla bilder på Pacman och spöken.
  6. #Pacmanbilderna ordnades även in i en lista, pacList, för att skapa en ordning bland dem.
  7. pac1 = pygame.image.load('Pacman1.png')
  8. pac2 = pygame.image.load('Pacman2.png')
  9. pac3 = pygame.image.load('Pacman3.png')
  10. pac4 = pygame.image.load('Pacman4.png')
  11. wall = pygame.image.load('Wall.png')
  12. pacList = [pac1, pac2, pac3, pac4, pac3, pac2]
  13. ghost1 = pygame.image.load('ghost1.png')
  14. ghost2 = pygame.image.load('ghost2.png')
  15. ghost3 = pygame.image.load('ghost3.png')
  16. ghost4 = pygame.image.load('ghost4.png')
  17.  
  18. #Antalet rutor i x- och y-led, spelplanen höjd och bredd samt bakgrundsfärgen i RGB-skala definieras.
  19. #Jag initierar också den inbyggda spelklockan.
  20. xCords, yCords = 20, 20
  21. size =width, height = 800, 800
  22. bgc = (0, 0, 0)
  23. clock = pygame.time.Clock()
  24.  
  25. #Spelplanen skapas och fylls ut med färgen. Planen namnges som "Pacman"
  26. screen = pygame.display.set_mode((width, height))
  27. screen.fill(bgc)
  28. pygame.display.set_caption("Pacman")
  29.  
  30. class Pacman(object):
  31.     def __init__(self, speed=5, x=400, y=400, sprite=pacList):
  32.         self.speed = speed
  33.         self.x = x
  34.         self.y = y
  35.         self.pos = (self.x,self.y)
  36.         self.spriteNum = 0
  37.         self.sprite = sprite
  38.    
  39.     def move(self, direction):
  40.         if direction == "up":
  41.             self.y -= self.speed
  42.         if direction == "down":
  43.             self.y += self.speed
  44.         if direction == "right":
  45.             self.x += self.speed
  46.         if direction == "left":
  47.             self.x -= self.speed
  48.         return (self.x, self.y)
  49.  
  50.     def spriteChange(self):
  51.         if self.spriteNum == 5:
  52.             self.spriteNum = 0
  53.         else:
  54.             self.spriteNum += 1
  55.         if direction == "down":
  56.             return self.rot_center(90)
  57.         return self.sprite[self.spriteNum]
  58.  
  59.     def rot_center(self, angle):
  60.         orig_rect = (self.sprite[self.spriteNum]).get_rect()
  61.         rot_image = pygame.transform.rotate(self.sprite[self.spriteNum], angle)
  62.         rot_rect = orig_rect.copy()
  63.         rot_rect.center = rot_image.get_rect().center
  64.         rot_image = rot_image.subsurface(rot_rect).copy()
  65.         return rot_image
  66.  
  67.    
  68. pacman = Pacman()
  69. direction = 0
  70. done = False
  71. while not done:
  72.     clock.tick(20)
  73.     screen.fill((0, 0, 0))
  74.     for event in pygame.event.get():
  75.         if event.type == KEYDOWN and event.key == K_w:
  76.             direction = "up"
  77.         elif event.type == KEYDOWN and event.key == K_s:
  78.             direction = "down"
  79.         elif event.type == KEYDOWN and event.key == K_d:
  80.             direction = "right"
  81.         elif event.type == KEYDOWN and event.key == K_a:
  82.             direction = "left"
  83.         if event.type == pygame.QUIT:
  84.             done = True
  85.     screen.blit(pacman.spriteChange(), pacman.move(direction))
  86.     pygame.display.flip()
  87. pygame.quit()