0

So im trying to remake a simple card game in pygame. Im trying to make it where whenever you hover over a card it will turn slightly transparent by changing the alpha of the card.

However I can't seem to get this to work and im genuinely confucious as to why my code doesnt work.

while running:
    mX, mY = pygame.mouse.get_pos()
    if 50 + 60 > mX > 50 and 500 + 84 > mY > 500:
        hand[0].set_alpha(100)
    else:
        hand[0].set_alpha(255)


    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        #Hover()
    if printHand:
        #print(hand)
        #print(cardSize)
        #print(hand[0].get_width())
        #print(hand[0].get_height())
        #print(hand[0].get_rect(center=(80, 542)))
        printHand = False

    if displayHand:
        DisplayHand(hand)
        DisplayBoard(pile1)
        DisplayBoard(pile2)
        displayHand = False



    print(50 + 60 > mX > 50 and 500 + 84 > mY > 500)
    pygame.display.update()

Where hand is an array of surfaces from image.load

As of right now this code doesnt do anything but the print statement at the end returns true

  • 1
    Hi Nate, as it stands, your question is hard to answer as you haven't included enough code for us to run. Can you put some time into creating a [MCVE](https://stackoverflow.com/help/minimal-reproducible-example) and update the question? – Peter Gibson Oct 04 '22 at 01:09
  • 1
    After you do `printHand = False` and `displayHand = False`, you never set them back to `true`, hence those `if` block are never executed again. – Rodrigo Rodrigues Oct 04 '22 at 03:35

1 Answers1

2

Here is a minimal example that creates a Card sprite with two images, swapping the image if the mouse is over the card.

import pygame

WIDTH = 640
HEIGHT = 480
FPS = 30

class Card(pygame.sprite.Sprite):
    """A playing card like sprite"""
    def __init__(self, color="white", pos=(0, 0)):
        pygame.sprite.Sprite.__init__(self)
        self.color = pygame.Color(color)
        # if you're loading an image, make sure to use .convert()
        self.orig_image = pygame.Surface((50, 100), pygame.SRCALPHA)
        self.orig_image.fill(self.color)
        # create a copy of the image
        self.alt_image = self.orig_image.copy()
        self.alt_image.set_alpha(127)  # make the copy transparent
        self.image = self.orig_image
        self.rect = self.image.get_rect(center=pos)

    def update(self):
        if self.rect.collidepoint(pygame.mouse.get_pos()):
            self.image = self.alt_image
        else:
            self.image = self.orig_image


pygame.init()
window = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()

cards = pygame.sprite.Group()
# create five cards distributed across the bottom of the screen
for x in range(5):
    card = Card(pos=((x + 1) * (WIDTH // 6), HEIGHT - 90))
    cards.add(card)


pygame.display.set_caption("♠♣♥♦ Cards")
paused = False
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYUP:
            pass
    # update game elements
    cards.update()
    # draw surface - fill background
    window.fill("black")
    ## draw sprites
    cards.draw(window)
    # show surface
    pygame.display.update()
    # limit frames
    clock.tick(FPS)
pygame.quit()

This will look like this, although the screenshot doesn't include the mouse cursor: Cards example

import random
  • 3,054
  • 1
  • 17
  • 22
  • 1
    Helped a lot, I'm new to pygame so I was having some trouble. Thanks so much youve saved my head from hitting a brick wall. – Nate Morales Oct 05 '22 at 06:21