我想讓我的Sprite在屏幕上移動,我創建了應該使它發生的功能,但它不起作用>問題在於move_ip函數,但我不知道如何解決它。這些是我第一次嘗試上課,因此歡迎任何建議。Move_ip不起作用,移動Sprites
import pygame
pygame.init()
finish = False
white = (255, 255, 255)
black = (0, 0, 0)
grey = (211, 211, 211)
font = pygame.font.Font("C:/Windows/Fonts/BRITANIC.TTF", 20)
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Game")
class Sprite(object):
def __init__(self, name):
self.health = 3
self.name = name
self.points = 0
self.x = 25
self.y = 25
def printName(self):
print (self.name)
class Dog(Sprite):
def __init__(self, name):
super(Dog, self).__init__(name)
self.dog_img = pygame.image.load("dog_left.png")
self.dog_rect = self.dog_img.get_rect()
def drawDog(self, screen):
screen.blit(self.dog_img, (self.x, self.y))
def takeBone(self, sp1, sp2):
takebone = pygame.sprite.collide_rect(sp1, sp2)
if takebone == True:
self.points += 1
def moving(self):
self.player_move_x = 0
self.player_move_y = 0
self.move = self.dog_img.move_ip(player_move_x, player_move_y)
class Bone(Sprite):
def __init__(self, name):
super(Bone, self).__init__(name)
self.neme = "Bone"
self.bone = pygame.image.load("bone.png")
self.bone_rect = self.bone.get_rect()
self.x = 250
self.y = 250
def drawBone(self, screen):
screen.blit(self.bone, (self.x, self.y))
end_pos = 170
player_x = 10
player_y = 10
background = pygame.image.load("grass.png")
background_rect = background.get_rect()
size = background.get_size()
screen2 = pygame.display.set_mode(size)
player = Dog("Tom")
bone = Bone("Bone")
timer = pygame.time.Clock()
while finish == False:
for event in pygame.event.get():
if event.type == pygame.QUIT:
finish = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player_move_x = -5
if event.key == pygame.K_RIGHT:
player_move_x = 5
if event.key == pygame.K_UP:
player_move_y = -5
if event.key == pygame.K_DOWN:
player_move_y = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
player_move_x = 0
if event.key == pygame.K_RIGHT:
player_move_x = 0
if event.key == pygame.K_UP:
player_move_y = 0
if event.key == pygame.K_DOWN:
player_move_y = 0
screen.blit(background, background_rect)
player.drawDog(screen)
bone.drawBone(screen)
pygame.display.flip()
timer.tick(25)
pygame.quit()
我已經修改了我的代碼:
import pygame
pygame.init()
finish = False
white = (255, 255, 255)
black = (0, 0, 0)
grey = (211, 211, 211)
font = pygame.font.Font("C:/Windows/Fonts/BRITANIC.TTF", 20)
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Game")
class Sprite(object):
def __init__(self, name):
self.health = 3
self.name = name
self.points = 0
self.x = 25
self.y = 25
def printName(self):
print (self.name)
class Dog(Sprite):
def __init__(self, name):
super(Dog, self).__init__(name)
self.dog_img = pygame.image.load("dog_left.png")
self.dog_rect = self.dog_img.get_rect()
def drawDog(self, screen):
screen.blit(self.dog_img, (self.x, self.y))
def takeBone(self, sp1, sp2):
takebone = pygame.sprite.collide_rect(sp1, sp2)
if takebone == True:
self.points += 1
def moving(self):
self.player_move_x = 0
self.player_move_y = 0
self.move = self.dog_rect.move_ip(self.player_move_x, self.player_move_y)
class Bone(Sprite):
def __init__(self, name):
super(Bone, self).__init__(name)
self.neme = "Bone"
self.bone = pygame.image.load("bone.png")
self.bone_rect = self.bone.get_rect()
self.x = 250
self.y = 250
def drawBone(self, screen):
screen.blit(self.bone, (self.x, self.y))
background = pygame.image.load("grass.png")
background_rect = background.get_rect()
size = background.get_size()
screen2 = pygame.display.set_mode(size)
player = Dog("Tom")
bone = Bone("Bone")
timer = pygame.time.Clock()
while finish == False:
for event in pygame.event.get():
if event.type == pygame.QUIT:
finish = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.player_move_x = -5
if event.key == pygame.K_RIGHT:
self.player_move_x = 5
if event.key == pygame.K_UP:
player.dog_rect.move_ip(-5, 0)
player_move_y = -5
if event.key == pygame.K_DOWN:
self.player_move_y = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
self.player_move_x = 0
if event.key == pygame.K_RIGHT:
self.player_move_x = 0
if event.key == pygame.K_UP:
player.dog_rect.move_ip(0, 0)
player_move_y = 0
if event.key == pygame.K_DOWN:
self.player_move_y = 0
screen.blit(background, background_rect)
player.drawDog(screen)
player.moving()
bone.drawBone(screen)
pygame.display.flip()
timer.tick(25)
pygame.quit()
我不熟悉的pygame的,但我沒有看到'KEYDOWN'或'KEYUP' if語句中的移動信息是如何傳送給你的'player'對象的。你需要調用你創建的'moving'方法,或者設置'player.player_move_x','player.player_move_y'? –