The code I pasted below moves seven card images from and to predefined spots on a surface. However, I would like the images to move one at a time--image1
moves to (100, 180)
and only then does image2
move to (240, 300)
, etc. Right now all seven images move at the same time.
I have seen SO posts like this and this, and looked at the referenced pyGame documentation. The sources tell me I need to "pump" the event que but, to be honest, I don't really understand. I understand the concept of event-based programming and writing event handlers, from languages like Delphi, C#, and even the pyQt GUI framework, but beyond that ... struggling. (When do I pump? Does pumping also update? Do I call update()
with pump=True
?)
NOTE: To clarify, my question is not about how to move an image between given coordinates. The code below already does that. It's about moving image1
between given coordinates before image2
or any other images move. I want image1
to move, then image2
, then image3
, etc. I don't want all seven images to move simultaneously.
import pygame
pygame.init()
window = pygame.display.set_mode((1160, 580))
clock = pygame.time.Clock()
pygame.display.set_caption('Blackjack Card Counting Drill')
pos_start = (580, 100)
pos1 = pos_start
pos_end1 = (100, 180)
pos2 = pos_start
pos_end2 = (240, 300)
pos3 = pos_start
pos_end3 = (400, 360)
pos4 = pos_start
pos_end4 = (580, 390)
pos5 = pos_start
pos_end5 = (760, 360)
pos6 = pos_start
pos_end6 = (930, 300)
pos7 = pos_start
pos_end7 = (1060, 180)
speed = 5
# pos = image position coordinates
def move(pos, pos_end):
# direction = vector whose coordinates are the image's end point - current position
direction = pygame.math.Vector2(pos_end) - pos
# as long as the direction vector is longer than the distance we want the image to move/method execution...
if direction.length() > speed:
# scale the direction vector so its length=speed, keeping its direction the same
# so, the vector's still pointing toward the end coordinates, and it's 5 units long
direction.scale_to_length(speed)
# take the image's current position and move it 5 units in the right direction
new_pos = pygame.math.Vector2(pos) + direction
# return its new location
pos = (new_pos.x, new_pos.y)
return pos
bg = pygame.image.load("blackjack_table.jpg")
deck = pygame.image.load('card_back.png')
# scale image to 0.1 size
deck = pygame.transform.rotozoom(deck, 0, 0.1)
image1 = pygame.image.load('2C.png')
image1 = pygame.transform.rotozoom(image1, 0, 0.1)
image2 = pygame.image.load('2C.png')
image2 = pygame.transform.rotozoom(image2, 0, 0.1)
image3 = pygame.image.load('2C.png')
image3 = pygame.transform.rotozoom(image3, 0, 0.1)
image4 = pygame.image.load('2C.png')
image4 = pygame.transform.rotozoom(image4, 0, 0.1)
image5 = pygame.image.load('2C.png')
image5 = pygame.transform.rotozoom(image5, 0, 0.1)
image6 = pygame.image.load('2C.png')
image6 = pygame.transform.rotozoom(image6, 0, 0.1)
image7 = pygame.image.load('2C.png')
image7 = pygame.transform.rotozoom(image7, 0, 0.1)
run = True
while run:
clock.tick(200)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window.blit(bg, (0, 0))
deck_rect = deck.get_rect(center=pos_start)
window.blit(deck, deck_rect)
pos1 = move(pos1, pos_end1)
image_rect = image1.get_rect(center=pos1)
window.blit(image1, image_rect)
pos2 = move(pos2, pos_end2)
image_rect2 = image2.get_rect(center=pos2)
window.blit(image2, image_rect2)
pos3 = move(pos3, pos_end3)
image_rect3 = image3.get_rect(center=pos3)
window.blit(image3, image_rect3)
pos4 = move(pos4, pos_end4)
image_rect4 = image4.get_rect(center=pos4)
window.blit(image4, image_rect4)
pos5 = move(pos5, pos_end5)
image_rect5 = image5.get_rect(center=pos5)
window.blit(image5, image_rect5)
pos6 = move(pos6, pos_end6)
image_rect6 = image6.get_rect(center=pos6)
window.blit(image6, image_rect6)
pos7 = move(pos7, pos_end7)
image_rect7 = image7.get_rect(center=pos7)
window.blit(image7, image_rect7)
pygame.display.update()
pygame.quit()
exit()
p.s. I know the code's inefficiently written. It needs to be refactored into procedures.