943,985 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 3436
  • Python RSS
Dec 13th, 2006
0

Revamping pac man for a beginner programmer

Expand Post »
I am horribly (obssesed?) with writing a visually appealing game (simple) in python. My text adventures didn't appeal to my aufience (family), so I was looking at a tut on making a pac man clone, supposed to be line by line, it seemes choppy. Any ways, here is the code.
Python Syntax (Toggle Plain Text)
  1. #! /usr/bin/env python
  2. import os, sys
  3. import pygame
  4. from pygame.locals import *
  5. from helpers import *
  6. if not pygame.font: print 'Warning, fonts disabled'
  7. if not pygame.mixer: print 'Warning, sound disabled'
  8. class PyManMain:
  9. """The Main PyMan Class - This class handles the main
  10. initialization and creating of the Game."""
  11.  
  12. def __init__(self, width=640,height=480):
  13. """Initialize"""
  14. """Initialize PyGame"""
  15. pygame.init()
  16. """Set the window Size"""
  17. self.width = width
  18. self.height = height
  19. """Create the Screen"""
  20. self.screen = pygame.display.set_mode((self.width
  21. , self.height))
  22.  
  23. def MainLoop(self):
  24. """This is the Main Loop of the Game"""
  25.  
  26. """Load All of our Sprites"""
  27. self.LoadSprites();
  28. """tell pygame to keep sending up keystrokes when they are
  29. held down"""
  30. pygame.key.set_repeat(500, 30)
  31.  
  32. """Create the background"""
  33. self.background = pygame.Surface(self.screen.get_size())
  34. self.background = self.background.convert()
  35. self.background.fill((0,0,0))
  36.  
  37. while 1:
  38. for event in pygame.event.get():
  39. if event.type == pygame.QUIT:
  40. sys.exit()
  41. elif event.type == KEYDOWN:
  42. if ((event.key == K_RIGHT)
  43. or (event.key == K_LEFT)
  44. or (event.key == K_UP)
  45. or (event.key == K_DOWN)):
  46. self.snake.move(event.key)
  47.  
  48. """Check for collision"""
  49. lstCols = pygame.sprite.spritecollide(self.snake
  50. , self.pellet_sprites
  51. , True)
  52. """Update the amount of pellets eaten"""
  53. self.snake.pellets = self.snake.pellets + len(lstCols)
  54.  
  55. """Do the Drawging"""
  56. self.screen.blit(self.background, (0, 0))
  57. if pygame.font:
  58. font = pygame.font.Font(None, 36)
  59. text = font.render("Pellets %s" % self.snake.pellets
  60. , 1, (255, 0, 0))
  61. textpos = text.get_rect(centerx=self.background.get_width()/2)
  62. self.screen.blit(text, textpos)
  63.  
  64. self.pellet_sprites.draw(self.screen)
  65. self.snake_sprites.draw(self.screen)
  66. pygame.display.flip()
  67.  
  68. def LoadSprites(self):
  69. """Load the sprites that we need"""
  70. self.snake = Snake()
  71. self.snake_sprites = pygame.sprite.RenderPlain((self.snake))
  72.  
  73. """figure out how many pellets we can display"""
  74. nNumHorizontal = int(self.width/64)
  75. nNumVertical = int(self.height/64)
  76. """Create the Pellet group"""
  77. self.pellet_sprites = pygame.sprite.Group()
  78. """Create all of the pellets and add them to the
  79. pellet_sprites group"""
  80. for x in range(nNumHorizontal):
  81. for y in range(nNumVertical):
  82. self.pellet_sprites.add(Pellet(pygame.Rect(x*64, y*64, 64, 64)))
  83.  
  84. class Snake(pygame.sprite.Sprite):
  85. """This is our snake that will move around the screen"""
  86.  
  87. def __init__(self):
  88. pygame.sprite.Sprite.__init__(self)
  89. self.image, self.rect = load_image('snake.png',-1)
  90. self.pellets = 0
  91. """Set the number of Pixels to move each time"""
  92. self.x_dist = 5
  93. self.y_dist = 5
  94.  
  95. def move(self, key):
  96. """Move your self in one of the 4 directions according to key"""
  97. """Key is the pyGame define for either up,down,left, or right key
  98. we will adjust outselfs in that direction"""
  99. xMove = 0;
  100. yMove = 0;
  101.  
  102. if (key == K_RIGHT):
  103. xMove = self.x_dist
  104. elif (key == K_LEFT):
  105. xMove = -self.x_dist
  106. elif (key == K_UP):
  107. yMove = -self.y_dist
  108. elif (key == K_DOWN):
  109. yMove = self.y_dist
  110. #self.rect = self.rect.move(xMove,yMove);
  111. self.rect.move_ip(xMove,yMove);
  112.  
  113. class Pellet(pygame.sprite.Sprite):
  114.  
  115. def __init__(self, rect=None):
  116. pygame.sprite.Sprite.__init__(self)
  117. self.image, self.rect = load_image('pellet.png',-1)
  118. if rect != None:
  119. self.rect = rect
  120.  
  121. if __name__ == "__main__":
  122. MainWindow = PyManMain()
  123. MainWindow.MainLoop()

Anyways, how do I change the sprites used? I see the self.loadSprites function, so I was wondering, how do I edit the sprites this function loads? If neccessary, I will give info on the directories they are placed in.
Also, what should I learn/read to better make (also, easy up the development process) these games. I want to make a rpg pacman clone. Not sure exactly what that means, I guess, pick up weapons, hit points, etx (unles hitpoints are too intermediate I could just have lives).
I want to have more than the black maze backround also. Any ideas/suggestions on how to make this game would be appreciated.
Also, where could I find the most in depth tut on making a first game in python?
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
mruane is offline Offline
76 posts
since Oct 2006
Dec 14th, 2006
0

Re: Revamping pac man for a beginner programmer

What does the module 'helpers' look like? I don't seem to have it in my normal PyGame installation.
Last edited by vegaseat; Dec 14th, 2006 at 12:23 pm.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Creating a Python ANSII game (dos)
Next Thread in Python Forum Timeline: python and mySQLdb module





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC