This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
user:bh011695:pygametutorial:worm.py [2010/12/17 03:57] – created bh011695 | user:bh011695:pygametutorial:worm.py [2010/12/17 17:53] (current) – bh011695 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | <file python> | ||
+ | import random | ||
+ | import pygame | ||
+ | class worm: | ||
+ | def __init__(self, | ||
+ | self.surface = surface | ||
+ | self.x = x #worm head x pos | ||
+ | self.y = y #worm head y pos | ||
+ | self.length = 200 #worm length | ||
+ | self.dirX = 1 #worm x direction | ||
+ | self.dirY = 0 #worm y direction | ||
+ | self.body = [] #x, y pos for worm body pixels | ||
+ | self.crashed = False #if true, exits the main game loop | ||
+ | |||
+ | def keyEvent(self, | ||
+ | """ | ||
+ | |||
+ | if event.key == pygame.K_UP and self.dirY != 1: | ||
+ | self.dirX = 0 | ||
+ | self.dirY = -1 | ||
+ | elif event.key == pygame.K_DOWN and self.dirY != -1: | ||
+ | self.dirX = 0 | ||
+ | self.dirY = 1 | ||
+ | elif event.key == pygame.K_LEFT and self.dirX != 1: | ||
+ | self.dirX = -1 | ||
+ | self.dirY = 0 | ||
+ | elif event.key == pygame.K_RIGHT and self.dirX != -1: | ||
+ | self.dirX = 1 | ||
+ | self.dirY = 0 | ||
+ | |||
+ | def move(self): | ||
+ | """ | ||
+ | |||
+ | self.x += self.dirX #increment worm's x direction | ||
+ | self.y += self.dirY #increment worm's y direction | ||
+ | |||
+ | #Checks to see if the worm is in contact with anything other than | ||
+ | #a black (background) or red (food) pixel, and if so, self.crashed | ||
+ | #is set to true | ||
+ | |||
+ | r, g, b, a = self.surface.get_at((self.x, | ||
+ | | ||
+ | if (r, g, b) == (0, 0, 0) or (r, g, b) == (255, 0, 0): | ||
+ | self.crashed = False | ||
+ | else: | ||
+ | self.crashed = True | ||
+ | |||
+ | self.body.insert(0, | ||
+ | |||
+ | #Compares the number of the worm's body pixels to the length. If | ||
+ | #true, a pixel is removed from the worm. | ||
+ | if len(self.body) > self.length: | ||
+ | self.body.pop() | ||
+ | |||
+ | def draw(self): | ||
+ | """ | ||
+ | |||
+ | for x, y in self.body: | ||
+ | self.surface.set_at((x, | ||
+ | |||
+ | def crashCheck(self): | ||
+ | """ | ||
+ | return self.crashed | ||
+ | |||
+ | def getX(self): | ||
+ | """ | ||
+ | return self.x | ||
+ | |||
+ | def getY(self): | ||
+ | """ | ||
+ | return self.y | ||
+ | |||
+ | def increaseLength(self): | ||
+ | """ | ||
+ | self.length += 4 | ||
+ | | ||
+ | class food: | ||
+ | """ | ||
+ | def __init__(self, | ||
+ | self.surface = surface | ||
+ | self.color = (255, 0, 0) # | ||
+ | self.x = random.randint(0, | ||
+ | self.y = random.randint(0, | ||
+ | | ||
+ | def drop(self): | ||
+ | """ | ||
+ | X_MAX = self.x + 5 #Max width of the food item | ||
+ | Y_MAX = self.y + 5 #Max height of the food item | ||
+ | |||
+ | #Draws a 5 X 5 food item to the screen | ||
+ | for i in range (self.x, X_MAX): | ||
+ | for j in range (self.y, Y_MAX): | ||
+ | self.surface.set_at((i, | ||
+ | |||
+ | #Returns food's color | ||
+ | def getColor(self): | ||
+ | return self.color | ||
+ | | ||
+ | def drawObstacles(screen, | ||
+ | GREEN = (0, 255, 0) | ||
+ | startX = 100 | ||
+ | startY = 50 | ||
+ | stopX = 100 | ||
+ | stopY = 150 | ||
+ | | ||
+ | pygame.draw.line(screen, | ||
+ | pygame.draw.line(screen, | ||
+ | pygame.draw.line(screen, | ||
+ | pygame.draw.line(screen, | ||
+ | pygame.draw.line(screen, | ||
+ | pygame.draw.line(screen, | ||
+ | pygame.draw.line(screen, | ||
+ | | ||
+ | def checkEatenFood(r, | ||
+ | foodEaten = False | ||
+ | | ||
+ | if (r, g, b) == foodColor: | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | return foodEaten | ||
+ | |||
+ | def checkForCrash(w, | ||
+ | if w.crashCheck(): | ||
+ | print(" | ||
+ | crash.play() | ||
+ | running = False | ||
+ | elif w.getX() <= 0: | ||
+ | print(" | ||
+ | crash.play() | ||
+ | running = False | ||
+ | elif w.getX() >= width - 1: | ||
+ | print(" | ||
+ | crash.play() | ||
+ | running = False | ||
+ | elif w.getY() <= 0: | ||
+ | print(" | ||
+ | crash.play() | ||
+ | running = False | ||
+ | elif w.getY() >= height - 1: | ||
+ | print(" | ||
+ | crash.play() | ||
+ | running = False | ||
+ | return running | ||
+ | | ||
+ | def getEvents(running, | ||
+ | for event in pygame.event.get(): | ||
+ | if event.type == pygame.QUIT: | ||
+ | running = False | ||
+ | elif event.type == pygame.KEYDOWN: | ||
+ | w.keyEvent(event) | ||
+ | return running | ||
+ | |||
+ | #Initialize variables | ||
+ | width = 640 # | ||
+ | height = 400 # | ||
+ | screen = pygame.display.set_mode((width, | ||
+ | pygame.display.set_caption(" | ||
+ | clock = pygame.time.Clock() # | ||
+ | FPS = 90 # | ||
+ | running = True # | ||
+ | SCREEN_COLOR = (0, 0, 0) # | ||
+ | itemsEaten = 0 # | ||
+ | |||
+ | #Initialize sounds | ||
+ | pygame.mixer.init() | ||
+ | chomp = pygame.mixer.Sound(" | ||
+ | crash = pygame.mixer.Sound(" | ||
+ | |||
+ | #Initialize worm and food objects w and f | ||
+ | w = worm(screen, | ||
+ | f = food(screen, | ||
+ | |||
+ | #main() | ||
+ | while running: | ||
+ | screen.fill(SCREEN_COLOR) | ||
+ | | ||
+ | drawObstacles(screen, | ||
+ | | ||
+ | f.drop() # | ||
+ | foodColor = f.getColor() # | ||
+ | w.draw() # | ||
+ | w.move() # | ||
+ | | ||
+ | #Gets the color of the worm's current x and y position on the screen | ||
+ | i = w.getX() | ||
+ | j = w.getY() | ||
+ | r, g, b, a = w.surface.get_at((i, | ||
+ | | ||
+ | #Checks to see if the worm's current position color matches that | ||
+ | #of the dropped food item. If yes, the number of eaten items is | ||
+ | # | ||
+ | if checkEatenFood(r, | ||
+ | f = food(screen, | ||
+ | itemsEaten += 1 | ||
+ | | ||
+ | #Checks to see if the worm has crashed into something and sets | ||
+ | #running accordingly. | ||
+ | running = checkForCrash(w, | ||
+ | | ||
+ | #Checks for events | ||
+ | running = getEvents(running, | ||
+ | | ||
+ | pygame.display.flip() # | ||
+ | clock.tick(FPS) # | ||
+ | #end main() | ||
+ | |||
+ | print(" | ||
+ | </ |