Below you will find pages that utilize the taxonomy term “Worm Series”
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.
So let’s add wrap first and see if that makes it simpler.
Adding wrap
Ok we use math here and I’m not going to explain much.
- copysign tells us if the number is negative or positive.
- abs gives a positive value (absolute value)
Finding center requires a math formula and a simple search will give you the answer you need.
tpos is tilepos - cryptic yes - you could expand the name to make clearer
############################
##### Helper Functions #####
############################
def get_tpos_from_pos(pos):
axs = int(math.copysign(1,pos[0]))
ays = int(math.copysign(1,pos[1]))
return axs * int(abs(pos[0])/TILESIZE[0]), ays * int(abs(pos[1])/TILESIZE[1])
def get_pos_from_tpos(tpos):
axs = int(math.copysign(1,tpos[0]))
ays = int(math.copysign(1,tpos[1]))
return axs * (abs(tpos[0]) * TILESIZE[0] + int(TILESIZE[0]/2)), ays * (abs(tpos[1]) * TILESIZE[1] + int(TILESIZE[1]/2))
def calc_current_tile_axis_center_pos(pos_axis):
axs = int(math.copysign(1,pos_axis))
return axs * int(abs(pos_axis)/TILESQRT) * TILESQRT + int(TILESQRT/2)
Grid Movement
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.
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
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
screen.fill(white)
in the loop with
screen.blit(background,(0,0))
Basic Pygame game structure
Prerequisites
Look at the previous posts
- Using Python
- Start using docs to learn for Python and Git
- Have VSCode installed
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.