简体   繁体   中英

pygame/python sprite not moving? What have i done wrong? (no errors)

Alright, so here's my code, I get no errors but my sprite (player) is not moving

import pygame,sys
import random
import time

#Colors
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)

class Block(pygame.sprite.Sprite):
    def __init__(self, color = blue, width = 50, height = 50):

    super(Block, self).__init__()

    self.image = pygame.Surface((width, height))

    self.image.fill(color)

    self.rect = self.image.get_rect()

def set_position(self, x , y):
    self.rect.x = x
    self.rect.y = y

def set_image(self, filename = None):
    if(filename != None):
        self.image = pygame.image.load(filename)

        self.rect = self.image.get_rect()

if (__name__ == "__main__"):
    pygame.init()

    window_size = window_width, window_height = 640,480
    window = pygame.display.set_mode(window_size)
    pygame.display.set_caption('Test')

    window.fill(white)
    clock = pygame.time.Clock()

    #important variables
    pos_x = 300
    pos_y = 200
    pos_x_change = 0
    pos_y_change = 0

    block_group = pygame.sprite.Group()
    player = Block()
    player.set_image('player.png')
    player.set_position(pos_x,pos_y)  #Player variable for pos

    another_block = Block(red)
    another_block.set_position(100,100)

    block_group.add(player, another_block)
    block_group.draw(window)

    pygame.display.update()



    running = True

    while(running):
        for event in pygame.event.get():
            if (event.type == pygame.QUIT):
                running = False

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                pos_x_change = -10
            if event.key == pygame.K_RIGHT:
                pos_x_change = 10
            if event.key == pygame.K_UP:
                pos_y_change -10
            if event.key == pygame.K_DOWN:
                pos_y_change = 10
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                pos_x_change = 0
            if event.key == pygame.K_RIGHT:
                pos_x_change = 0
            if event.key == pygame.K_UP:
                pos_y_change == 0
            if event.key == pygame.K_DOWN:
                pos_y_change == 0


    pos_x += pos_x_change
    pos_y += pos_y_change
    clock.tick(60)

pygame.quit 
quit()

As you can see, I clearly added the pos_x and pos_y and set it to pos_x += pos_x_change and pos_y += pos_y_change but the sprite is still not moving. I'm guessing it's a misplacement of code because python heavily relies on indentation? Please explain to me what i have done wrong, It would be greatly appreciated.

It looks like you're updating the variables that contain the players x and y coordinates, but you're not updating the display. That's why it looks like the position isn't changing. I believe calling player.set_position(pos_x,pos_y) just above the clock.tick(60) statement will fix the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM