Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 moving image with blit
#1
Hi,

I am developing a game where the player sprite will move automatically after rolling a dice. based on the number of dice it will move the sprite to the specific mouse location but it is not working! I can see it moves but gets back to its original position which is set before the while loop.

Code:

while done == False:
    screen.fill(WHITE)

    # move the image based on the dice answer
    moveList = {1: [80, 555], 2: [140, 555], 3: [200, 555], 4: [260, 555], 5: [320, 555], 6: [380, 555], 7: [440, 555],
                8: [500, 555], 9: [560, 555], 10: [620, 555]}

    # mouse = pygame.mouse.get_pos()
    # mouseX = mouse[0]
    # mouseY = mouse[1]
    # print (mouseX, mouseY)

    screen.blit(bg_image,[45,0])
    screen.blit(player1_img, [0, 545])
    screen.blit(player2_img, [0, 565])
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
            pygame.quit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_r and roll == False:
                roll = True
            elif event.key == pygame.K_r and roll == True:
                roll = False
                # switch player unless it is 6
                if player == 1 and num == 6:
                    thepos = moveList[num]
                    theXpos = thepos[0]
                    theYpos = thepos[1]
                    screen.blit(player2_img, [theXpos, theYpos])

Quote
#2
we need your whole code.

You should be using pygame rects for positioning, not arbitrary x and y positions.
Quote
#3
(Jul-09-2019, 04:59 PM)rwahdan Wrote:

while done == False:
    screen.fill(WHITE)
# ...snip...
    screen.blit(bg_image,[45,0])
    screen.blit(player1_img, [0, 545])
    screen.blit(player2_img, [0, 565])
# ...snip...

Every iteration, you draw the players at the same spot, right before checking for events. If you want them to move, you need to track their current location somewhere (and probably initialize that location to (0, 545)), and update it when you want them to move.
metulburr likes this post
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [PyGame] Keep moving pressing a key alexponceal 1 168 Jul-09-2019, 04:07 PM
Last Post: Windspar
  [PyGame] pygame.draw.rect function stretches across the screen instead of moving BubblesTheGiraffe 2 343 Jun-11-2019, 08:32 PM
Last Post: metulburr
  moving a sprite pfaber11 3 419 May-15-2019, 12:52 PM
Last Post: pfaber11
  drawing, moving, and collision problems (pygame) SheeppOSU 26 1,870 Apr-22-2019, 03:09 AM
Last Post: SheeppOSU
  error with survace.blit joemcsk1 3 575 Aug-06-2018, 12:23 AM
Last Post: metulburr
  Moving chess piece using mouse reepio13 7 1,784 Feb-24-2018, 11:26 PM
Last Post: reepio13
  Moving an object in a circular path pi314159geek 1 3,058 Dec-25-2017, 12:06 PM
Last Post: Windspar

Forum Jump:


Users browsing this thread: 1 Guest(s)