English 中文(简体)
我的花旗运动
原标题:my pygame sprite wont move

im trying to get a sprite to move around the screen with a walk animation and all it will do is stay stationary but it still goes through the animation. here s the code:

from pygame.locals import *
import pygame._view

pygame.init()
clock = pygame.time.Clock()

height = 500
width = 500
screen = pygame.display.set_mode((width, height), 0, 32)
pygame.display.set_caption( placeholder text )

photo =  grassbackground.png 
background = pygame.image.load(photo).convert()

photo1 = 1

user = pygame.sprite.Sprite()

change = False

x, y = (0, 0)

up = False
down = False
left = False
right = False

speed = 3

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_UP:
                up = True
                y += 1
                change = True
            if event.key == K_DOWN:
                down = True
                y -= 1
                change = True
            if event.key == K_LEFT:
                left = True
                x -= 1
                change = True
            if event.type == K_RIGHT:
                right = True
                x += 1
                change = True

        if event.type == KEYUP:
            if event.key == K_UP:
                up = False
                change = False
            if event.key == K_DOWN:
                down = False
                change = False
            if event.key == K_LEFT:
                left = False
                change = False
            if event.key == K_RIGHT:
                right = False
                change = False

if down and user.rect.bottom < height:
    user.rect.top += speed
if up and user.rect.top > 0:
    user.rect.top -= speed
if left and user.rect.left > 0:
    user.rect.left -= speed
if right and user.rect.right < width:
    user.rect.right += speed

if change == True:
    pygame.time.wait(110)
    photo1 += 1

if change == False:
    photo1 = 1

if photo1 == 1:
    user.image = pygame.image.load("1.png").convert()
    user.rect = user.image.get_rect()
    screen.blit(user.image, user.rect)

if photo1 == 2:
    user.image = pygame.image.load("2.png").convert()
    user.rect = user.image.get_rect()
    screen.blit(user.image, user.rect)

if photo1 == 3:
    user.image = pygame.image.load("3.png").convert()
    user.rect = user.image.get_rect()
    screen.blit(user.image, user.rect)

if photo1 >= 4:
    photo1 = 1

thesprites = pygame.sprite.RenderPlain((user))
thesprites.update()

screen.blit(background, (0, 0))
thesprites.draw(screen)

pygame.display.update()
clock.tick(60)

(p.s. im not sure why i have the x and y coordinates in there, i guess i just put that there in case i decided to use it) im not using any defined class and would prefer not to. thanks in advance

最佳回答

我可以肯定地说,因为你带上了必要的图像档案,以掌握你的照片。

I can t comment on posts because I haven t been active enough, but it looks like you re recreating the rect here:

if photo1 == 1:
    user.image = pygame.image.load("1.png").convert()
    user.rect = user.image.get_rect()
    screen.blit(user.image, user.rect)

if photo1 == 2:
    user.image = pygame.image.load("2.png").convert()
    user.rect = user.image.get_rect()
    screen.blit(user.image, user.rect)

if photo1 == 3:
    user.image = pygame.image.load("3.png").convert()
    user.rect = user.image.get_rect()
    screen.blit(user.image, user.rect)

即便是你调整了上述几条内容:

if down and user.rect.bottom < height:
    user.rect.top += speed
if up and user.rect.top > 0:
    user.rect.top -= speed
if left and user.rect.left > 0:
    user.rect.left -= speed
if right and user.rect.right < width:
    user.rect.right += speed

你们必须储存用于参与者职位的费用,然后在你创造新形象后重新应用。

除此以外,在考虑建立和使用职能时,《守则》将大大受益于这些职能。 http://www.penzilla.net/tutorials/python/Functions/“rel=“nofollow”http://www.penzilla.net/tutorials/python/Functions/。

问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签