Screen wrap & Detect edges

Wrap and follow

We finish up this mini series with this post. The basic game will be functional with these last two changes in the game. I don’t want to get into game design or polishing up this ‘demo’ to turn it into a full fun game. That I will let you try using examples from elsewhere.

Choices

In earlier experimenting I had the tail following the head but when I added the screen-wrap it broke.

Grid Movement

There is a few different ways to do this and each way gives you slightly different results.

I wanted a method that would detect when I crossed the center of a tile or landed on it.

I came up with this for going left - I could have started with right, up, or down I guess.

This is debug code and simply prints out position and if center was crossed. If you tried this you’d likely come up with a different solution.

Worm eats food

Food

The food will be placed in a random position initially. When the worm head passes over it we simulate eating and placing new food by simply placing the food at a new position using random.

It is cheaper on resource space than destroying and re-creating the object as a new object.

import random

Change the way “GameObject” places itself.

def __init__(self, img_name, initial_pos):
    self.img = pygame.image.load(img_name)
    self.img = pygame.transform.scale_by(self.img, 0.5 )
    self.rect = self.img.get_rect()
    self.set_pos(initial_pos)

def set_pos(self, pos):
    self.rect.center = pos

Create a food object that repositions not moves. We set it off-screen to start.

Creating and Moving our player

Artwork

  • first get some images for our worm and food

Head over to Kenney’s shape characters package and get the following images to follow along. The files we want are in the png/default folder. The larger images in that folder are 80x80. If you are adventurous try other images.

I’m going to use individual 2D PNG files for each item to start. Note I change all file names to lowercase as that works with Linux, Mac, and Windows.

Add grid lines and Kinematic objects

CODE can be found at my Git Repository

Add background with lines

We want a grid, but we don’t want to draw the lines every frame. We will create a white background with lines on it using code.

#colors
white = (255,255,255)
black = (0,0,0)

#clear the display
screen.fill(white)

# Create a background
background = pygame.Surface.copy(screen)

#draw lines for the grid on the background
for y in range(0, height, 40):
    pygame.draw.line(background, black, (0,y), (width,y))
for x in range(0, width,  40):
    pygame.draw.line(background, black, (x,0), (x,height))

# blit the background to screen
screen.blit(background,(0,0))

We replace the

Basic Pygame game structure

Prerequisites

Look at the previous posts

We use python and Pygame, and this tutorial focuses mostly on using Pygame.

The game should look like this when done

Encouragement: Don’t let Others discourage you. We all hear it. If you listen you will never achieve your goal.

Repository

Github and BitBucket both provide instructions

Create a empty repository online and then follows the instructions given by GitHub to create locally and set remote to your empty GitHub repository.