Py学习  »  Python

python pygame动画不带类,但只要我把它放到一个类中,它就不想工作了?

Miccs • 4 年前 • 461 次点击  

好吧,基本上我有一个我制作的游戏,在我用类把它放到面向对象的编程中之前,它工作得很好,动画在移动时工作,但是当我把它变成面向对象的编程时,我的角色移动但没有动画,他也在移动时不可见,只有当我停止按下时才会出现移动按钮请帮忙

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()
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/55230
 
461 次点击  
文章 [ 1 ]  |  最新文章 4 年前
Rabbid76
Reply   •   1 楼
Rabbid76    4 年前

有2个 Indentation 问题。第一个在 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:
            # [...]

第二个在主回路中:

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

pygame.transform.scale 代码开头的指令是无用的,因为 . 不缩放曲面本身,它将返回一个新的缩放曲面。

更改代码如下( 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))