pygame/python精灵不动?我做错了什么? (没有错误)
pygame/python sprite not moving? What have i done wrong? (no errors)
好的,这是我的代码,我没有收到任何错误,但我的精灵(玩家)没有移动
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 严重依赖缩进?请向我解释我做错了什么,将不胜感激。
您似乎正在更新包含玩家 x 和 y 坐标的变量,但并未更新显示。这就是为什么看起来位置没有改变的原因。我相信在 clock.tick(60)
语句上方调用 player.set_position(pos_x,pos_y)
会解决问题。
好的,这是我的代码,我没有收到任何错误,但我的精灵(玩家)没有移动
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 严重依赖缩进?请向我解释我做错了什么,将不胜感激。
您似乎正在更新包含玩家 x 和 y 坐标的变量,但并未更新显示。这就是为什么看起来位置没有改变的原因。我相信在 clock.tick(60)
语句上方调用 player.set_position(pos_x,pos_y)
会解决问题。