I'm trying to move a text alongside the player character but it gets scaled weird...
Here's a picture of the player character alongside the text "test":
It should be some kind of antialiasing since that is off in the code for the moving text:
import pygame, sys
from pygame.locals import *
class MovingText():
def __init__(self, text, font_size, color, surface, target, off_1, off_2):
self.font = pygame.font.SysFont(None, font_size)
self.textobj = self.font.render(text, 0, color)
self.textrect = self.textobj.get_rect()
self.surface = surface
self.target = target
self.offset = (off_1, off_2)
self.textrect.center = self.target_pos()
def update(self):
self.textrect.center = self.target_pos()
self.surface.blit(self.textobj, self.textrect.center)
def target_pos(self):
pos = self.target.rect.center
return pos[0] + self.offset[1], pos[1] + self.offset[0]
What I think is causing the problem is either when the text gets created, here:
self.follow_text = movingtext.MovingText('test', 10, (255, 255, 255), self.display, self.player, 10, 5)
If you think it's something else you're free to check the rest of the code out, don't really think that but i've been proven wrong once or twice:
import pygame, sys
from pygame.locals import *
from pygame.mouse import get_pos
import time
from utils import button, constants, movingtext
from entities import player, entity
import game
class Game():
def __init__(self, map_number):
pygame.init()
self.clock = pygame.time.Clock()
self.screen = pygame.display.set_mode((pygame.display.Info().current_w, pygame.display.Info().current_h), pygame.RESIZABLE)
self.display = pygame.Surface((300,300))
self.font_small = pygame.font.SysFont(None, 20)
self.font_medium = pygame.font.SysFont(None, 32)
self.test_bg = pygame.image.load('images/wp.png')
self.pause = False
self.timer = 0
self.game_called = time.time()
self.flag_mover = False
self.map_number = map_number
f = open('maps/map'+self.map_number+'.txt')
self.map_data = [[int(c) for c in row] for row in f.read().split('\n')]
f.close()
#Tile list -----
self.spawn_img = pygame.image.load('images/spawn.png').convert()
self.spawn_img.set_colorkey((0, 0, 0))
self.goal_img = pygame.image.load('images/goal.png').convert()
self.goal_img.set_colorkey((0, 0, 0))
self.key_img = pygame.image.load('images/key.png').convert()
self.key_img.set_colorkey((0, 0, 0))
self.lava_img = pygame.image.load('images/lava.png').convert()
self.lava_img.set_colorkey((0, 0, 0))
self.grass_img = pygame.image.load('images/grass2.png').convert()
self.grass_img.set_colorkey((0, 0, 0))
#Player
for y, row in enumerate(self.map_data):
for x, tile in enumerate(row):
if tile == 1:
self.player = player.Player(self.display, (150 + (x+1) * 10 - y * 10, 100 + x * 5 + (y-0.5) * 5), self.map_data)
#goal flag
for y, row in enumerate(self.map_data):
for x, tile in enumerate(row):
if tile == 2:
self.goal_flag = entity.Entity(self.display, (150 + (x+1) * 10 - y * 10, 100 + x * 5 + (y-1) * 5), 'images/goal_flag.png')
#points
self.point_list = []
for y, row in enumerate(self.map_data):
for x, tile in enumerate(row):
if tile == 3:
self.points = entity.Entity(self.display, (150 + (x+1) * 10 - y * 10, 100 + x * 5 + (y-0.5) * 5),'images/point.png')
self.point_list.append(self.points)
self.running = True
self.click = False
def drawText(self, text, font, color, surface, x, y):
textobj = font.render(text, 1, color)
textrect = textobj.get_rect()
textrect.topleft = (x, y)
surface.blit(textobj, textrect)
def gameLoop(self):
while self.running:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
if self.pause == False:
self.pause = True
else:
self.pause = False
if self.pause == False:
self.screen.blit(pygame.transform.scale(self.display, self.screen.get_size()), (0, 0))
self.display.fill(0) #clears the scree
self.drawText('game', self.font_small, (255, 255, 255), self.screen, 20, 20)
# self.drawText('time: '+str(int(self.timer/1000)), self.font_small, (255, 255, 255), self.screen, self.player.rect[0], self.player.rect[1])
self.follow_text = movingtext.MovingText('test', 10, (255, 255, 255), self.display, self.player, 10, 5)
# self.follow_text = movingtext.MovingText()
#Draws the map
for y, row in enumerate(self.map_data):
for x, tile in enumerate(row):
if tile == 0:
self.display.blit(self.lava_img, (150 + x * 10 - y * 10, 100 + x * 5 + y * 5))
if tile == 1:
self.display.blit(self.spawn_img, (150 + x * 10 - y * 10, 100 + x * 5 + y * 5))
if tile == 2:
self.display.blit(self.goal_img, (150 + x * 10 - y * 10, 100 + x * 5 + y * 5))
if tile == 3:
self.display.blit(self.key_img, (150 + x * 10 - y * 10, 100 + x * 5 + y * 5))
if tile == 4:
self.display.blit(self.grass_img, (150 + x * 10 - y * 10, 100 + x * 5 + y * 5))
#collision detection between entities
if self.goal_flag.rect[0] == self.player.rect[0] and self.goal_flag.rect[1] == self.player.rect[1] - 2:
self.flag_mover = True
if self.flag_mover == True:
self.goal_flag.rect[1] += -0.1
#point collision
for point_collision in self.point_list:
if point_collision.rect[0] == self.player.rect[0] and point_collision.rect[1] == self.player.rect[1]:
self.point_list.remove(point_collision)
#update
for points in self.point_list:
points.update()
self.goal_flag.update()
self.player.update()
self.follow_text.update()
else:
self.screen.blit(pygame.transform.scale(self.display, self.screen.get_size()), (0, 0))
self.drawText('game', self.font_small, (255, 255, 255), self.screen, 20, 20)
self.drawText('PAUSED', self.font_medium, (255, 255, 255), self.screen, pygame.display.Info().current_w/2-50, pygame.display.Info().current_h/2)
for y, row in enumerate(self.map_data):
for x, tile in enumerate(row):
if tile == 0:
self.display.blit(self.lava_img, (150 + x * 10 - y * 10, 100 + x * 5 + y * 5))
if tile == 1:
self.display.blit(self.spawn_img, (150 + x * 10 - y * 10, 100 + x * 5 + y * 5))
if tile == 2:
self.display.blit(self.goal_img, (150 + x * 10 - y * 10, 100 + x * 5 + y * 5))
if tile == 3:
self.display.blit(self.key_img, (150 + x * 10 - y * 10, 100 + x * 5 + y * 5))
if tile == 4:
self.display.blit(self.grass_img, (150 + x * 10 - y * 10, 100 + x * 5 + y * 5))
pygame.display.update()
self.clock.tick(60)
The text is scaled as you draw the text on a surface and scale the surface to the size of your window. You have to draw the text on the display surface directly.
Related
I am trying to make realistic water in pygame:
This is till now my code:
from random import randint
import pygame
WIDTH = 700
HEIGHT = 500
win = pygame.display.set_mode((WIDTH, HEIGHT))
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
AQUA = 'aqua'
RADIUS = 1
x, y = 0, HEIGHT//2
K = 1
FORCE = 100
VELOCITY = 0.5
run = True
class Molecule:
def __init__(self, x, y, radius, force, k):
self.x = x
self.y = y
self.radius = radius
self.force = force
self.k = k
self.max_amplitude = y + force/k
self.min_amplitude = y - force/k
self.up = False
self.down = True
self.restore = False
def draw(self, win):
pygame.draw.circle(win, BLACK, (self.x, self.y), self.radius)
def oscillate(self):
if self.y <= self.max_amplitude and self.down == True:
self.y += VELOCITY
if self.y == self.max_amplitude or self.up:
self.up = True
self.down = False
self.y -= VELOCITY
if self.y == self.min_amplitude:
self.up = False
self.down = True
molecules = []
for i in range(100):
FORCE = randint(10, 20)
molecules.append(Molecule(x, y, RADIUS, FORCE, K))
x += 10
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
win.fill(WHITE)
for molecule in molecules:
molecule.draw(win)
molecule.oscillate()
for i in range(len(molecules)):
try:
pygame.draw.line(win, BLACK, (molecules[i].x, molecules[i].y), (molecules[i+1].x, molecules[i+1].y))
pygame.draw.line(win, AQUA, (molecules[i].x, molecules[i].y), (molecules[i+1].x, HEIGHT))
except:
pass
pygame.display.flip()
pygame.quit()
But as may expected the water curve is not smooth:
Look at it:
Sample Img1
I want to connect the two randomly added wave points using a set of circles not line like in this one so that a smooth curve could occur.
And in this way i could add the water color to it such that it will draw aqua lines or my desired color line from the point to the end of screen and all this will end up with smooth water flowing simulation.
Now the question is how could i make the points connect together smoothly into a smooth curve by drawing point circles at relative points?
I suggest sticking the segments with a Bézier curves. Bézier curves can be drawn with pygame.gfxdraw.bezier
Calculate the slopes of the tangents to the points along the wavy waterline:
ts = []
for i in range(len(molecules)):
pa = molecules[max(0, i-1)]
pb = molecules[min(len(molecules)-1, i+1)]
ts.append((pb.y-pa.y) / (pb.x-pa.x))
Use the the tangents to define 4 control points for each segment and draw the curve with pygame.gfxdraw.bezier:
for i in range(len(molecules)-1):
p0 = molecules[i].x, molecules[i].y
p3 = molecules[i+1].x, molecules[i+1].y
p1 = p0[0] + 10, p0[1] + 10 * ts[i]
p2 = p3[0] - 10, p3[1] - 10 * ts[i+1]
pygame.gfxdraw.bezier(win, [p0, p1, p2, p3], 4, BLACK)
Complete example:
from random import randint
import pygame
import pygame.gfxdraw
WIDTH = 700
HEIGHT = 500
win = pygame.display.set_mode((WIDTH, HEIGHT))
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
AQUA = 'aqua'
RADIUS = 1
x, y = 0, HEIGHT//2
K = 1
FORCE = 100
VELOCITY = 0.5
class Molecule:
def __init__(self, x, y, radius, force, k):
self.x = x
self.y = y
self.radius = radius
self.force = force
self.k = k
self.max_amplitude = y + force/k
self.min_amplitude = y - force/k
self.up = False
self.down = True
self.restore = False
def draw(self, win):
pygame.draw.circle(win, BLACK, (self.x, self.y), self.radius)
def oscillate(self):
if self.y <= self.max_amplitude and self.down == True:
self.y += VELOCITY
if self.y == self.max_amplitude or self.up:
self.up = True
self.down = False
self.y -= VELOCITY
if self.y == self.min_amplitude:
self.up = False
self.down = True
molecules = []
for i in range(50):
FORCE = randint(10, 20)
molecules.append(Molecule(x, y, RADIUS, FORCE, K))
x += 20
clock = pygame.time.Clock()
run = True
while run:
clock.tick(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
win.fill(WHITE)
for molecule in molecules:
molecule.draw(win)
molecule.oscillate()
ts = []
for i in range(len(molecules)):
pa = molecules[max(0, i-1)]
pb = molecules[min(len(molecules)-1, i+1)]
ts.append((pb.y-pa.y) / (pb.x-pa.x))
for i in range(len(molecules)-1):
p0 = molecules[i].x, molecules[i].y
p3 = molecules[i+1].x, molecules[i+1].y
p1 = p0[0] + 10, p0[1] + 10 * ts[i]
p2 = p3[0] - 10, p3[1] - 10 * ts[i+1]
pygame.gfxdraw.bezier(win, [p0, p1, p2, p3], 4, BLACK)
for i in range(len(molecules)-1):
pygame.draw.line(win, AQUA, (molecules[i].x, molecules[i].y), (molecules[i].x, HEIGHT))
pygame.display.flip()
pygame.quit()
If you want to "fill" the water, you must calculate the points along the Bézier line and draw a filled polygon. How to calculate a Bézier curve is explained in Trying to make a Bezier Curve on PyGame library How Can I Make a Thicker Bezier in Pygame? and "X". You can use the following function:
def ptOnCurve(b, t):
q = b.copy()
for k in range(1, len(b)):
for i in range(len(b) - k):
q[i] = (1-t) * q[i][0] + t * q[i+1][0], (1-t) * q[i][1] + t * q[i+1][1]
return round(q[0][0]), round(q[0][1])
def bezier(b, samples):
return [ptOnCurve(b, i/samples) for i in range(samples+1)]
Use the bezier to stitch the wavy water polygon:
ts = []
for i in range(len(molecules)):
pa = molecules[max(0, i-1)]
pb = molecules[min(len(molecules)-1, i+1)]
ts.append((pb.y-pa.y) / (pb.x-pa.x))
pts = [(WIDTH, HEIGHT), (0, HEIGHT)]
for i in range(len(molecules)-1):
p0 = molecules[i].x, molecules[i].y
p3 = molecules[i+1].x, molecules[i+1].y
p1 = p0[0] + 10, p0[1] + 10 * ts[i]
p2 = p3[0] - 10, p3[1] - 10 * ts[i+1]
pts += bezier([p0, p1, p2, p3], 4)
Draw the polygon with pygame.draw.polygon():
pygame.draw.polygon(win, AQUA, pts)
Complete example:
from random import randint
import pygame
class Node:
def __init__(self, x, y, force, k, v):
self.x = x
self.y = y
self.y0 = y
self.force = force
self.k = k
self.v = v
self.direction = 1
def oscillate(self):
self.y += self.v * self.direction
if self.y0 - self.force / self.k > self.y or self.y0 + self.force / self.k < self.y:
self.direction *= -1
def draw(self, surf):
pygame.draw.circle(surf, "black", (self.x, self.y), 3)
window = pygame.display.set_mode((700, 500))
clock = pygame.time.Clock()
width, height = window.get_size()
no_of_nodes = 25
dx = width / no_of_nodes
nodes = [Node(i*dx, height//2, randint(15, 30), 1, 0.5) for i in range(no_of_nodes+1)]
def ptOnCurve(b, t):
q = b.copy()
for k in range(1, len(b)):
for i in range(len(b) - k):
q[i] = (1-t) * q[i][0] + t * q[i+1][0], (1-t) * q[i][1] + t * q[i+1][1]
return round(q[0][0]), round(q[0][1])
def bezier(b, samples):
return [ptOnCurve(b, i/samples) for i in range(samples+1)]
run = True
while run:
clock.tick(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
for molecule in nodes:
molecule.oscillate()
ts = []
for i in range(len(nodes)):
pa = nodes[max(0, i-1)]
pb = nodes[min(len(nodes)-1, i+1)]
ts.append((pb.y-pa.y) / (pb.x-pa.x))
pts = [(width, height), (0, height)]
for i in range(len(nodes)-1):
p0 = nodes[i].x, nodes[i].y
p3 = nodes[i+1].x, nodes[i+1].y
p1 = p0[0] + 10, p0[1] + 10 * ts[i]
p2 = p3[0] - 10, p3[1] - 10 * ts[i+1]
pts += bezier([p0, p1, p2, p3], 4)
window.fill("white")
pygame.draw.polygon(window, 'aqua', pts)
for molecule in nodes:
molecule.draw(window)
pygame.display.flip()
pygame.quit()
exit()
I'm writing app which draws analog clock based on a current time which python get by pytz and datetime. Everything works fine except shutting down a window of an app. It causes problem for windows when red X button is clicked (only when function is executed, when we select time zone). May it be caused by using nested loop?
import pygame
import sys
import math
import time
from datetime import datetime
import pytz
size = 500
pygame.init()
screen = pygame.display.set_mode((size, size))
pygame.display.set_caption("CLOCK")
font = pygame.font.SysFont("Arial", 20)
clock = pygame.time.Clock()
tz_London = pytz.timezone("Europe/London")
datetime_London = datetime.now(tz_London)
starting_hour_London = int(datetime_London.strftime("%H"))
# getting actual time
now = datetime.now()
starting_hour_Warsaw = int(now.strftime("%H"))
starting_minute = int(now.strftime("%M"))
starting_second = int(now.strftime("%S"))
angle_second = int(((starting_second/60) * 360) - 90)
angle_minute = ((starting_minute/60) * 360) - 90
angle_hour_Warsaw = None
angle_hour_London = None
if starting_hour_Warsaw in range(0, 12):
angle_hour_Warsaw = ((starting_hour_Warsaw/12) * 360 + (starting_minute * 0.5) + (starting_second * (1/120))) - 90
else:
angle_hour_Warsaw = (((starting_hour_Warsaw - 12) / 12) * 360 + (starting_minute * 0.5) + (starting_second * (1 / 120))) - 90
if starting_hour_London in range(0, 12):
angle_hour_London = ((starting_hour_London / 12) * 360 + (starting_minute * 0.5) + (starting_second * (1 / 120))) - 90
else:
angle_hour_London = (((starting_hour_London - 12) / 12) * 360 + (starting_minute * 0.5) + (starting_second * (1 / 120))) - 90
screen.fill((0, 0, 0))
def drawing_clock():
pygame.draw.circle(screen, (255, 0 ,0), (size/2, size/2), 200, 10)
lenght = 190
offset = 150
starting_pos = (size/2,size/2)
angles = [0, 90, 180, 270]
# Vertical and horizontal marks
for angle in angles:
x = starting_pos[0] + int(math.cos(math.radians(angle))) * lenght
y = starting_pos[1] + int(math.sin(math.radians(angle))) * lenght
pygame.draw.line(screen, (255, 0, 0), starting_pos, (x, y), 10)
for angle in angles:
x = starting_pos[0] + int(math.cos(math.radians(angle))) * offset
y = starting_pos[1] + int(math.sin(math.radians(angle))) * offset
pygame.draw.line(screen, (0, 0, 0), starting_pos, (x, y), 10)
# Angled marks
angles_2 = [30,60, 120, 150, 210, 240, 300, 330]
for angle in angles_2:
x = starting_pos[0] + math.cos(math.radians(angle)) * lenght
y = starting_pos[1] + math.sin(math.radians(angle)) * lenght
pygame.draw.line(screen, (255, 0, 0), starting_pos, (x, y), 5)
for angle in angles_2:
x = starting_pos[0] + math.cos(math.radians(angle)) * (offset+25)
y = starting_pos[1] + math.sin(math.radians(angle)) * (offset+25)
pygame.draw.line(screen, (0, 0, 0), starting_pos, (x, y), 10)
# Buttons
pygame.draw.circle(screen, (255, 0, 0), (size / 8, size / 8), 40, 5)
pygame.draw.circle(screen, (255, 0, 0), ((size / 8)*7, size / 8), 40, 5)
button_London = font.render("London", False, (255, 255, 255))
button_London_rect = button_London.get_rect(center = (size/8, size/8))
screen.blit(button_London, button_London_rect)
button_Warsaw = font.render("Warsaw", False, (255, 255, 255))
button_Warsaw_rect = button_London.get_rect(center = ((size/8)*7, size/8))
screen.blit(button_Warsaw, button_Warsaw_rect)
pygame.display.update()
def timezone():
tz = None
x, y = pygame.mouse.get_pos()
if tz == None:
if x > ((size/8)-20) and x < ((size/8)+20) and y > ((size/8)-20) and y < ((size/8)+20):
tz = angle_hour_London
elif x > (((size/8)*7)-20) and x < (((size/8)*7)+20) and y > ((size/8)-20) and y < ((size/8)+20):
tz = angle_hour_Warsaw
print(tz)
clock_hands(tz)
def clock_hands(tz):
global clock, angle_second, angle_minute, angle_hour_Warsaw, angle_hour_London
lenght_sec = 180
lenght_min = 150
lenght_hours = 120
start_pos = (size/2, size/2)
while True:
# Drawing each hand
offset = 150
angle_second += 6
x = start_pos[0] + math.cos(math.radians(angle_second)) * lenght_sec
y = start_pos[1] + math.sin(math.radians(angle_second)) * lenght_sec
pygame.draw.line(screen, (255, 0, 0), start_pos, (x, y), 5)
x_2 = start_pos[0] + math.cos(math.radians(angle_minute)) * lenght_min
y_2 = start_pos[1] + math.sin(math.radians(angle_minute)) * lenght_min
pygame.draw.line(screen, (0, 255, 0), start_pos, (x_2, y_2), 5)
x_3 = start_pos[0] + math.cos(math.radians(tz)) * lenght_hours
y_3 = start_pos[1] + math.sin(math.radians(tz)) * lenght_hours
pygame.draw.line(screen, (0, 0, 255), start_pos, (x_3, y_3), 5)
pygame.display.update()
time.sleep(1)
# Deleting outdated hands
# For second hand
if angle_second in [90, 180, 270, 360, 0]:
x_extra = start_pos[0] + math.cos(math.radians(angle_second)) * offset
y_extra = start_pos[1] + math.sin(math.radians(angle_second)) * offset
pygame.draw.line(screen, (0, 0, 0), start_pos, (x_extra, y_extra), 7)
pygame.display.update()
elif angle_second in [30, 60, 120, 150, 210, 240, 300, 330]:
x_extra = start_pos[0] + math.cos(math.radians(angle_second)) * (offset + 25)
y_extra = start_pos[1] + math.sin(math.radians(angle_second)) * (offset + 25)
pygame.draw.line(screen, (0, 0, 0), start_pos, (x_extra, y_extra), 7)
pygame.display.update()
else:
x_extra = start_pos[0] + math.cos(math.radians(angle_second)) * lenght_sec
y_extra = start_pos[1] + math.sin(math.radians(angle_second)) * lenght_sec
pygame.draw.line(screen, (0, 0, 0), start_pos, (x_extra, y_extra), 5)
pygame.display.update()
# For minute hand
pygame.draw.line(screen, (0, 0, 0), start_pos, (x_2, y_2), 5)
pygame.display.update()
angle_minute += 0.1
# For hour hand
pygame.draw.line(screen, (0, 0, 0), start_pos, (x_3, y_3), 5)
pygame.display.update()
tz += (30/3600)
if angle_second == 360:
angle_second = 0
drawing_clock()
while True:
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
timezone()
I have rewritten your code to merge the two while loops. The function clock_hands() now runs every iteration of the loop, however only increments the second hand if the new frame variable (which increments by 1 every loop and goes back to 0 every second) is 0, meaning that the second hand is only updated every second.
As well as the loop merge and the frame system, I have made it so that the clock is redrawn every loop, after the screen is made black, as an easier way of deleting the previous hands.
I hope this helps. If you have any questions ask them and I'll reply as soon as I can.
import pygame
import sys
import math
from datetime import datetime
import pytz
size = 500
pygame.init()
screen = pygame.display.set_mode((size, size))
pygame.display.set_caption("CLOCK")
font = pygame.font.SysFont("Arial", 20)
clock = pygame.time.Clock()
fps = 60
frame = -1
tz = None
tz_London = pytz.timezone("Europe/London")
datetime_London = datetime.now(tz_London)
starting_hour_London = int(datetime_London.strftime("%H"))
# getting actual time
now = datetime.now()
starting_hour_Warsaw = int(now.strftime("%H"))
starting_minute = int(now.strftime("%M"))
starting_second = int(now.strftime("%S"))
start_pos = (size / 2, size / 2)
angle_second = int(((starting_second/60) * 360) - 90)
angle_minute = ((starting_minute/60) * 360) - 90
angle_hour_Warsaw = None
angle_hour_London = None
if starting_hour_Warsaw in range(0, 12):
angle_hour_Warsaw = ((starting_hour_Warsaw/12) * 360 + (starting_minute * 0.5) + (starting_second * (1/120))) - 90
else:
angle_hour_Warsaw = (((starting_hour_Warsaw - 12) / 12) * 360 + (starting_minute * 0.5) + (starting_second * (1 / 120))) - 90
if starting_hour_London in range(0, 12):
angle_hour_London = ((starting_hour_London / 12) * 360 + (starting_minute * 0.5) + (starting_second * (1 / 120))) - 90
else:
angle_hour_London = (((starting_hour_London - 12) / 12) * 360 + (starting_minute * 0.5) + (starting_second * (1 / 120))) - 90
def drawing_clock():
pygame.draw.circle(screen, (255, 0, 0), (size/2, size/2), 200, 10)
lenght = 190
offset = 150
starting_pos = (size / 2, size / 2)
angles = [0, 90, 180, 270]
# Vertical and horizontal marks
for angle in angles:
x = starting_pos[0] + int(math.cos(math.radians(angle))) * lenght
y = starting_pos[1] + int(math.sin(math.radians(angle))) * lenght
pygame.draw.line(screen, (255, 0, 0), starting_pos, (x, y), 10)
for angle in angles:
x = starting_pos[0] + int(math.cos(math.radians(angle))) * offset
y = starting_pos[1] + int(math.sin(math.radians(angle))) * offset
pygame.draw.line(screen, (0, 0, 0), starting_pos, (x, y), 10)
# Angled marks
angles_2 = [30,60, 120, 150, 210, 240, 300, 330]
for angle in angles_2:
x = starting_pos[0] + math.cos(math.radians(angle)) * lenght
y = starting_pos[1] + math.sin(math.radians(angle)) * lenght
pygame.draw.line(screen, (255, 0, 0), starting_pos, (x, y), 5)
for angle in angles_2:
x = starting_pos[0] + math.cos(math.radians(angle)) * (offset+25)
y = starting_pos[1] + math.sin(math.radians(angle)) * (offset+25)
pygame.draw.line(screen, (0, 0, 0), starting_pos, (x, y), 10)
# Buttons
pygame.draw.circle(screen, (255, 0, 0), (size / 8, size / 8), 40, 5)
pygame.draw.circle(screen, (255, 0, 0), ((size / 8)*7, size / 8), 40, 5)
button_London = font.render("London", False, (255, 255, 255))
button_London_rect = button_London.get_rect(center = (size/8, size/8))
screen.blit(button_London, button_London_rect)
button_Warsaw = font.render("Warsaw", False, (255, 255, 255))
button_Warsaw_rect = button_London.get_rect(center = ((size/8)*7, size/8))
screen.blit(button_Warsaw, button_Warsaw_rect)
pygame.display.update()
def timezone():
x, y = pygame.mouse.get_pos()
if x > ((size/8)-20) and x < ((size/8)+20) and y > ((size/8)-20) and y < ((size/8)+20):
return angle_hour_London
elif x > (((size/8)*7)-20) and x < (((size/8)*7)+20) and y > ((size/8)-20) and y < ((size/8)+20):
return angle_hour_Warsaw
def clock_hands(tz, should_update_second_hand):
global clock, angle_second, angle_minute, angle_hour_Warsaw, angle_hour_London
lenght_sec = 180
lenght_min = 150
lenght_hours = 120
# Drawing each hand
offset = 150
if should_update_second_hand:
angle_second += 6
x = start_pos[0] + math.cos(math.radians(angle_second)) * lenght_sec
y = start_pos[1] + math.sin(math.radians(angle_second)) * lenght_sec
x_2 = start_pos[0] + math.cos(math.radians(angle_minute)) * lenght_min
y_2 = start_pos[1] + math.sin(math.radians(angle_minute)) * lenght_min
x_3 = start_pos[0] + math.cos(math.radians(tz)) * lenght_hours
y_3 = start_pos[1] + math.sin(math.radians(tz)) * lenght_hours
pygame.draw.line(screen, (255, 0, 0), start_pos, (x, y), 5)
pygame.draw.line(screen, (0, 255, 0), start_pos, (x_2, y_2), 5)
pygame.draw.line(screen, (0, 0, 255), start_pos, (x_3, y_3), 5)
if should_update_second_hand:
tz += (30/3600)
if angle_second == 360:
angle_second = 0
while True:
clock.tick(fps)
frame = (frame + 1) % fps
screen.fill((0, 0, 0))
drawing_clock()
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
tz = timezone()
if tz is not None:
clock_hands(tz, frame == 0)
pygame.display.update()
I'm not managing to get this math correct, and it's a little bit difficult to explain in words. I have managed to create a isometric grid, which you can select the tiles with the mouse perfectly, and I have managed to implement a camera movement using wasd keys and still get the tiles correctly selected, but there is a slightly bug which I can not figure out where is coming from.
This is what happens, but only sometimes, depend where the camera offset is:
when this happens, it is only on the x axis, and not in every tile.
I'm almost giving up on this cause I can't find the bug, thought of posting here to see if anyone had similar problem.
import time
import pygame
import sys
import math
from os import path
from settings import *
from sprites import *
# ------------------------- SETTINGS ---------------------------- #
# COLORS (r, g, b)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
DARKGREY = (40, 40, 40)
LIGHTGREY = (100, 100, 100)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
# game settings
WIDTH = 1024
HEIGHT = 768
FPS = 60
title = "Isometric-Based game"
BGCOLOUR = DARKGREY
TILE_X = 80
TILE_Y = 40
WORLD_X, WORLD_Y = 14, 10
ORIGIN_X, ORIGIN_Y = 5, 1
# Debug
pygame.init()
font = pygame.font.Font(None, 25)
CAMERA_SPEED = 300
def get_info(info_list):
display_surface = pygame.display.get_surface()
for i, key in enumerate(info_list):
text = font.render(str(key) + " : " + str(info_list[key]), True, (255, 255, 255), (0, 0, 0))
text_rect = text.get_rect()
text_rect.y = 20 * i
display_surface.blit(text, text_rect)
# ------------------------- SPRITES ---------------------------- #
class Camera:
def __init__(self, game, x, y):
self.game = game
self.x, self.y = self.game.to_screen(x, y)
self.vx, self.vy = 0, 0
def update(self):
self.get_keys()
self.x += self.vx * self.game.dt
self.y += self.vy * self.game.dt
def get_keys(self):
self.vx, self.vy = 0, 0
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
self.vy = -CAMERA_SPEED
if keys[pygame.K_s]:
self.vy = CAMERA_SPEED
if keys[pygame.K_a]:
self.vx = -CAMERA_SPEED
if keys[pygame.K_d]:
self.vx = CAMERA_SPEED
if self.vx != 0 and self.vy != 0:
self.vx *= 1.0
self.vy *= 0.50
class MouseSelection:
def __init__(self, game, image):
self.game = game
self.image = image
def update(self):
# get mouse x and y
self.mouse_x, self.mouse_y = pygame.mouse.get_pos()
# get the mouse offset position inside the tile
self.offset_x, self.offset_y = self.mouse_x % TILE_X, self.mouse_y % TILE_Y
self.offset_x += self.game.scroll_x % TILE_X # Add camera scroll to offset
self.offset_y += self.game.scroll_y % TILE_Y
# get the cell number
self.cell_x, self.cell_y = (self.mouse_x // TILE_X), (self.mouse_y // TILE_Y)
self.cell_x += int((self.game.scroll_x // TILE_X)) # Add camera scroll to cell
self.cell_y += int((self.game.scroll_y // TILE_Y))
# get the selected cell in iso grid
self.selected_x = (self.cell_y - ORIGIN_Y) + (self.cell_x - ORIGIN_X)
self.selected_y = (self.cell_y - ORIGIN_Y) - (self.cell_x - ORIGIN_X)
# height and width of a quarter of a tile, select the corner of the tile to nodge to a direction
h, w = TILE_Y / 2, TILE_X / 2
if self.offset_y < (h / w) * (w - self.offset_x):
self.selected_x -= 1
if self.offset_y > (h / w) * self.offset_x + h:
self.selected_y += 1
if self.offset_y < (h / w) * self.offset_x - h:
self.selected_y -= 1
if self.offset_y > (h / w) * (2 * w - self.offset_x) + h:
self.selected_x += 1
# translate the selected cell to world coordinate
self.selectedWorld_x, self.selectedWorld_y = self.game.to_screen(self.selected_x, self.selected_y)
def draw(self):
# Draw the selected tile with the camera scroll offset
self.game.screen.blit(self.image, (self.selectedWorld_x - self.game.scroll_x,
self.selectedWorld_y - self.game.scroll_y))
class SpriteSheet:
def __init__(self, image):
self.image = image
self.frames = []
def get_image(self):
for row in range(2):
for col in range(4):
if row == 0:
image = pygame.Surface((TILE_Y, TILE_Y / 2)).convert_alpha()
image.blit(self.image, (0, 0), (col * TILE_X / 2, row * TILE_Y / 2, TILE_X, TILE_Y))
image = pygame.transform.scale(image, (TILE_X, TILE_Y))
else:
image = pygame.Surface((TILE_Y, TILE_Y)).convert_alpha()
image.blit(self.image, (0, 0), (col * TILE_X / 2, row * TILE_Y / 2, TILE_X, TILE_Y * 2))
image = pygame.transform.scale(image, (TILE_X, TILE_Y * 2))
image.set_colorkey(WHITE)
self.frames.append(image)
return self.frames
# ------------------------- GAME LOOP ---------------------------- #
class Game:
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption(title)
self.clock = pygame.time.Clock()
pygame.key.set_repeat(400, 100)
self.debug = {}
self.sprite_sheet_image = pygame.image.load("isometric_whitebg - Copy.png")
self.index = 1
self.scroll_x, self.scroll_y = 0, 0
def new(self):
# initialize all variables and do all the setup for a new game
self.sprite_sheet = SpriteSheet(self.sprite_sheet_image)
self.tile_selected = self.sprite_sheet.get_image()[0]
self.tiles = self.sprite_sheet.get_image()
self.mouse_selection = MouseSelection(self, self.tile_selected)
self.camera = Camera(self, 1, 1)
def run(self):
# game loop - set self.playing = False to end the game
self.playing = True
while self.playing:
self.dt = self.clock.tick(FPS) / 1000
self.events()
self.update()
self.draw()
def quit(self):
pygame.quit()
sys.exit()
def update(self):
# update portion of the game loop
self.camera.update()
self.mouse_selection.update()
self.mx, self.my = pygame.mouse.get_pos()
# -------------------------------------------------- CAMERA SCROLLING ----------------------------------------#
if self.camera.x - self.scroll_x != WIDTH / 2:
self.scroll_x += (self.camera.x - (self.scroll_x + WIDTH / 2)) / 10
if self.camera.y - self.scroll_y != HEIGHT / 2:
self.scroll_y += (self.camera.y - (self.scroll_y + HEIGHT / 2)) / 10
# -------------------------------------------------- CAMERA SCROLLING ----------------------------------------#
self.debug_info()
def to_screen(self, x, y):
screen_x = (ORIGIN_X * TILE_X) + (x - y) * (TILE_X / 2)
screen_y = (ORIGIN_Y * TILE_Y) + (x + y) * (TILE_Y / 2)
return screen_x, screen_y
def draw_world(self):
for y in range(WORLD_Y):
for x in range(WORLD_X):
vWorld_x, vWorld_y = self.to_screen(x, y)
# Invisible tile
if self.index == 0:
self.screen.blit(self.tiles[1], (vWorld_x, vWorld_y))
# Grass
elif self.index == 1:
self.screen.blit(self.tiles[2], (vWorld_x - self.scroll_x, vWorld_y - self.scroll_y))
def draw(self):
self.screen.fill(BGCOLOUR)
self.draw_world()
self.mouse_selection.draw()
get_info(self.debug)
pygame.display.flip()
def debug_info(self):
self.debug["FPS"] = int(self.clock.get_fps())
self.debug["Cell"] = self.mouse_selection.cell_x, self.mouse_selection.cell_y
self.debug["Selected"] = int(self.mouse_selection.selected_x), int(self.mouse_selection.selected_y)
self.debug["Scroll"] = int(self.scroll_x), int(self.scroll_y)
self.debug["Mouse"] = int(self.mx), int(self.my)
self.debug["Mouse_offset"] = int(self.mouse_selection.offset_x), int(self.mouse_selection.offset_y)
def events(self):
# catch all events here
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
self.quit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
pass
game = Game()
while True:
game.new()
game.run()
Define the corner points of the map:
map_outline = [
pygame.math.Vector2(left_x, left_y),
pygame.math.Vector2(top_x, top_y),
pygame.math.Vector2(right_x, right_y,
pygame.math.Vector2(bottom_x, bottom_y)
]
With this information you can calculate the x and y axis of the map:
origin = map_outline[0]
x_axis = (map_outline[1] - map_outline[0]) / columns
y_axis = (map_outline[3] - map_outline[0]) / rows
You can use the x-axis and the y-axis to calculate a point in the map as a function of the row and column:
def transform(p, mat2x2):
x = p[0] * mat2x2[0][0] + p[1] * mat2x2[1][0]
y = p[0] * mat2x2[0][1] + p[1] * mat2x2[1][1]
return pygame.math.Vector2(x, y)
p_position = transform((column + 0.5, row + 0.5), (x_axis, y_axis)) + origin
If you want to get the row and column depending on the mouse cursor, you need to do the opposite. You need to calculate the inverse 2x2 matrix from the x and y axis. Using the inverse matrix, you can calculate the row and column as a function of a point on the map:
def inverseMat2x2(m):
a, b, c, d = m[0].x, m[0].y, m[1].x, m[1].y
det = 1 / (a*d - b*c)
return [(d*det, -b*det), (-c*det, a*det)]
m_pos = pygame.mouse.get_pos()
m_grid_pos = transform(pygame.math.Vector2(m_pos) - origin, point_to_grid)
m_col, m_row = int(m_grid_pos[0]), int(m_grid_pos[1])
Also see PyGameExamplesAndAnswers - Isometric
Minimal example:
replit.com/#Rabbid76/Pygame-IsometircMap
import pygame
pygame.init()
window = pygame.display.set_mode((500, 300))
clock = pygame.time.Clock()
colors = {'g': (40, 128, 40), 'd': (90, 60, 40)}
tilemap = [
'gdddg',
'dgddd',
'ggddg',
'ggddg',
'ddddg',
'dgggd'
]
columns, rows = len(tilemap[0]), len(tilemap)
isometric_tiles = {}
for key, color in colors.items():
tile_surf = pygame.Surface((50, 50), pygame.SRCALPHA)
tile_surf.fill(color)
tile_surf = pygame.transform.rotate(tile_surf, 45)
isometric_size = tile_surf.get_width()
tile_surf = pygame.transform.scale(tile_surf, (isometric_size, isometric_size//2))
isometric_tiles[key] = tile_surf
tile_size = (isometric_size, isometric_size//2)
def tileRect(column, row, tile_size):
x = (column + row) * tile_size[0] // 2
y = ((columns - column - 1) + row) * tile_size[1] // 2
return pygame.Rect(x, y, *tile_size)
game_map = pygame.Surface(((columns+rows) * isometric_size // 2, (columns+rows) * isometric_size // 4), pygame.SRCALPHA)
for column in range(columns):
for row in range(rows):
tile_surf = isometric_tiles[tilemap[row][column]]
tile_rect = tileRect(column, row, tile_size)
game_map.blit(tile_surf, tile_rect)
map_rect = game_map.get_rect(center = window.get_rect().center)
map_outline = [
pygame.math.Vector2(0, columns * isometric_size / 4),
pygame.math.Vector2(columns * isometric_size / 2, 0),
pygame.math.Vector2((columns+rows) * isometric_size // 2, rows * isometric_size / 4),
pygame.math.Vector2(rows * isometric_size / 2, (columns+rows) * isometric_size // 4)
]
for pt in map_outline:
pt += map_rect.topleft
origin = map_outline[0]
x_axis = (map_outline[1] - map_outline[0]) / columns
y_axis = (map_outline[3] - map_outline[0]) / rows
def inverseMat2x2(m):
a, b, c, d = m[0].x, m[0].y, m[1].x, m[1].y
det = 1 / (a*d - b*c)
return [(d*det, -b*det), (-c*det, a*det)]
point_to_grid = inverseMat2x2((x_axis, y_axis))
def transform(p, mat2x2):
x = p[0] * mat2x2[0][0] + p[1] * mat2x2[1][0]
y = p[0] * mat2x2[0][1] + p[1] * mat2x2[1][1]
return pygame.math.Vector2(x, y)
font = pygame.font.SysFont(None, 30)
textO = font.render("O", True, (255, 255, 255))
textX = font.render("X", True, (255, 0, 0))
textY = font.render("Y", True, (0, 255, 0))
p_col, p_row = 2, 2
run = True
while run:
clock.tick(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a and p_col > 0:
p_col -= 1
if event.key == pygame.K_d and p_col < columns-1:
p_col += 1
if event.key == pygame.K_w and p_row > 0:
p_row -= 1
if event.key == pygame.K_s and p_row < rows-1:
p_row += 1
p_position = transform((p_col + 0.5, p_row + 0.5), (x_axis, y_axis)) + origin
m_pos = pygame.mouse.get_pos()
m_grid_pos = transform(pygame.math.Vector2(m_pos) - origin, point_to_grid)
m_col, m_row = int(m_grid_pos[0]), int(m_grid_pos[1])
window.fill((0, 0, 0))
window.blit(game_map, map_rect)
pygame.draw.lines(window, (164, 164, 164), True, map_outline, 3)
pygame.draw.line(window, (255, 0, 0), origin, origin+x_axis, 3)
pygame.draw.line(window, (0, 255, 0), origin, origin+y_axis, 3)
pygame.draw.circle(window, (255, 255, 255), origin, 5)
pygame.draw.circle(window, (255, 0, 0), origin+x_axis, 5)
pygame.draw.circle(window, (0, 255, 0), origin+y_axis, 5)
window.blit(textO, textO.get_rect(topright = origin+(-5, 5)))
window.blit(textX, textX.get_rect(bottomright = origin+x_axis+(-5, -5)))
window.blit(textY, textX.get_rect(topright = origin+y_axis+(-5, 5)))
pygame.draw.ellipse(window, (255, 255, 0), (p_position[0]-16, p_position[1]-8, 32, 16))
if 0 <= m_grid_pos[0] < columns and 0 <= m_grid_pos[1] < rows:
tile_rect = tileRect(m_col, m_row, tile_size).move(map_rect.topleft)
pts = [tile_rect.midleft, tile_rect.midtop, tile_rect.midright, tile_rect.midbottom]
pygame.draw.lines(window, (255, 255, 255), True, pts, 4)
pygame.display.update()
pygame.quit()
exit()
I recently tried making a submarine game and made about 132 lines of code. I run the code and pressed the specified keys to move the submarine. Errors came up in the Shell that event doesn't have attribute keysm. Could anyone tell what I am supposed to replace event with or correct keysm?
Full Code:
from tkinter import *
from random import randint
from time import sleep, time
from math import sqrt
HEIGHT = 500
WIDTH = 800
window = Tk()
bub_id = list()
bub_r = list()
bub_speed = list()
MIN_BUB_R = 10
MAX_BUB_R = 30
MAX_BUB_SPD = 10
GAP = 100
window.title('Bubble Blaster')
c = Canvas(window, width=WIDTH, height=HEIGHT, bg='darkblue')
c.pack()
ship_id = c.create_polygon(5, 5, 5, 25, 30, 15, fill='red')
ship_id2 = c.create_oval(0, 0, 30, 30, outline='red')
SHIP_R = 15
MID_X = WIDTH / 2
MID_Y = HEIGHT / 2
SHIP_SPD = 10
BUB_CHANCE = 10
time_text = c.create_text(50, 50, fill='white')
score_text = c.create_text(150, 50, fill='white')
TIME_LIMIT = 30
BONUS_SCORE = 1000
score = 0
bonus = 0
end = time() + TIME_LIMIT
c.move(ship_id, MID_X, MID_Y)
c.move(ship_id2, MID_X, MID_Y)
def move_ship(event):
if event.keysm == 'Up':
c.move(ship_id, 0, -SHIP_SPD)
c.move(ship_id2, 0, -SHIP_SPD)
elif event.keysm == 'Down':
c.move(ship_id, 0, SHIP_SPD)
c.move(ship_id2, 0, SHIP_SPD)
elif event.keysm == 'Left':
c.move(ship_id2, -SHIP_SPD, 0)
c.move(ship_id2, -SHIP_SPD, 0)
elif event.keysm == 'Right':
c.move(ship_id, SHIP_SPD, 0)
c.move(ship_id2, SHIP_SPD, 0)
c.bind_all('<Key>', move_ship)
def create_bubble():
x = WIDTH + GAP
y = randint(0, HEIGHT)
r = randint(MIN_BUB_R, MAX_BUB_R)
id1 = c.create_oval(x - r, y - r, x + r, y + r, outline='white')
bub_id.append(id1)
bub_r.append(r)
bub_speed.append(randint(1, MAX_BUB_SPD))
def move_bubbles():
for i in range(len(bub_id)):
c.move(bub_id[i], -bub_speed[i], 0)
def get_coords(id_num):
pos = c.coords(id_num)
x = (pos[0] + pos[2])/2
y = (pos[1] + pos[3])/2
return x, y
def distance(id1, id2):
x1, y1 = get_coords(id1)
x2, y2 = get_coords(id2)
return sqrt((x2 - x1)**2 + (y2 - y1)**2)
def del_bubble(i):
del bub_r[i]
del bub_speed[i]
c.delete(bub_id[i])
del bub_id[i]
def collision():
points = 0
for bub in range(len(bub_id)-1, -1, -1):
if distance(ship_id2, bub_id[bub]) < (SHIP_R + bub_r[bub]):
points += (bub_r[bub] + bub_speed[bub])
del_bubble(bub)
return points
def show_score(score):
c.itemconfig(score_text, text=str(score))
def show_time(time_left):
c.itemconfig(time_text, text=str(time_left))
def clean_up_bubs():
for i in range(len(bub_id)-1, -1, -1):
x, y = get_coords(bub_id[i])
if x < -GAP:
del_bubble(i)
c.create_text(50, 30, text='TIME', fill='white')
c.create_text(150, 30, text='SCORE', fill='white')
#MAIN GAME LOOP
while time() < end:
if randint(1, BUB_CHANCE) == 1:
create_bubble()
move_bubbles()
clean_up_bubs()
score += collision()
if (int(score / BONUS_SCORE)) > bonus:
bonus += 1
end += TIME_LIMIT
show_score(score)
show_time(int(end - time()))
window.update()
sleep(0.01)
Since character movement is grid based, characters look a bit odd when going from square to square as they just appear from one square onto another. To make the movement feel more natural, I wanted to add "afterimages" so that there would be a simulation of smooth movement.
Demonstrational image:
Since my code has characters moving directly onto the next square, I don't know how to blit sprites in between.
if IDO.get_key(pygame.K_RIGHT):
if PhaseFocus == 0 or PhaseFocus == 2:
Reticlex +=1
if Currently_Selected != 0 and Currently_Selected.Phase == 2:
if Currently_Selected.x != Reticlex:
Currently_Selected.x = Reticlex
if Currently_Selected.x != Reticley:
Currently_Selected.y = Reticley
if IDO.get_key(pygame.K_LEFT):
if PhaseFocus == 0 or PhaseFocus == 2:
Reticlex -=1
if Currently_Selected != 0 and Currently_Selected.Phase == 2:
if Currently_Selected.x != Reticlex:
Currently_Selected.x = Reticlex
if Currently_Selected.x != Reticley:
Currently_Selected.y = Reticley
When the currently selected character is in Phase 2 (carried around) they should have these afterimages.
Let's see how we can do this.
Step 1: Basic setup
We start with an "empty" pygame game, like this:
import pygame
def main():
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
while True:
events = pygame.event.get()
for e in events:
if e.type == pygame.QUIT:
return
screen.fill((30, 30, 30))
pygame.display.flip()
clock.tick(60)
if __name__ == '__main__':
main()
We have a Clock, a single game loop and handle the QUIT event. I always store the events from event.get() in a variable in case I want to iterate over them again, e.g. when a Sprite wants to listen for an event. Always call event.get() only once per frame!
Step 2: A grid
We want a grid based game, so let's draw a grid. Also, we store a bunch of Rects in a list so it will be easy for us to lookup the screen coordinates of an object. We want to center objects in their tile, so using the Rect class will do the dirty work for us.
import pygame
TILESIZE = 32
GRID_W, GRID_H = (20, 15)
def create_grid():
surf = pygame.Surface((TILESIZE * GRID_W, TILESIZE * GRID_H))
surf.set_colorkey((2, 2, 2))
surf.fill((2, 2, 2))
grid = []
for y in range(GRID_H):
line = []
for x in range(GRID_W):
r = pygame.Rect(x * TILESIZE, y * TILESIZE, TILESIZE, TILESIZE)
line.append(r)
pygame.draw.rect(surf, pygame.Color('grey'), r, 1)
grid.append(line)
return grid, surf
def main():
screen = pygame.display.set_mode((TILESIZE * GRID_W, TILESIZE * GRID_H))
clock = pygame.time.Clock()
grid, grid_surf = create_grid()
while True:
events = pygame.event.get()
for e in events:
if e.type == pygame.QUIT:
return
screen.fill((30, 30, 30))
screen.blit(grid_surf, (0, 0))
pygame.display.flip()
clock.tick(60)
Step 3: Something's moving on the grid
We now create an actor that travels on this grid. In this case it's a simple Sprite that moves every second.
import pygame
import random
TILESIZE = 32
GRID_W, GRID_H = (20, 15)
LOOKUP = None
class Actor(pygame.sprite.Sprite):
def __init__(self, grid_pos):
super().__init__()
self.image = pygame.Surface((TILESIZE // 2, TILESIZE // 2))
self.rect = self.image.get_rect()
self.pos = pygame.Vector2()
self.update_pos(grid_pos)
self.image.fill(pygame.Color('dodgerblue'))
self.timeout = 1000
def update_pos(self, grid_pos):
self.grid_pos = grid_pos
self.rect.center = get_grid_rect(grid_pos).center
self.pos = pygame.Vector2(self.rect.topleft)
def move_random(self):
d = random.choice([-1, 1])
x, y = self.grid_pos
if random.randint(0, 2):
x += d
else:
y += d
self.update_pos((x, y))
def update(self, events, dt):
self.timeout -= dt
if self.timeout <= 0:
self.timeout = 1000
self.move_random()
def get_grid_rect(pos):
x, y = pos
return LOOKUP[y][x]
def create_grid():
surf = pygame.Surface((TILESIZE * GRID_W, TILESIZE * GRID_H))
surf.set_colorkey((2, 2, 2))
surf.fill((2, 2, 2))
grid = []
for y in range(GRID_H):
line = []
for x in range(GRID_W):
r = pygame.Rect(x * TILESIZE, y * TILESIZE, TILESIZE, TILESIZE)
line.append(r)
pygame.draw.rect(surf, pygame.Color('grey'), r, 1)
grid.append(line)
return grid, surf
def main():
screen = pygame.display.set_mode((TILESIZE * GRID_W, TILESIZE * GRID_H))
dt, clock = 0, pygame.time.Clock()
grid, grid_surf = create_grid()
global LOOKUP
LOOKUP = grid
sprites = pygame.sprite.Group(Actor((9, 6)))
while True:
events = pygame.event.get()
for e in events:
if e.type == pygame.QUIT:
return
sprites.update(events, dt)
screen.fill((30, 30, 30))
screen.blit(grid_surf, (0, 0))
sprites.draw(screen)
pygame.display.flip()
dt = clock.tick(60)
if __name__ == '__main__':
main()
Step 4: Smooth movement
Instead of always drawing the Actor that the center of the tiles, we set a target_grid_pos when we want to move. Each frame, we move the Actor a little bit until we reached our target tile. We use pygame's Vector2 class and it's lerp and distance_to methods.
import pygame
import random
TILESIZE = 32
GRID_W, GRID_H = (20, 15)
LOOKUP = None
class Actor(pygame.sprite.Sprite):
def __init__(self, grid_pos):
super().__init__()
self.image = pygame.Surface((TILESIZE // 2, TILESIZE // 2))
self.rect = self.image.get_rect()
self.pos = pygame.Vector2()
self.update_pos(grid_pos)
self.image.fill(pygame.Color('dodgerblue'))
self.timeout = 1000
def update_pos(self, grid_pos):#
self.target_pos = None
self.target_grid_pos = None
self.grid_pos = grid_pos
self.rect.center = get_grid_rect(grid_pos).center
self.pos = pygame.Vector2(self.rect.center)
def move_random(self):
d = random.choice([-1, 1])
x, y = self.grid_pos
if random.randint(0, 2):
x += d
else:
y += d
self.target_pos = pygame.Vector2(get_grid_rect((x, y)).center)
self.target_grid_pos = (x, y)
def update(self, events, dt):
self.timeout -= dt
if self.timeout <= 0:
self.timeout = 1000
self.move_random()
if self.target_grid_pos:
self.pos = self.pos.lerp(self.target_pos, 0.1)
if self.pos.distance_to(self.target_pos) < 1:
self.update_pos(self.target_grid_pos)
self.rect.center = self.pos
def get_grid_rect(pos):
x, y = pos
return LOOKUP[y][x]
def create_grid():
surf = pygame.Surface((TILESIZE * GRID_W, TILESIZE * GRID_H))
surf.set_colorkey((2, 2, 2))
surf.fill((2, 2, 2))
grid = []
for y in range(GRID_H):
line = []
for x in range(GRID_W):
r = pygame.Rect(x * TILESIZE, y * TILESIZE, TILESIZE, TILESIZE)
line.append(r)
pygame.draw.rect(surf, pygame.Color('grey'), r, 1)
grid.append(line)
return grid, surf
def main():
screen = pygame.display.set_mode((TILESIZE * GRID_W, TILESIZE * GRID_H))
dt, clock = 0, pygame.time.Clock()
grid, grid_surf = create_grid()
global LOOKUP
LOOKUP = grid
sprites = pygame.sprite.Group(Actor((9, 6)))
while True:
events = pygame.event.get()
for e in events:
if e.type == pygame.QUIT:
return
sprites.update(events, dt)
screen.fill((30, 30, 30))
screen.blit(grid_surf, (0, 0))
sprites.draw(screen)
pygame.display.flip()
dt = clock.tick(60)
if __name__ == '__main__':
main()
Step 5: Adding the effect
Every 300 ms, we create a copy of the Actor's image, blit it at the Actor's position, and make sure to delete it after 200ms. In a real game, you probably want to cache the images or use pre-rendered ones.
I switched the Group for LayeredUpdates so we can make sure the original images is always drawn above the "shadows".
I also added an image (from rltiles) instead of a simple rect so it looks nicer in this demo.
import pygame
import random
import gzip
import base64
TILESIZE = 64
GRID_W, GRID_H = (10, 7)
LOOKUP = None
class Shadow(pygame.sprite.Sprite):
def __init__(self, source):
super().__init__()
self._layer = 5
self.image = source.image.copy().convert_alpha()
self.image.fill((0, 0, 200, 100), special_flags=pygame.BLEND_ADD)
self.rect = source.rect.copy()
self.timeout = 200
def update(self, events, dt):
self.timeout -= dt
if self.timeout <= 0:
self.kill()
class Actor(pygame.sprite.Sprite):
def __init__(self, grid_pos):
super().__init__()
self._layer = 10
data, size = gzip.decompress(base64.b64decode(HYDRA)), (64, 64)
self.image = pygame.image.fromstring(data, size, "RGB")
self.image.set_colorkey((71, 108, 108))
self.rect = self.image.get_rect()
self.pos = pygame.Vector2()
self.update_pos(grid_pos)
self.timeout = 1000
self.shadow_timeout = 100
def update_pos(self, grid_pos):#
self.target_pos = None
self.target_grid_pos = None
self.grid_pos = grid_pos
self.rect.center = get_grid_rect(grid_pos).center
self.pos = pygame.Vector2(self.rect.center)
def move_random(self):
d = random.choice([-1, 1])
x, y = self.grid_pos
if random.randint(0, 2):
x += d
else:
y += d
self.target_pos = pygame.Vector2(get_grid_rect((x, y)).center)
self.target_grid_pos = (x, y)
def update(self, events, dt):
self.timeout -= dt
if self.timeout <= 0:
self.timeout = 1000
self.move_random()
if self.target_grid_pos:
self.shadow_timeout -= dt
if self.shadow_timeout <= 0:
self.shadow_timeout = 100
self.groups()[0].add(Shadow(self))
self.pos = self.pos.lerp(self.target_pos, 0.1)
if self.pos.distance_to(self.target_pos) < 1:
self.update_pos(self.target_grid_pos)
self.rect.center = self.pos
def get_grid_rect(pos):
x, y = pos
return LOOKUP[y][x]
def create_grid():
surf = pygame.Surface((TILESIZE * GRID_W, TILESIZE * GRID_H))
surf.set_colorkey((2, 2, 2))
surf.fill((2, 2, 2))
grid = []
for y in range(GRID_H):
line = []
for x in range(GRID_W):
r = pygame.Rect(x * TILESIZE, y * TILESIZE, TILESIZE, TILESIZE)
line.append(r)
pygame.draw.rect(surf, pygame.Color('grey'), r, 1)
grid.append(line)
return grid, surf
def main():
screen = pygame.display.set_mode((TILESIZE * GRID_W, TILESIZE * GRID_H))
dt, clock = 0, pygame.time.Clock()
grid, grid_surf = create_grid()
global LOOKUP
LOOKUP = grid
sprites = pygame.sprite.LayeredUpdates(Actor((4, 3)))
while True:
events = pygame.event.get()
for e in events:
if e.type == pygame.QUIT:
return
sprites.update(events, dt)
screen.fill((30, 30, 30))
screen.blit(grid_surf, (0, 0))
sprites.draw(screen)
pygame.display.flip()
dt = clock.tick(60)
HYDRA = 'H4sIAEVFLF0C/+16Z2xk15Xmea8CK5LFKrJYOeecc85VJItkkaxizjl0ZOeWWq2WrNCSW61kW5IlB8lKli3ZLdnWjlfQAl57vTuDhWeA3QHs/THwzGCwwGLmhweY/bXnkrJ2VyPbrVYL2B9LHBSq2Y/vfffc757zffe+6t5e9YuL3d3y9nZhbQ2jsrNT2d3949dXDq/Pr64eXV/eIn+b21jLHtvKHdvMra8W19cq23/6Pnct9vcz8wvuXN6VzmQXFhHSHxkpfpa3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