简体   繁体   English

python pygame animation 可以在没有 class 的情况下工作,但是一旦我将它放入 class 中,它就不想工作了?

[英]python pygame animation works without class but as soon as i put it into a class it doesnt want to work?

okay, so basically i have this game that im making and before i put it into Object Oriented Programming with classes, it worked perfectly fine, the animation worked when moving, but as soon as i changed it into Object Oriented Programming, my character moves but no animation, he also goes invisible when he moves, only appears when i stop pressing a movement button please help好的,所以基本上我有这个游戏,我正在制作,在我将它放入带有类的 Object 面向编程之前,它工作得很好,animation 在移动时工作,但是一旦我将它更改为 Object 面向编程,我的角色移动但是没有 animation,他移动时也会隐形,只有在我停止按下移动按钮时才会出现,请帮忙

heres the code:继承人的代码:

import time
import pygame
pygame.init()

win = pygame.display.set_mode((1280,720)) #creates a window size of 640 x 480 pixels

pygame.display.set_caption("World of Python") #The caption of the window is "World of Python"
pygame.transform.scale(pygame.image.load('R1.png'), (40, 60))
pygame.transform.scale(pygame.image.load('R2.png'), (40, 60))
pygame.transform.scale(pygame.image.load('R3.png'), (40, 60))
pygame.transform.scale(pygame.image.load('L1.png'), (40, 60))
pygame.transform.scale(pygame.image.load('L2.png'), (40, 60))
pygame.transform.scale(pygame.image.load('L3.png'), (40, 60))
pygame.transform.scale(pygame.image.load('U1.png'), (40, 60))
pygame.transform.scale(pygame.image.load('U2.png'), (40, 60))
pygame.transform.scale(pygame.image.load('U3.png'), (40, 60))
pygame.transform.scale(pygame.image.load('D1.png'), (40, 60))
pygame.transform.scale(pygame.image.load('D2.png'), (40, 60))
pygame.transform.scale(pygame.image.load('D3.png'), (40, 60))

moveRight = [pygame.image.load('R1.png'), pygame.image.load('R2.png'), pygame.image.load('R3.png')] #list of frames
moveLeft = [pygame.image.load('L1.png'), pygame.image.load('L2.png'), pygame.image.load('L3.png')]#list of frames
moveUp = [pygame.image.load('U1.png'), pygame.image.load('U2.png'), pygame.image.load('U3.png')]#list of frames
moveDown = [pygame.image.load('D1.png'), pygame.image.load('D2.png'), pygame.image.load('D3.png')]#list of frames
character = pygame.image.load('D2.png') #standard frame
bg = pygame.image.load('Grass.png') #background
character = pygame.transform.scale(character, (40, 60))

class player(object):
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.vel = 5
        self.left = False
        self.right = False
        self.down = False
        self.up = False
        self.moveCount = 0

    def draw(self,win):

        if self.moveCount + 1 >= 9: #if move is greater than or equal to 9
            self.moveCount = 0

            if self.left:
                win.blit(moveLeft[self.moveCount//3], (self.x,self.y)) #goes through the frames integer division (MOD), 1,2,3 because 3 frames for each movement.
                self.moveCount += 1
        elif self.right:
                win.blit(moveRight[self.moveCount//3], (self.x,self.y))#goes through the frames integer division (MOD), 1,2,3 because 3 frames for each movement.
                self.moveCount += 1
        elif self.up:
                win.blit(moveUp[self.moveCount//3], (self.x,self.y))#goes through the frames integer division (MOD), 1,2,3 because 3 frames for each movement.
                self.moveCount += 1
        elif self.down:
                win.blit(moveDown[self.moveCount//3], (self.x,self.y))#goes through the frames integer division (MOD), 1,2,3 because 3 frames for each movement.
                self.moveCount += 1
        else:
                win.blit(character, (self.x,self.y)) #if character is standing, draw character in its position



#main
def redrawGameWindow():
        win.blit(bg, (0,0)) #spawns background at coordinate 0,0
        man.draw(win)     
        pygame.display.update()





man = player(920, 240, 40, 60)
run = True
while run:    #while loop
    pygame.time.delay(25)#framerate of 40, (milliseconds)

    for event in pygame.event.get():
        if event.type == pygame.QUIT: #if you pressed x, you exit
            run = False

    keys = pygame.key.get_pressed()  #defines keys pressed

    if keys[pygame.K_LEFT] and man.x > man.vel: #checks for border and if a button is pressed
        man.x -= man.vel
        man.left = True
        man.right = False
    elif keys[pygame.K_RIGHT]and man.x < 1280 - man.width:#checks for border and if a button is pressed
        man.x += man.vel
        man.right = True
        man.left = False
    elif keys[pygame.K_UP] and man.y > man.vel:#checks for border and if a button is pressed
        man.y -= man.vel
        man.up = True
        man.down = False
    elif keys[pygame.K_DOWN] and man.y < 720 - man.height:#checks for border and if a button is pressed
        man.y += man.vel
        man.down = True
        man.up = False
    else:       #this is incase the player is not moving
        man.left = False
        man.right = False
        man.up = False
        man.down = False
        man.moveCount = 0

        redrawGameWindow()



pygame.quit()

There are 2 Indentation issues.有 2 个缩进问题。 The 1st is in player.draw :第一个在player.draw中:

class player(object):
    # [...]

    def draw(self,win):

        if self.moveCount + 1 >= 9: 
            self.moveCount = 0

        #<--| INDENTATION !!!
        if self.left:
                win.blit(moveLeft[self.moveCount//3], (self.x,self.y)) 
                self.moveCount += 1
        elif self.right:
            # [...]

The 2nsd one is in the main loop: 2nsd 一个在主循环中:

run = True
while run:
    # [...]

    else:       #this is incase the player is not moving
        man.left = False
        man.right = False
        man.up = False
        man.down = False
        man.moveCount = 0

    #<--| INDENTATION !!!
    redrawGameWindow()

Furthermore, the pygame.transform.scale instructions at the begin of the code are useless, because pygame.transform.scale .此外,代码开头的pygame.transform.scale指令没有用,因为pygame.transform.scale does not scale the surface itself, it returns a new scaled surface.不缩放表面本身,它返回一个新的缩放表面。

Change th code as follows (str converts the number to a string):更改代码如下(str将数字转换为字符串):

moveRight = [pygame.transform.scale(pygame.image.load('R' + str(i+1) + '.png'), (40, 60)) for i in range(3)]
moveLeft  = [pygame.transform.scale(pygame.image.load('L' + str(i+1) + '.png'), (40, 60)) for i in range(3)]
moveUp    = [pygame.transform.scale(pygame.image.load('U' + str(i+1) + '.png'), (40, 60)) for i in range(3)]
moveDown  = [pygame.transform.scale(pygame.image.load('D' + str(i+1) + '.png'), (40, 60)) for i in range(3)]
character = pygame.transform.scale(pygame.image.load('D2.png'), (40, 60))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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