Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import sys
- WIDTH = 800
- HEIGHT = 800
- FPS = 10
- COLOR_BLACK = (0, 0, 0)
- COLOR_RED = (255, 0, 0)
- WIDTH_STEPS = WIDTH / 40
- HEIGHT_STEPS = HEIGHT / 40
- class Brick(object):
- x = 0
- y = 0
- color = None
- def __init__(self, x, y, color):
- self.x = x
- self.y = y
- self.color = color
- def show(self, pygame, background):
- pygame.draw.rect(background,
- self.color,
- pygame.Rect(self.x * WIDTH_STEPS, self.y * HEIGHT_STEPS, WIDTH_STEPS, HEIGHT_STEPS))
- class Snake(object):
- x = 5
- y = 5
- xspeed = 1
- yspeed = 0
- tail = []
- total = 0
- pygame = None
- def __init__(self, pygame):
- self.pygame = pygame
- self.tail.append(Brick(self.x, self.y, COLOR_RED))
- self.total = 1;
- def update(self):
- print len(self.tail)
- if (self.total == len(self.tail)):
- for i in xrange(0, len(self.tail) - 1):
- self.tail[i] = self.tail[i+1]
- self.tail.append(Brick(self.x, self.y, COLOR_RED))
- self.tail[self.total - 1] = Brick(self.x, self.y, COLOR_RED)
- self.x = self.x + self.xspeed
- self.y = self.y + self.yspeed
- #self.total += 1
- def show(self, background):
- for i in self.tail:
- i.show(self.pygame, background)
- def setSpeed(self, xspeed, yspeed):
- self.xspeed = xspeed
- self.yspeed = yspeed
- def main():
- # Initialize imported pygame modules
- pygame.init()
- # Set the window's caption
- pygame.display.set_caption("Snake")
- clock = pygame.time.Clock()
- screen = pygame.display.set_mode((WIDTH, HEIGHT))
- background = pygame.Surface((WIDTH, HEIGHT))
- background = background.convert()
- background.fill(COLOR_BLACK)
- # Blit everything to screen
- screen.blit(background, (0, 0))
- # Update the screen
- pygame.display.flip()
- snk = Snake(pygame)
- print snk
- # Main loop
- while True:
- clock.tick(FPS)
- # Erase everything drawn at last step by filling the background
- # with color black
- background.fill(COLOR_BLACK)
- # Check for Quit event
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- sys.exit()
- # Check for key presses and update paddles accordingly
- keys_pressed = pygame.key.get_pressed()
- if keys_pressed[pygame.K_w]:
- snk.setSpeed(0, -1)
- # Do something
- pass
- if keys_pressed[pygame.K_s]:
- snk.setSpeed(0, 1)
- # Do something
- pass
- if keys_pressed[pygame.K_a]:
- snk.setSpeed(-1, 0)
- # Do something
- pass
- if keys_pressed[pygame.K_d]:
- snk.setSpeed(1, 0)
- # Do something
- pass
- snk.update()
- snk.show(background)
- # Update game state
- pygame.display.flip()
- screen.blit(background, (0, 0))
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement