有人可以在我的pygame代码中实现重力和运行动画吗?

问题描述

在本学期,我正在与pygame一起制作Mario,以进行我的课堂评估,这是pygame的新手,我在任何地方都找不到如何在游戏中正确实现重力的方法,因此当马里奥跳楼时,他便倒下了。我的第二个问题是尝试制作连续动画,我希望Mario在顺序中左右移动时浏览图片。 1、2、3、2(一遍又一遍)。如果有人可以帮助,那就太好了!

import pygame

import time

import random


pygame.init()


display_width = 800
display_height = 600
mario_width = 55

blue = (77,240,255)
green = (0,186,43)
ground_colour = (186,150,97)

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Super Italiano Bros")

direction = 'right'
clock = pygame.time.Clock()
cloudImg = pygame.image.load('Cloud.png')


rightMarioImg = pygame.image.load('MarioRight.png')
rightMarioImg2 = pygame.image.load("MarioRight2.png")
rightMarioImg3 = pygame.image.load("MarioRight3.png")

leftMarioImg = pygame.image.load('MarioLeft.png')
leftMarioImg2 = pygame.image.load('MarioLeft2.png')
leftMarioImg3 = pygame.image.load('MarioLeft3.png')

def marioRight(x,y):
    gameDisplay.blit(rightMarioImg,(x,y))

def marioLeft(x,y):
    gameDisplay.blit(leftMarioImg,y))

def cloud(x_cloud,y_cloud):
    gameDisplay.blit(cloudImg,(x_cloud,y_cloud))

def cloud_two(x_cloud_two,y_cloud_two):
    gameDisplay.blit(cloudImg,(x_cloud_two,y_cloud_two))       

def game_loop():
    global direction

    x = (320)
    y = (360)

    x_cloud = (-200)
    y_cloud = (-220)

    x_cloud_two = (200)
    y_cloud_two = (-170)

    x_change = (0)
    y_change = (0)

    gameExit = False

    while not gameExit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True
            print(event)

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                x_change = -5
                direction = 'left'
            if event.key == pygame.K_RIGHT:
                x_change = 5
                direction = 'right'
            if event.key == pygame.K_UP:
                y_change = -60
            if event.key == pygame.K_ESCAPE:
                gameExit = True
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == pygame.K_UP:
                x_change = 0
                y_change = 60

        x += x_change
        y += y_change

        if x <= 0:
            x = 0
        if y <= 300:
            y = 300
        if y >= 360:
            y = 360
            
        gameDisplay.fill(blue)
        pygame.draw.rect(gameDisplay,ground_colour,(0,500,800,500))
        pygame.draw.rect(gameDisplay,green,470,30))
        cloud(x_cloud,y_cloud)
        cloud_two(x_cloud_two,y_cloud_two)

        if direction == 'right':
            marioRight(x,y)
        else:
            marioLeft(x,y)

        pygame.display.update()
        clock.tick(60)

game_loop()
pygame.quit()
quit()

解决方法

解决了一些问题,并做了很多代码样式设计。当您按下向上键时,他跳了一个固定的量,当您释放它时,他又回到了地面。现在,它会自己动画。图像在每个循环中都会更改,这可能是非常快的,因此您可能只想每隔一定的帧数就增加一次状态。

我所做的更改摘要:

  1. 导入通常分为三个不同的块(由单个空白行划分),并按字母顺序排列:
    1. 第一块包含所有标准库导入
    2. 第二个块包含第三方库
    3. 第三块包含该项目中其他文件的本地导入
  2. 文件应具有0个未使用的导入
  3. Python中的函数和变量名称遵循蛇形符号:my_variablemy_function。 “常量”以大写字母MY_CONSTANT书写。班级使用驼峰式的情况:MyClass
  4. dictlist允许您以更有条理的方式存储数据。例如,我们可以有MARIO_IMGSdict中的list MARIO_IMGS['right'][0]个存储所有马里奥图像,以便可以像game_loop一样进行访问(第一个图像是列表中的索引0)从0开始计数。
  5. 为避免使用全局变量,我将循环内仅需要使用的变量移至if __name__ == '__main__':函数,其余未定义的顶级语句位于cloud块中,该块在以下情况下运行脚本已启动,但导入此脚本后未启动。
  6. 具有cloud_twocloud(x_cloud,y_cloud)函数的功能完全相同的功能被重用是没有意义的。致电cloud(x_cloud_two,y_cloud_two),然后致电x = (320)
  7. 如果不需要,请不要使用不需要的括号,例如:x = 320应该为pygame.QUIT
  8. 您在处理事件循环之外的事件时遇到了缩进错误,因此仅处理最后一个事件(退出时除外,该部分位于循环内,因此将针对quit()检查每个事件)
  9. 行数不得超过80个字符。
  10. 不需要python文件末尾的
  11. quit()import pygame # Window width and height in px DISPLAY_SIZE = (800,600) # Window title TITLE = "Super Italiano Bros" # Predefined colors BLUE = (77,240,255) GREEN = (0,186,43) GROUND_COLOR = (186,150,97) # Images MARIO_IMGS = { 'right': [ pygame.image.load('MarioRight.png'),pygame.image.load("MarioRight2.png"),pygame.image.load("MarioRight3.png"),],'left': [ pygame.image.load('MarioLeft.png'),pygame.image.load('MarioLeft2.png'),pygame.image.load('MarioLeft3.png'),] } CLOUD_IMG = pygame.image.load('Cloud.png') MAX_STATES = min(map(len,MARIO_IMGS.values())) # 3 def draw_mario(x,y,direction='right',state=0): screen.blit(MARIO_IMGS[direction][state],(x,y)) def draw_cloud(x_cloud,y_cloud): screen.blit(CLOUD_IMG,(x_cloud,y_cloud)) def game_loop(): # Mario position x = 320 y = 360 x_change = 0 state = 0 direction = 'right' # Cloud positions x_cloud = -200 y_cloud = -220 x_cloud_two = 200 y_cloud_two = -170 # Clock used to limit the FPS clock = pygame.time.Clock() game_exit = False while not game_exit: for event in pygame.event.get(): if event.type == pygame.QUIT: game_exit = True print(event) if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: x_change = -5 direction = 'left' if event.key == pygame.K_RIGHT: x_change = 5 direction = 'right' if event.key == pygame.K_UP: y = 300 if event.key == pygame.K_ESCAPE: game_exit = True if event.type == pygame.KEYUP: if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT: x_change = 0 elif event.key == pygame.K_UP: y = 360 x += x_change if x < 0: x = 0 elif x > DISPLAY_SIZE[0]: x = DISPLAY_SIZE[0] screen.fill(BLUE) pygame.draw.rect(screen,GROUND_COLOR,(0,500,800,500)) pygame.draw.rect(screen,GREEN,470,30)) draw_cloud(x_cloud,y_cloud) draw_cloud(x_cloud_two,y_cloud_two) draw_mario(x,direction,state) # Increase the state to animate Mario state += 1 state %= MAX_STATES # Start back from 0 if we have reached the end pygame.display.update() clock.tick(60) if __name__ == '__main__': pygame.init() screen = pygame.display.set_mode(DISPLAY_SIZE) pygame.display.set_caption(TITLE) game_loop() pygame.quit() 主要用于从终端窗口关闭python解释器。
Sprite

您应该将mario和cloud更改为Sprite s。 rect是具有imagescreen.blit(IMAGE,POSITION)属性的类,这些属性定义了绘制方式,然后您不告诉我们使用image而是告诉pygame绘制精灵然后他将使用这两个属性在rect的位置绘制所需的const strapi = require('strapi'); strapi(/* your config */).start();

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...