img1 = pygame.image.load(r"C:\Users\ga-sa\Downloads\As.png")
img2 = pygame.image.load(r"C:\Users\ga-sa\Downloads\AssetsXOR.png")
img3 = pygame.image.load(r"C:\Users\ga-sa\Downloads\AssetsNOT.png")
img4 = pygame.image.load(r"C:\Users\ga-sa\Downloads\AssetsAND.png")
background = pygame.image.load(r'C:\Users\ga-sa\Downloads\honeycomb.png')
images = [img1, img2, img3, img4]
current_image = -1
img_rects = [images[i].get_rect(topleft=(20 + 40 * i, 20)) for i in range(len(images))]
LeftButton = 0
while 1:
for e in pygame.event.get():
if e.type == QUIT:
pygame.quit()
exit(0)
if e.type == pygame.MOUSEBUTTONDOWN:
mouse_rect = pygame.Rect(e.pos, (1, 1))
current_image = mouse_rect.collidelist(img_rects)
if e.type == MOUSEMOTION:
if e.buttons[LeftButton]:
rel = e.rel
if 0 <= current_image < len(images):
img_rects[current_image].x += rel[0]
img_rects[current_image].y += rel[1]
screen.blit(background,(0,0))
for i in range(len(images)):
screen.blit(images[i], img_rects[i])
pygame.display.flip()
pygame.time.delay(30)
pygame.display.fli()
pygame.display.update()
mainClock.tick(60)
i just wanna rotate this images keyboards but a don`t how i can do it somebody could help me and the full code is here https://pastebin.com/Rb7jh2GH
Add an angle
variable. Use pygame.key.get_pressed()
to get the sate of the keys (see How can I make a sprite move when key is held down). Change the angle
dependent on the key pressed:
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
angle -= 1
if keys[pygame.K_LEFT]:
angle += 1
Use pygame.transform.rotate
to rotate an image. See How do I rotate an image around its center using PyGame?:
for i in range(len(images)):
rotated_image = pygame.transform.rotate(images[i], angle)
rotated_rect = rotated_image.get_rect(center = img_rects[i].center)
screen.blit(rotated_image, rotated_rect)
If you just want to rotate the current_image
, you need a list of angles
current_image = -1
img_rects = [images[i].get_rect(topleft=(20 + 40 * i, 20)) for i in range(len(images))]
img_angles = [0 for _ in range(len(images))]
LeftButton = 0
while 1:
for e in pygame.event.get():
if e.type == QUIT:
pygame.quit()
exit(0)
if e.type == pygame.MOUSEBUTTONDOWN:
mouse_rect = pygame.Rect(e.pos, (1, 1))
current_image = mouse_rect.collidelist(img_rects)
if e.type == MOUSEMOTION:
if e.buttons[LeftButton]:
rel = e.rel
if 0 <= current_image < len(images):
img_rects[current_image].x += rel[0]
img_rects[current_image].y += rel[1]
keys = pygame.key.get_pressed()
if 0 <= current_image < len(img_angles):
if keys[pygame.K_RIGHT]:
img_angles[current_image] -= 1
if keys[pygame.K_LEFT]:
img_angles[current_image] += 1
screen.blit(background,(0,0))
for i in range(len(images)):
rotated_image = pygame.transform.rotate(images[i], img_angles[i])
rotated_rect = rotated_image.get_rect(center = img_rects[i].center)
screen.blit(rotated_image, rotated_rect)
pygame.display.flip()