Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import random
- #
- # pygame (simple) template by furas
- #
- # === CONSTANTS === UPPER_CASE names
- DISPLAY_WIDTH = 800
- DISPLAY_HEIGHT = 600
- FPS = 5
- BLACK = ( 0, 0, 0)
- WHITE = (255, 255, 255)
- RED = (255, 0, 0)
- GREEN = ( 0, 255, 0)
- BLUE = ( 0, 0, 255)
- BLOCK_SIZE = 50
- # === CLASSES === CamelCase names
- class Snake(): # now it is almost Snake(pygame.sprite.Sprite):
- def __init__(self, screen, x, y, tail_color=GREEN, head_color=BLUE):
- self.screen = screen
- self.screen_rect = screen.get_rect()
- # single tile position and size
- self.rect = pygame.Rect(x, y, BLOCK_SIZE, BLOCK_SIZE)
- # add one tile at start - `self.make_longer` will add more tiles
- self.tiles = [self.rect]
- # add automatically new 5 tiles (one tile in every step)
- self.make_longer = 5
- # you can use image file like PNG
- self.image_head = pygame.Surface( (BLOCK_SIZE, BLOCK_SIZE) )
- self.image_head.fill(head_color)
- self.image_tail = pygame.Surface( (BLOCK_SIZE, BLOCK_SIZE) )
- self.image_tail.fill(tail_color)
- # speed
- self.x_change = 0
- self.y_change = 0
- def draw(self):
- # draw all tiles without head ([1:])
- for tile_rect in self.tiles[1:]:
- self.screen.blit(self.image_tail, tile_rect)
- # draw head ([0])
- self.screen.blit(self.image_head, self.tiles[0])
- def update(self):
- # create new head
- self.head_rect = self.tiles[0].move(self.x_change, self.y_change)
- # wall colision - is head still on screen ?
- if not self.screen_rect.contains(self.head_rect):
- return False # dead
- # add head at beginning of tiles (now snake is longer)
- self.tiles.insert(0, self.head_rect)
- # make snake longer
- if not self.make_longer:
- # remove last tile to keep the same width
- del self.tiles[-1]
- else:
- # make longer (one tile in every step)
- self.make_longer -= 1
- return True # still alive
- def add(self):
- # add one tile to self.tiles
- self.make_longer = 1
- class Food(): # now it is almost Food(pygame.sprite.Sprite):
- def __init__(self, screen):
- self.screen = screen
- # apple position and size
- self.rect = pygame.Rect(0, 0, BLOCK_SIZE, BLOCK_SIZE)
- self.spawn() # set random position
- # you can use image from file like PNG
- self.image = pygame.Surface( (BLOCK_SIZE, BLOCK_SIZE) )
- self.image.fill(RED)
- def spawn(self):
- self.rect.x = random.randrange(0, 800, BLOCK_SIZE)
- self.rect.y = random.randrange(0, 600, BLOCK_SIZE)
- def draw(self):
- self.screen.blit(self.image, self.rect)
- # === FUNCTIONS === lower_case names
- def main():
- score = 0
- # --- init ---
- pygame.init()
- game_display = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
- game_display_rect = game_display.get_rect()
- pygame.display.set_caption('Snake')
- # --- objects ---
- schlange = Snake(game_display, game_display_rect.centerx, game_display_rect.centery, GREEN)
- essen = Food(game_display)
- # --- (constants) texts ---
- font = pygame.font.SysFont(None, 50)
- text_pause = font.render('PAUSE', True, WHITE)
- text_pause_rect = text_pause.get_rect()
- # center on screen
- text_pause_rect.center = game_display_rect.center
- text_game_over = font.render('GAME OVER', True, WHITE)
- text_game_over_rect = text_game_over.get_rect()
- # center on screen
- text_game_over_rect.center = game_display_rect.center
- # --- (changing) texts ---
- text_score = font.render('SCORE: '+ str(score), True, WHITE)
- text_score_rect = text_game_over.get_rect()
- # left top cornert
- text_score_rect.left = game_display_rect.left
- text_score_rect.top = game_display_rect.top
- # --- mainloop ---
- paused = False
- alive = True
- clock = pygame.time.Clock()
- running = True
- while running:
- # --- envents ---
- for event in pygame.event.get():
- # --- global events ---
- if event.type == pygame.QUIT:
- running = False
- # --- player events ---
- # you can move it into Snake.event_handle(event)
- # and call schlange.event_handle(event)
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_UP:
- if schlange.y_change == 0: # don't move backword
- schlange.x_change = 0
- schlange.y_change = -50
- elif event.key == pygame.K_DOWN:
- if schlange.y_change == 0: # don't move backword
- schlange.x_change = 0
- schlange.y_change = 50
- elif event.key == pygame.K_LEFT:
- if schlange.x_change == 0: # don't move backword
- schlange.x_change = -50
- schlange.y_change = 0
- elif event.key == pygame.K_RIGHT:
- if schlange.x_change == 0: # don't move backword
- schlange.x_change = 50
- schlange.y_change = 0
- elif event.key == pygame.K_SPACE:
- # pause/unpause game
- paused = not paused
- # --- updates (without draws) ---
- if not paused and alive:
- # move snake
- alive = schlange.update()
- if alive:
- # check snake head collision with apple
- if schlange.tiles[0].colliderect(essen.rect):
- # move apple to new position
- essen.spawn()
- # make snake longer
- schlange.add()
- # new score and new text
- score += 1
- text_score = font.render('SCORE: '+ str(score), True, WHITE)
- text_score_rect = text_game_over.get_rect()
- # left top cornert
- text_score_rect.left = game_display_rect.left
- text_score_rect.top = game_display_rect.top
- # --- all draws (without updates) ---
- # background
- game_display.fill(BLACK)
- # objects
- schlange.draw()
- essen.draw()
- # text and decorations
- game_display.blit(text_score, text_score_rect)
- if paused:
- game_display.blit(text_pause, text_pause_rect)
- if not alive:
- game_display.blit(text_game_over, text_game_over_rect)
- # move on screen
- clock.tick(FPS)
- pygame.display.update()
- # --- the end ---
- # quit or create ending screen
- pygame.quit()
- # ---------------------------------------------------------------------
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement