How to draw a line in pygame that moves up the screen - python

I have made a python game, and when you press space it shoots a bullet (a line). It is only able to shoot once though. I was wondering how I could make it shoot multiple times?
shotStartX = x + 30
shotStartY = y + 20
shotEndX = x + 30
shotEndY = y
shoot = False
while True:
if event.type == pygame.KEYDOWN:
elif event.key == pygame.K_SPACE:
shoot = True
gameDisplay.fill(blue)
if shoot:
pygame.draw.line(gameDisplay,red,(shotStartX,shotStartY),(shotEndX,shotEndY),5)
shotStartY -= 10
shotEndY -= 10
gameDisplay.blit(rocketImg,(x,y))
pygame.display.update()
clock.tick(30)

I would create two classes, bullet and bullets and limit the firing rate to a certain time. Try this:
import pygame
import time
# define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
W = 480
H = 720
FPS = 60
BULLET_SPEED = 5
BULLET_FIRING_RATE = 0.5 # seconds
class bullet:
def __init__(s, x, y): # x, y = starting point
s.x = x
s.y = y
s.dead = False
def draw(s):
global DS
global WHITE
pygame.draw.circle(DS, WHITE, (s.x, s.y), 10)
def move(s):
global BULLET_SPEED
s.y -= BULLET_SPEED
if s.y <= 0:
s.dead = True
class bullets:
def __init__(s):
s.container = []
s.lastBulletTime = time.time() -BULLET_FIRING_RATE
def add(s, x, y):
global BULLET_FIRING_RATE
now = time.time()
if now - s.lastBulletTime >= BULLET_FIRING_RATE:
s.container.append(bullet(x, y))
s.lastBulletTime = now
def do(s):
deadBullets = []
for b in s.container:
b.draw()
b.move()
if b.dead: deadBullets.append(b)
for db in deadBullets:
s.container.remove(db)
pygame.init()
DS = pygame.display.set_mode((W, H))
CLOCK = pygame.time.Clock()
BULLETS = bullets()
# press escape to exit
while True:
e = pygame.event.get()
if pygame.key.get_pressed()[pygame.K_ESCAPE]: break
mx, my = pygame.mouse.get_pos()
mb = pygame.mouse.get_pressed()
if mb[0]:
BULLETS.add(mx, my)
DS.fill(BLACK)
BULLETS.do()
pygame.draw.circle(DS, WHITE, (mx, my), 40)
pygame.display.update()
CLOCK.tick(FPS)
pygame.quit()

Related

Is it possible to drag two sprites independently using FINGERDOWN in pygame on mobile?

I'm creating a pong style game for android where the paddles are controlled by dragging the touchscreen. The idea is that 2 players can play on the same device, each controlling a paddle with the touchscreen. Is it possible to drag 2 sprites to different locations at the same time using the touch events from pygame 2.0?
I've been able to move the paddles one at a time using the FINGERDOWN/FINGERMOTION/FINGERUP events but can't figure out how to use multitouch to control the paddles independantly from one another at the same time. If I drag both paddles they seem to both follow the coordinates of one FINGERDOWN event.
Here is my code, I hope the formatting is ok, it's the first time I'm posting on the site.
GIF example showing problem
import sys
import pygame
from pygame.locals import *
class Player(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = pygame.Surface((200,200))
self.image.fill(WHITE)
self.rect = self.image.get_rect(center = (x, y))
self.drag = False
def update(self, events):
for e in events:
if e.type == FINGERDOWN:
for finger, pos in fingers.items():
if self.rect.collidepoint(pos):
self.drag = True
self.mx, self.my = pos
self.offx = self.rect.x - self.mx
self.offy = self.rect.y - self.my
if e.type == FINGERMOTION:
if self.drag:
for finger, pos in fingers.items():
self.mx, self.my = pos
self.rect.y = self.my + self.offy
if e.type == FINGERUP:
self.drag = False
def handle_events():
global events
for event in events:
if pygame.event == QUIT:
pygame.quit()
sys.exit()
if event.type == VIDEORESIZE:
DW, DH = event.w, event.h
if event.type == FINGERDOWN:
x = int(event.x * DS.get_width())
y = int(event.y * DS.get_height())
fingers[event.finger_id] = x, y
if event.type == FINGERMOTION:
x = int(event.x * DS.get_width())
y = int(event.y * DS.get_height())
fingers[event.finger_id] = x, y
if event.type == FINGERUP:
fingers.pop(event.finger_id, None)
pygame.init()
#display settings
DW = 0
DH = 0
DS = pygame.display.set_mode((DW, DH), pygame.FULLSCREEN | pygame.RESIZABLE)
DW, DH = DS.get_size()
FPS = 60
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
#player intialisation
p = Player(0 + 100, DH/2)
p2 = Player(DW - 100, DH/2)
sprites = pygame.sprite.Group()
sprites.add(p, p2)
fingers = {}
clock = pygame.time.Clock()
while True:
events = pygame.event.get()
handle_events()
DS.fill(BLACK)
sprites.draw(DS)
sprites.update(events)
pygame.display.update()
clock.tick(FPS)
The events FINGERDOWN, FINGERUP and FINGERMOTION give you the finger_id (also see How to use Pygame touch events in a mobile game?).
You must store the ID of the finger touching the object in an attribute. Drag the object only when the finger with the corresponding ID is moved:
class Player(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = pygame.Surface((200,200))
self.image.fill(WHITE)
self.rect = self.image.get_rect(center = (x, y))
self.drag = False
self.finger_id = None
def update(self, events):
for e in events:
if e.type == FINGERDOWN:
for finger, pos in fingers.items():
if self.rect.collidepoint(pos):
self.finger_id = finger
self.drag = True
self.mx, self.my = pos
self.offx = self.rect.x - self.mx
self.offy = self.rect.y - self.my
if e.type == FINGERMOTION:
if self.drag:
for finger, pos in fingers.items():
if finger == self.finger_id:
self.mx, self.my = pos
self.rect.y = self.my + self.offy
if e.type == FINGERUP:
if not self.finger_id in fingers:
self.drag = False
Actually you don't need the the global fingers map at all
import sys
import pygame
from pygame.locals import *
class Player(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = pygame.Surface((200, 200))
self.image.fill("red")
self.rect = self.image.get_rect(center = (x, y))
self.drag = False
self.finger_id = None
def update(self, events):
for e in events:
if e.type == FINGERDOWN:
x = int(e.x * DS.get_width())
y = int(e.y * DS.get_height())
if self.rect.collidepoint((x, y)):
self.finger_id = e.finger_id
self.drag = True
self.mx, self.my = x, y
self.offx = self.rect.x - self.mx
self.offy = self.rect.y - self.my
if e.type == FINGERMOTION:
if self.drag and self.finger_id == e.finger_id:
x = int(e.x * DS.get_width())
y = int(e.y * DS.get_height())
self.mx, self.my = x, y
self.rect.y = self.my + self.offy
if e.type == FINGERUP:
if self.finger_id == e.finger_id:
self.drag = False
def handle_events(events):
for event in events:
if event.type == VIDEORESIZE:
DW, DH = event.w, event.h
pygame.init()
#display settings
DW = 0
DH = 0
#DS = pygame.display.set_mode((DW, DH), pygame.FULLSCREEN | pygame.RESIZABLE)
DS = pygame.display.set_mode((640, 480))
DW, DH = DS.get_size()
FPS = 60
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
#player intialisation
p = Player(0 + 100, DH/2)
p2 = Player(DW - 100, DH/2)
sprites = pygame.sprite.Group()
sprites.add(p, p2)
clock = pygame.time.Clock()
run = True
while run:
events = pygame.event.get()
run = not any(event for event in events if event.type == QUIT)
handle_events(events)
DS.fill(BLACK)
sprites.draw(DS)
sprites.update(events)
pygame.display.update()
clock.tick(FPS)
pygame.quit()
sys.exit()

How to Move Sprite to a Specific Location Without Keys

Im trying to make a game with pygame where I can click a sprite then click somewhere on the screen for the sprite to move towards. So far, I'm able to click the sprite and get a response but I'm not sure how to tell the sprite to go to a given location where I click. I've seen something online with sprite.goal but I can't seem to make it work.
This is what I have
if event.type==pygame.MOUSEBUTTONDOWN:
pos=pygame.mouse.get_pos()
#White is a rectangle
if White.collidepoint(pos):
Moving=True
elif Moving==True:
#This is where I would tell it to move to pos
I'll show you a very simple example. Write a function that moves a point 1 step to a target point and returns the new position:
def move_to_target(pos, target):
x, y = pos
if x < target[0]:
x += 1
elif x > target[0]:
x -= 1
if y < target[1]:
y += 1
elif y > target[1]:
y -= 1
return (x, y)
Set a new target when the mouse button is pressed and call the function at each frame:
import pygame
pygame.init()
window = pygame.display.set_mode((400, 400))
clock = pygame.time.Clock()
def move_to_target(pos, target):
x, y = pos
if x < target[0]:
x += 1
elif x > target[0]:
x -= 1
if y < target[1]:
y += 1
elif y > target[1]:
y -= 1
return (x, y)
my_sprite = pygame.Surface((20, 20), pygame.SRCALPHA)
pygame.draw.circle(my_sprite, (255, 255, 0), (10, 10), 10)
pos = (200, 200)
target = (200, 200)
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
target = event.pos
pos = move_to_target(pos, target)
window.fill(0)
window.blit(my_sprite, pos)
pygame.display.flip()
clock.tick(100)
pygame.quit()
exit()
For variable speed and a straighter and smoother movement, you need to tweak the function. See How to make smooth movement in pygame.
import pygame
pygame.init()
window = pygame.display.set_mode((400, 400))
clock = pygame.time.Clock()
LERP_FACTOR = 0.05
minimum_distance = 0
maximum_distance = 100
def move_to_target(pos, target):
target_vector = pygame.math.Vector2(*target)
follower_vector = pygame.math.Vector2(*pos)
new_follower_vector = pygame.math.Vector2(*pos)
distance = follower_vector.distance_to(target_vector)
if distance > minimum_distance:
direction_vector = (target_vector - follower_vector) / distance
min_step = max(0, distance - maximum_distance)
max_step = distance - minimum_distance
step_distance = min_step + (max_step - min_step) * LERP_FACTOR
new_follower_vector = follower_vector + direction_vector * step_distance
return (new_follower_vector.x, new_follower_vector.y)
my_sprite = pygame.Surface((20, 20), pygame.SRCALPHA)
pygame.draw.circle(my_sprite, (255, 255, 0), (10, 10), 10)
pos = (200, 200)
target = (200, 200)
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
target = event.pos
pos = move_to_target(pos, target)
window.fill(0)
window.blit(my_sprite, pos)
pygame.display.flip()
clock.tick(100)
pygame.quit()
exit()

Why are the trees not displaying?

I want to add the tree sprite to the sprite group named tree_group
I expect the distance of the trees spawning is calculated like this: if 800 - (tree.x + TREE_HEIGHT) > Tree.get_distance(score): tree_group.draw(tree)
For some reason, the trees are not appearing and I believe that this is because the tree sprite is not in the group yet, that is why I want to use the add() function.
I want the draw_display function to iterate over all the trees currently on the screen and move them using the get_speed equation.
My code is this:
import pygame
import sys
import random
import os
import time
from pygame.event import post
# initializes pygame libraries
pygame.init()
pygame.font.init()
pygame.mixer.init()
score = 0
FPS = 60
VEL = 5
# region | main window attributes and background
WIDTH, HEIGHT = 800, 800
display = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Ski Game")
BACKGROUND = pygame.transform.scale(pygame.image.load(os.path.join("background.jpg")), (WIDTH, HEIGHT))
# endregion
# region | fonts and colors
SCORE_FONT = pygame.font.SysFont('comicsans', 40)
GAME_OVER_FONT = pygame.font.SysFont('comicsans', 100)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
# endregion
# region | linking sprites and resizing then
SKIER_WIDTH = 65
SKIER_HEIGHT = 105
SKIER = pygame.transform.scale(pygame.image.load(
os.path.join("skier.png")), (SKIER_WIDTH, SKIER_HEIGHT))
# endregion
clock = pygame.time.Clock()
done = False
# create a custom event
CRASH_TREE = pygame.USEREVENT + 1
def draw_display(score, skier):
# blits the background
display.blit(BACKGROUND, (0, 0))
score_text = SCORE_FONT.render("Score: " + str(score), 1, BLACK)
display.blit(score_text, (WIDTH / 2 - score_text.get_width()/2, 10))
# blit skier
display.blit(SKIER, (skier.x, skier.y))
# blit trees
tree = Tree(score)
tree_group = pygame.sprite.Group()
tree_group.draw(display)
if score == 0:
tree_group.add(tree)
score += 1
elif 800 - (tree.rect.x + 150) > tree.get_distance(score):
tree_group.add(tree)
score += 1
pygame.display.update()
class Tree(pygame.sprite.Sprite):
def __init__(self, score):
super().__init__()
self.TREE_WIDTH = 80
self.TREE_HEIGHT = 150
self.image = pygame.transform.scale(pygame.image.load(os.path.join("tree.png")), (self.TREE_WIDTH, self.TREE_HEIGHT))
self.rect = self.image.get_rect()
self.rect.x = random.randrange(0, WIDTH)
self.rect.y = 700
print("init")
def get_speed(self, score, base=2, interval=10, multiplier=0.05):
return base + ((score//interval) * multiplier)
def get_distance(self, score, base=100, interval=10, multiplier=5):
return base + ((score//interval) * multiplier)
def update(self):
self.rect.y -= self.get_speed(score)
print("update")
def handle_skier_movement(keys_pressed, skier):
if keys_pressed[pygame.K_LEFT] and skier.x - VEL > 0: # LEFT
skier.x -= VEL
if keys_pressed[pygame.K_RIGHT] and skier.x + VEL + skier.width < WIDTH: # RIGHT
skier.x += VEL
if keys_pressed[pygame.K_UP] and skier.y - VEL > 0: # UP
skier.y -= VEL
if keys_pressed[pygame.K_DOWN] and skier.y + VEL + skier.width < WIDTH: # DOWN
skier.y += VEL
def game_over():
game_over_text = GAME_OVER_FONT.render("GAME OVER", 1, BLACK)
display.blit(game_over_text, (WIDTH/2 - game_over_text.get_width() /
2, HEIGHT/2 - game_over_text.get_height()/2))
pygame.display.update()
pygame.time.delay(5000)
def main():
skier = pygame.Rect(700, 300, SKIER_WIDTH, SKIER_HEIGHT)
score = 0
clock = pygame.time.Clock()
run = True
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
keys_pressed = pygame.key.get_pressed()
handle_skier_movement(keys_pressed, skier)
draw_display(score, skier)
tree = Tree(score)
tree.update()
if event.type == CRASH_TREE:
game_over()
score = 0
break
if __name__ == "__main__":
main()
In your draw_display function, you are first drawing the group and then adding the elements. That way, you are only drawing an empty group, and thus nothing happens. You should therefore place the tree_group.draw(display) line after the elif statement, right before pygame.display.update() line.

get position of sprite, and move it in response to it being clicked on [duplicate]

I'm making a chess game, but I'm completely stuck on the drag and drop element, there's a few guides out there but they're all either dragging shapes, or only dragging one image.
I've tried several variants of code, but all were 50+ lines just to move one .png and most were incredibly inefficient
pygame.init()
pygame.display.set_caption("Python Chess")
clock = pygame.time.Clock()
red = (213,43,67)
chew = pygame.image.load("chew.png")
gameDisplay.fill(red)
gameDisplay.blit(chew, (400, 400))
pygame.display.update()
drag = 0
if pygame.MOUSEBUTTONDOWN:
drag = 1
if pygame.MOUSEBUTTONUP:
drag = 0
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
Image simply doesn't drag.
Let's walk through this step by step.
Step 1: Let's start with a basic skeleton of every pygame game:
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(pygame.Color('grey'))
pygame.display.flip()
clock.tick(60)
if __name__ == '__main__':
main()
We create a window and then start a loop to listen for events and drawing the window.
So far, so good. Nothing to see here, let's move on.
Step 2: a chess board
So, we want a chess game. So we need a board. We create a list of lists to represent our board, and we create a Surface that draws our board on the screen. We want to always seperate our game's state from the actual drawing functions, so we create a board variable and a board_surf.
import pygame
TILESIZE = 32
def create_board_surf():
board_surf = pygame.Surface((TILESIZE*8, TILESIZE*8))
dark = False
for y in range(8):
for x in range(8):
rect = pygame.Rect(x*TILESIZE, y*TILESIZE, TILESIZE, TILESIZE)
pygame.draw.rect(board_surf, pygame.Color('black' if dark else 'white'), rect)
dark = not dark
dark = not dark
return board_surf
def create_board():
board = []
for y in range(8):
board.append([])
for x in range(8):
board[y].append(None)
return board
def main():
screen = pygame.display.set_mode((640, 480))
board = create_board()
board_surf = create_board_surf()
clock = pygame.time.Clock()
while True:
events = pygame.event.get()
for e in events:
if e.type == pygame.QUIT:
return
screen.fill(pygame.Color('grey'))
screen.blit(board_surf, (0, 0))
pygame.display.flip()
clock.tick(60)
if __name__ == '__main__':
main()
Step 3: Where's the mouse?
We need to know which piece we want to select, so we have to translate the screen coordinates (where's the mouse relative to the window?) to the world coordinates (which square of the board is the mouse pointing to?).
So if the board is not located at the origin (the position (0, 0)), we also have to take this offset into account.
Basically, we have to substract that offset (which is the position of the board on the screen) from the mouse position (so we have the mouse position relative to the board), and divide by the size of the squares.
To see if this works, let's draw a red rectangle on the selected square.
import pygame
TILESIZE = 32
BOARD_POS = (10, 10)
def create_board_surf():
board_surf = pygame.Surface((TILESIZE*8, TILESIZE*8))
dark = False
for y in range(8):
for x in range(8):
rect = pygame.Rect(x*TILESIZE, y*TILESIZE, TILESIZE, TILESIZE)
pygame.draw.rect(board_surf, pygame.Color('black' if dark else 'white'), rect)
dark = not dark
dark = not dark
return board_surf
def create_board():
board = []
for y in range(8):
board.append([])
for x in range(8):
board[y].append(None)
return board
def get_square_under_mouse(board):
mouse_pos = pygame.Vector2(pygame.mouse.get_pos()) - BOARD_POS
x, y = [int(v // TILESIZE) for v in mouse_pos]
try:
if x >= 0 and y >= 0: return (board[y][x], x, y)
except IndexError: pass
return None, None, None
def main():
screen = pygame.display.set_mode((640, 480))
board = create_board()
board_surf = create_board_surf()
clock = pygame.time.Clock()
while True:
events = pygame.event.get()
for e in events:
if e.type == pygame.QUIT:
return
piece, x, y = get_square_under_mouse(board)
screen.fill(pygame.Color('grey'))
screen.blit(board_surf, BOARD_POS)
if x != None:
rect = (BOARD_POS[0] + x * TILESIZE, BOARD_POS[1] + y * TILESIZE, TILESIZE, TILESIZE)
pygame.draw.rect(screen, (255, 0, 0, 50), rect, 2)
pygame.display.flip()
clock.tick(60)
if __name__ == '__main__':
main()
Step 4: Let's draw some pieces
Chess is boring without some pieces to move around, so let's create some pieces.
I just use a SysFont to draw some text instead of using real images, so everyone can just copy/paste the code and run it immediately.
We store a tuple (color, type) in the nested board list. Also, let's use some other colors for our board.
import pygame
TILESIZE = 32
BOARD_POS = (10, 10)
def create_board_surf():
board_surf = pygame.Surface((TILESIZE*8, TILESIZE*8))
dark = False
for y in range(8):
for x in range(8):
rect = pygame.Rect(x*TILESIZE, y*TILESIZE, TILESIZE, TILESIZE)
pygame.draw.rect(board_surf, pygame.Color('darkgrey' if dark else 'beige'), rect)
dark = not dark
dark = not dark
return board_surf
def get_square_under_mouse(board):
mouse_pos = pygame.Vector2(pygame.mouse.get_pos()) - BOARD_POS
x, y = [int(v // TILESIZE) for v in mouse_pos]
try:
if x >= 0 and y >= 0: return (board[y][x], x, y)
except IndexError: pass
return None, None, None
def create_board():
board = []
for y in range(8):
board.append([])
for x in range(8):
board[y].append(None)
for x in range(0, 8):
board[1][x] = ('black', 'pawn')
for x in range(0, 8):
board[6][x] = ('white', 'pawn')
return board
def draw_pieces(screen, board, font):
for y in range(8):
for x in range(8):
piece = board[y][x]
if piece:
color, type = piece
s1 = font.render(type[0], True, pygame.Color(color))
s2 = font.render(type[0], True, pygame.Color('darkgrey'))
pos = pygame.Rect(BOARD_POS[0] + x * TILESIZE+1, BOARD_POS[1] + y * TILESIZE + 1, TILESIZE, TILESIZE)
screen.blit(s2, s2.get_rect(center=pos.center).move(1, 1))
screen.blit(s1, s1.get_rect(center=pos.center))
def draw_selector(screen, piece, x, y):
if piece != None:
rect = (BOARD_POS[0] + x * TILESIZE, BOARD_POS[1] + y * TILESIZE, TILESIZE, TILESIZE)
pygame.draw.rect(screen, (255, 0, 0, 50), rect, 2)
def main():
pygame.init()
font = pygame.font.SysFont('', 32)
screen = pygame.display.set_mode((640, 480))
board = create_board()
board_surf = create_board_surf()
clock = pygame.time.Clock()
while True:
events = pygame.event.get()
for e in events:
if e.type == pygame.QUIT:
return
piece, x, y = get_square_under_mouse(board)
screen.fill(pygame.Color('grey'))
screen.blit(board_surf, BOARD_POS)
draw_pieces(screen, board, font)
draw_selector(screen, piece, x, y)
pygame.display.flip()
clock.tick(60)
if __name__ == '__main__':
main()
Step 5: Drag'n'Drop
For drag and drop we need two things:
we have to change the state of your game (going into the "drag-mode")
eventhandling to enter and leave the "drag-mode"
It's actually not that complicated. To enter the "drag-mode", we just set a variable (selected_piece) when the MOUSEBUTTONDOWN event occurs. Since we already have the get_square_under_mouse function, it's easy to know if there's actually a piece under the mouse cursor.
if selected_piece is set, we draw a line and the piece under the mouse cursor, and we keep track of the current square under the cursor in case the MOUSEBUTTONUP event occurs. If that's the case, we swap the position of the piece in our board.
import pygame
TILESIZE = 32
BOARD_POS = (10, 10)
def create_board_surf():
board_surf = pygame.Surface((TILESIZE*8, TILESIZE*8))
dark = False
for y in range(8):
for x in range(8):
rect = pygame.Rect(x*TILESIZE, y*TILESIZE, TILESIZE, TILESIZE)
pygame.draw.rect(board_surf, pygame.Color('darkgrey' if dark else 'beige'), rect)
dark = not dark
dark = not dark
return board_surf
def get_square_under_mouse(board):
mouse_pos = pygame.Vector2(pygame.mouse.get_pos()) - BOARD_POS
x, y = [int(v // TILESIZE) for v in mouse_pos]
try:
if x >= 0 and y >= 0: return (board[y][x], x, y)
except IndexError: pass
return None, None, None
def create_board():
board = []
for y in range(8):
board.append([])
for x in range(8):
board[y].append(None)
for x in range(0, 8):
board[1][x] = ('black', 'pawn')
for x in range(0, 8):
board[6][x] = ('white', 'pawn')
return board
def draw_pieces(screen, board, font, selected_piece):
sx, sy = None, None
if selected_piece:
piece, sx, sy = selected_piece
for y in range(8):
for x in range(8):
piece = board[y][x]
if piece:
selected = x == sx and y == sy
color, type = piece
s1 = font.render(type[0], True, pygame.Color('red' if selected else color))
s2 = font.render(type[0], True, pygame.Color('darkgrey'))
pos = pygame.Rect(BOARD_POS[0] + x * TILESIZE+1, BOARD_POS[1] + y * TILESIZE + 1, TILESIZE, TILESIZE)
screen.blit(s2, s2.get_rect(center=pos.center).move(1, 1))
screen.blit(s1, s1.get_rect(center=pos.center))
def draw_selector(screen, piece, x, y):
if piece != None:
rect = (BOARD_POS[0] + x * TILESIZE, BOARD_POS[1] + y * TILESIZE, TILESIZE, TILESIZE)
pygame.draw.rect(screen, (255, 0, 0, 50), rect, 2)
def draw_drag(screen, board, selected_piece, font):
if selected_piece:
piece, x, y = get_square_under_mouse(board)
if x != None:
rect = (BOARD_POS[0] + x * TILESIZE, BOARD_POS[1] + y * TILESIZE, TILESIZE, TILESIZE)
pygame.draw.rect(screen, (0, 255, 0, 50), rect, 2)
color, type = selected_piece[0]
s1 = font.render(type[0], True, pygame.Color(color))
s2 = font.render(type[0], True, pygame.Color('darkgrey'))
pos = pygame.Vector2(pygame.mouse.get_pos())
screen.blit(s2, s2.get_rect(center=pos + (1, 1)))
screen.blit(s1, s1.get_rect(center=pos))
selected_rect = pygame.Rect(BOARD_POS[0] + selected_piece[1] * TILESIZE, BOARD_POS[1] + selected_piece[2] * TILESIZE, TILESIZE, TILESIZE)
pygame.draw.line(screen, pygame.Color('red'), selected_rect.center, pos)
return (x, y)
def main():
pygame.init()
font = pygame.font.SysFont('', 32)
screen = pygame.display.set_mode((640, 480))
board = create_board()
board_surf = create_board_surf()
clock = pygame.time.Clock()
selected_piece = None
drop_pos = None
while True:
piece, x, y = get_square_under_mouse(board)
events = pygame.event.get()
for e in events:
if e.type == pygame.QUIT:
return
if e.type == pygame.MOUSEBUTTONDOWN:
if piece != None:
selected_piece = piece, x, y
if e.type == pygame.MOUSEBUTTONUP:
if drop_pos:
piece, old_x, old_y = selected_piece
board[old_y][old_x] = 0
new_x, new_y = drop_pos
board[new_y][new_x] = piece
selected_piece = None
drop_pos = None
screen.fill(pygame.Color('grey'))
screen.blit(board_surf, BOARD_POS)
draw_pieces(screen, board, font, selected_piece)
draw_selector(screen, piece, x, y)
drop_pos = draw_drag(screen, board, selected_piece, font)
pygame.display.flip()
clock.tick(60)
if __name__ == '__main__':
main()
Of course there's a lot that can be improved (like using better datatypes than tuples, extract common logic into functions etc), but this should give you a good start on how to implement such things.
Always keep in mind:
write a single game loop that handles events, game logic, and drawing
make sure to only call pygame.display.flip once per frame
seperate your game state from your drawing functions
never call time.sleep or pygame.time.wait
use the build-in classes like Vector2 and Rect, they'll make your live easier (I didn't the Sprite class in this code, but it's also very usefull)
use functions to clean up your code
avoid global variables, except for constants
PyGame is low-level library, not game engine, and you have to make almost all from scratch.
This example drags two images but for more images it could use list or pygame.sprite.Group with pygame.sprite.Sprites and then code is getting longer and longer but as I said PyGame is not game engine like ie. Godot Engine (which uses language similar to Python). Maybe with Pyglet could be easier because you don't have to write mainloop from scratch but it still need some work. In PyGame you have to write from scratch even main element - mainloop.
import pygame
# --- constants ---
RED = (213, 43, 67)
# --- main ---
pygame.init()
screen = pygame.display.set_mode((800,600))
chew1 = pygame.image.load("chew.png")
chew1_rect = chew1.get_rect(x=400, y=400)
chew2 = pygame.image.load("chew.png") # use different image
chew2_rect = chew1.get_rect(x=200, y=200)
drag = 0
# --- mainloop ---
clock = pygame.time.Clock()
game_exit = False
while not game_exit:
# - events -
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_exit = True
elif event.type == pygame.MOUSEBUTTONDOWN:
drag = 1
elif event.type == pygame.MOUSEBUTTONUP:
drag = 0
elif event.type == pygame.MOUSEMOTION:
if drag:
chew1_rect.move_ip(event.rel)
chew2_rect.move_ip(event.rel)
# - draws -
screen.fill(RED)
screen.blit(chew1, chew1_rect)
screen.blit(chew2, chew2_rect)
pygame.display.update()
# - FPS -
clock.tick(30)
# --- end ---
pygame.quit()
EDIT: the same with Group and Sprite and now you have to only add images to group items and rest of the code doesn't need to be changed.
import pygame
# --- constants ---
RED = (213, 43, 67)
# --- classes ---
class Item(pygame.sprite.Sprite):
def __init__(self, image, x, y):
super().__init__()
self.image = pygame.image.load(image)
self.rect = self.image.get_rect(x=x, y=y)
def update(self, rel):
self.rect.move_ip(rel)
# --- main ---
pygame.init()
screen = pygame.display.set_mode((800,600))
items = pygame.sprite.Group(
Item("chew.png", 200, 200),
Item("chew.png", 400, 200),
Item("chew.png", 200, 400),
Item("chew.png", 400, 400),
)
drag = 0
# --- mainloop ---
clock = pygame.time.Clock()
game_exit = False
while not game_exit:
# - events -
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_exit = True
elif event.type == pygame.MOUSEBUTTONDOWN:
drag = 1
elif event.type == pygame.MOUSEBUTTONUP:
drag = 0
elif event.type == pygame.MOUSEMOTION:
if drag:
items.update(event.rel)
# - draws -
screen.fill(RED)
items.draw(screen)
pygame.display.update()
# - FPS -
clock.tick(30)
# --- end ---
pygame.quit()
EDIT: this version use Group to move only clicked image(s). If you click in place where two (or more) images are overlapped then it will drag two (or more) images.
import pygame
# --- constants ---
RED = (213, 43, 67)
# --- classes ---
class Item(pygame.sprite.Sprite):
def __init__(self, image, x, y):
super().__init__()
self.image = pygame.image.load(image)
self.rect = self.image.get_rect(x=x, y=y)
def update(self, rel):
self.rect.move_ip(rel)
# --- main ---
pygame.init()
screen = pygame.display.set_mode((800,600))
items = pygame.sprite.Group(
Item("chew.png", 150, 50),
Item("chew.png", 400, 50),
Item("chew.png", 150, 300),
Item("chew.png", 400, 300),
)
dragged = pygame.sprite.Group()
# --- mainloop ---
clock = pygame.time.Clock()
game_exit = False
while not game_exit:
# - events -
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_exit = True
elif event.type == pygame.MOUSEBUTTONDOWN:
dragged.add(x for x in items if x.rect.collidepoint(event.pos))
elif event.type == pygame.MOUSEBUTTONUP:
dragged.empty()
elif event.type == pygame.MOUSEMOTION:
dragged.update(event.rel)
# - draws -
screen.fill(RED)
items.draw(screen)
pygame.display.update()
# - FPS -
clock.tick(30)
# --- end ---
pygame.quit()
EDIT: similar program in Pyglet - it moves two images with less code.
Pyglet has point (0,0) in left bottom corner.
To create red background it has to draw rectangle (QUADS) in OpenGL.
import pyglet
window = pyglet.window.Window(width=800, height=600)
batch = pyglet.graphics.Batch()
items = [
pyglet.sprite.Sprite(pyglet.resource.image('chew.png'), x=200, y=100, batch=batch),
pyglet.sprite.Sprite(pyglet.resource.image('chew.png'), x=400, y=300, batch=batch),
]
#window.event
def on_draw():
#window.clear()
pyglet.graphics.draw(4, pyglet.gl.GL_QUADS, ('v2f', [0,0, 800,0, 800,600, 0,600]), ('c3B', [213,43,67, 213,43,67, 213,43,67, 213,43,67])) #RED = (213, 43, 67)
batch.draw()
#window.event
def on_mouse_drag(x, y, dx, dy, buttons, modifiers):
for i in items:
i.x += dx
i.y += dy
pyglet.app.run()

Issue extending tail in Snake game Pygame

I'm writing a basic Snake game in Pygame, but the body of the snake is not extending after its eaten 4 pieces of food, nor does it extend after the first bite. I'm creating a new body object after each bite, and storing it in a list. x and y variables are used to store the position of the head prior to its movement, which in turn I use as the coordinates for the first segment of the snake's body. I'm then trying to iterate over this list, creating new objects with the x and y coordinates of the previous body segment to create the tail effect. Why is it stopping short?
import pygame
from pygame.locals import *
import random
import sys
pygame.init()
FPS = 30
fpsClock = pygame.time.Clock()
WIN_WIDTH = 680 #width of window
WIN_HEIGHT = 500 #height of window
DISPLAY = (WIN_WIDTH, WIN_HEIGHT) #variable for screen display
DEPTH = 32 #standard
FLAGS = 0 #standard
BLACK = (0, 0, 0) #black
RED = (255, 0, 0) #red
GOLD = (255, 215, 0)
IDK = (178, 154, 96)
screen = pygame.display.set_mode(DISPLAY, FLAGS, DEPTH)
pygame.display.set_caption('Snake')
snake_parts = [1]
Score = 0
speed = 10
snakex = 15
snakey = 70
size = 20
Snakes = 1
# --- classes ---
class Snake(pygame.Rect):
def __init__(self, x, y, screen, size, colour):
pygame.Rect.__init__(self, x, y, size, 20)
self.screen = screen
self.colour = colour
self.x = x
self.y = y
def draw(self, screen):
pygame.draw.rect(self.screen, self.colour, self)
def coordinates(self):
return self.x, self.y
class Food(pygame.Rect):
def __init__(self, x, y, screen):
pygame.Rect.__init__(self, x, y, 20, 20)
self.screen = screen
def draw(self, screen):
pygame.draw.rect(self.screen, GOLD, self)
# --- functions ---
def get_food_pos(WIN_WIDTH, WIN_HEIGHT):
WIN_WIDTH = random.randint(1, WIN_WIDTH)
WIN_HEIGHT = random.randint(1, WIN_HEIGHT)
return WIN_WIDTH, WIN_HEIGHT
eaten = True
pressed_right = True
pressed_left = False
pressed_up = False
pressed_down = False
pygame.key.set_repeat(10,10)
while True:
screen.fill(BLACK)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN: # check for key presses
if event.key == pygame.K_LEFT: # left arrow turns left
pressed_left = True
pressed_right = False
pressed_up = False
pressed_down = False
elif event.key == pygame.K_RIGHT: # right arrow turns right
pressed_right = True
pressed_left = False
pressed_up = False
pressed_down = False
elif event.key == pygame.K_UP: # up arrow goes up
pressed_up = True
pressed_right = False
pressed_left = False
pressed_down = False
elif event.key == pygame.K_DOWN: # down arrow goes down
pressed_down = True
pressed_right = False
pressed_up = False
pressed_left = False
x = snakex
y = snakey
if pressed_left:
snakex -= speed
elif pressed_right:
snakex += speed
elif pressed_up:
snakey -= speed
elif pressed_down:
snakey += speed
snake_parts[0] = Snake(snakex, snakey, screen, int(size), IDK)
snake_parts[0].draw(screen)
if eaten:
foodx, foody = get_food_pos(WIN_WIDTH, WIN_HEIGHT)
eaten = False
my_food = Food(foodx, foody, screen)
my_food.draw(screen)
if snake_parts[0].colliderect(my_food):
eaten = True
screen.fill(BLACK)
a_snake = Snake(snakex, snakey, screen, int(size), RED)
snake_parts.append(a_snake)
if len(snake_parts) >= 1:
for i in range(1, len(snake_parts)-1):
tempx, tempy = snake_parts[i].coordinates()
snake_parts[i] = Snake(x, y, screen, int(size), RED)
snake_parts[i].draw(screen)
snake_parts[i+1] = Snake(tempx, tempy, screen, int(size), RED)
x, y = tempx, tempy
Figured it out, the issue was this line:
snake_parts[i+1] = Snake(tempx, tempy, screen, int(size), RED)
removed it and it's working fine.

Categories