pygame/Python的精灵不动?我做错了什么? (没有错误) [英] pygame/python sprite not moving? What have i done wrong? (no errors)

查看:97
本文介绍了pygame/Python的精灵不动?我做错了什么? (没有错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这是我的代码,我没有收到任何错误,但是我的精灵(玩家)没有动弹

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()

如您所见,我清楚地添加了pos_x和pos_y 并将其设置为pos_x + = pos_x_change 和pos_y + = pos_y_change 但精灵仍然没有移动. 我猜这是代码的错位,因为python严重依赖缩进吗?请向我解释我做错了什么,将不胜感激.

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.

推荐答案

似乎您正在更新包含玩家x和y坐标的变量,但您没有更新显示内容.这就是为什么看起来位置没有变化的原因.我相信在clock.tick(60)语句上方调用player.set_position(pos_x,pos_y)可以解决此问题.

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.

这篇关于pygame/Python的精灵不动?我做错了什么? (没有错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆