• 周一. 4月 22nd, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

为什么我的雪碧在跳?

[db:作者]

3月 7, 2023

我试着跟着这个视频:https://www.youtube.com/watch?v=pN9pBx5ln40&index=20&list=PLsk-HSGFjnaH5yghzu7PcOzm9NhsW0Urw

然而,当我运行我的程序时,我的角色注定要保持静止,但由于某些原因它会反弹

这就是它的样子:https://gyazo.com/015b20c0e4ad64ff3905cbef5865eedc

主.py

import pygame
from map import *
from char import *

pygame.init()

class game():
    def __init__(self):
        self.width = 1280
        self.height = 720
        self.gameRunning = True
        self.clock = pygame.time.Clock()
        self.FPS = 60
        self.screen = pygame.display.set_mode((self.width, self.height))
        self.loadMapData()

    def update(self):

        self.screen.fill(white)
        self.screen.blit(self.map_img, (0,528))
        playerGroup.draw(self.screen)
        pygame.display.update()


    def gameLoop(self):

        self.clock.tick(self.FPS)
        self.event()
        player1.move()
        self.update()

    def event(self):

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.gameRunning = False

    def loadMapData(self):

        self.map = TiledMap()
        self.map_img = self.map.make_mapSurface()
        for tile_object in self.map.gameMap.objects:
            if tile_object.name == "Floor":
                Collision(tile_object.x, tile_object.y + 528, tile_object.width, tile_object.height)


g = game()
player1 = player()
while g.gameRunning == True:
    g.gameLoop()

地图.py

import pygame
import pytmx
from char import *

pygame.init()

class TiledMap():
    def __init__(self):
        self.gameMap = pytmx.load_pygame("CollisionMap.tmx")
        self.mapwidth = self.gameMap.tilewidth * self.gameMap.width
        self.mapheight = self.gameMap.tileheight * self.gameMap.height

    def renderMap(self, surface):
        for layer in self.gameMap.visible_layers:
            if isinstance(layer, pytmx.TiledTileLayer):
                for x,y,gid in layer:
                    tile = self.gameMap.get_tile_image_by_gid(gid)
                    surface.blit(tile, (x * self.gameMap.tilewidth, y * self.gameMap.tileheight))

    def make_mapSurface(self):

        mapSurface = pygame.Surface((self.mapwidth, self.mapheight), pygame.SRCALPHA)
        self.renderMap(mapSurface)
        return mapSurface

class Collision(pygame.sprite.Sprite):
    def __init__(self, x, y, width, height):
        self.groups = SolidGroup
        pygame.sprite.Sprite.__init__(self, self.groups)
        self.rect = pygame.Rect(x, y, width, height)

夏比

import pygame

pygame.init()

black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
playerGroup = pygame.sprite.Group()
SolidGroup = pygame.sprite.Group()

class player(pygame.sprite.Sprite):
    def __init__(self):
        self.groups = playerGroup
        pygame.sprite.Sprite.__init__(self, self.groups)
        self.image = pygame.Surface((50, 50))
        self.image.fill(blue)
        self.rect = self.image.get_rect()
        self.rect.center = (1280/2, 720/2)
        self.position = pygame.math.Vector2(400, 300)
        self.acceleration = pygame.math.Vector2(0, 0)
        self.velocity = pygame.math.Vector2(0, 0)
        self.friction = -0.18

    def move(self):
        self.acceleration = pygame.math.Vector2(0, 0.5)
        key = pygame.key.get_pressed()
        if key[pygame.K_RIGHT]:
            self.acceleration.x = 1
        if key[pygame.K_LEFT]:
            self.acceleration.x = -1

        self.acceleration.x += self.velocity.x * self.friction
        self.velocity += self.acceleration
        self.position += self.velocity + 0.5 * self.acceleration

        hits = pygame.sprite.spritecollide(self, SolidGroup, False)
        if hits:
            self.position.y = hits[0].rect.top
            self.velocity.y = 0

        self.rect.midbottom = self.position

Tags:

rectimportselfmapifobjectinitdefwidthpygametileheightspriteaccelerationgamemap1条回答网友

1楼 ·

发布于 2023-03-07 00:10:52

问题是,当你的精灵像这样撞击时,它被设置在平台的顶部:

self.position.y = hits[0].rect.top

但这只是把它放得太高了一点,这样它就会再次下降,等等。简单的解决方法:

self.position.y = hits[0].rect.top + 1

《为什么我的雪碧在跳?》有15个想法
  1. I loved you even more than you’ll say here. The picture is nice and your writing is stylish, but you read it quickly. I think you should give it another chance soon. I’ll likely do that again and again if you keep this walk safe.

  2. I just could not depart your web site prior to suggesting that I really loved the usual info an individual supply in your visitors Is gonna be back regularly to check up on new posts

  3. Brilliant work! The article is both informative and interesting. Have you considered including more images in your next articles? It might enhance the overall reader experience.

  4. of course like your website but you have to check the spelling on several of your posts A number of them are rife with spelling issues and I in finding it very troublesome to inform the reality on the other hand I will certainly come back again

  5. Your article was indeed entertaining. I look forward to further agreeable contributions from you. However, how might we communicate?

  6. For the past few days I’ve been frequently checking out this wonderful site, they produce first-rate content for fans. The site owner works hard to captivate readers. I’m thrilled and hope they continue providing superb material.

  7. This gateway is incredible. The splendid substance displays the manager’s commitment. I’m overwhelmed and envision more such astonishing presents.

  8. I highly advise steer clear of this platform. My personal experience with it has been nothing but dismay and suspicion of deceptive behavior. Exercise extreme caution, or better yet, find an honest platform to meet your needs.

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注