简体   繁体   English

跳入 pygame + 理解 pygame 数学(矢量)

[英]Jumping in pygame + understanding pygame math (vector)

sprites.py:精灵.py:

import pygame as pg
from settings import *
vec = pg.math.Vector2
# for movement

class Player(pg.sprite.Sprite):
    def __init__(self, game):
        pg.sprite.Sprite.__init__(self)
        self.game = game
        self.image = pg.Surface((30, 40))
        self.image.fill((255, 255, 153))
        self.rect = self.image.get_rect()
        self.rect.center = WIDTH / 2, HEIGHT / 2
        self.pos = vec(WIDTH / 2, HEIGHT / 2)
        self.vel = vec(0, 0)
        self.acc = vec(0, 0)
def jump(self):
    self.rect.x += 1
    hits = pg.sprite.spritecollide(self, self.game.platforms, False)
    self.rect.x -= 1
    if hits:
        self.vel.y = -20

def update(self):
    self.acc = vec(0, PLAYER_GRAV)
    # pouze zryhlení
    keys = pg.key.get_pressed()
    if keys[pg.K_LEFT]:
        self.acc.x = - PLAYER_ACC
    if keys[pg.K_RIGHT]:
        self.acc.x = PLAYER_ACC

    # apply friction
    self.acc.x += self.vel.x * PLAYER_FRICTION
    # pohyb tělesa
    self.vel += self.acc
    self.pos += self.vel + 0.5 * self.acc

    self.rect.midbottom = self.pos

    if self.pos.x > WIDTH:
        self.pos.x = 0
    if self.pos.x < 0:
        self.pos.x = WIDTH

class Platform(pg.sprite.Sprite): def init (self, x, y, w, h): pg.sprite.Sprite. class 平台(pg.sprite.Sprite):def init (self,x,y,w,h):pg.sprite.Sprite。 init (self) self.image = pg.Surface((w, h)) self.image.fill(GREEN) self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y init (self) self.image = pg.Surface((w, h)) self.image.fill(GREEN) self.rect = self.image.get_rect() self.rect.x = x self.rect.y =是的

main.py:主要.py:

import random
import pygame as pg
from settings import *
from sprites import *

class Game:
    def __init__(self):
        # initialize the game window
        self.running = True
        pg.init()
        pg.mixer.init()
        self.screen = pg.display.set_mode((WIDTH, HEIGHT))
        pg.display.set_caption('My game')
        self.clock = pg.time.Clock()

    def new(self):
        # to start a new game, reset the pieces
        self.all_sprites = pg.sprite.Group()
        self.platforms = pg.sprite.Group()
        self.player = Player(self)
        self.all_sprites.add(self.player)
        p1 = Platform(0, HEIGHT - 40, WIDTH, 40)
        p2 = Platform(WIDTH / 2 - 50, HEIGHT * 3 / 4, 100, 20)
        self.platforms.add(p2)
        self.platforms.add(p1)
        self.all_sprites.add(p1)
        self.all_sprites.add(p2)
        self.run()

    def run(self):
        self.playing = True
        while self.playing:
            self.clock.tick(FPS)
            self.events()
            self.update()
            self.draw()

    def update(self):
        self.all_sprites.update()
        hits = pg.sprite.spritecollide(self.player, self.platforms, False)
        if hits:
            self.player.pos.y = hits[0].rect.top
            self.player.rect.midbottom = self.player.pos
            self.player.vel.y = 0

    def events(self):
        for event in pg.event.get():
            if event.type == pg.QUIT:
                if self.playing:
                    self.playing = False
                self.running = False
            if event.type == pg.KEYDOWN:
                if event.key == pg.K_SPACE:
                    self.player.jump()

    def draw(self):
        self.screen.fill(BLACK)
        self.all_sprites.draw(self.screen)
        pg.display.flip()

    def show_start_screen(self):
        pass

    def show_go_screen(self):
        pass


game = Game()
game.show_start_screen()

while game.running:
    game.new()
    game.show_go_screen()

pg.quit()

settings.py:设置.py:

# game settings
WIDTH = 480
HEIGHT = 600
FPS = 60

# player settings
PLAYER_ACC = 0.5
PLAYER_FRICTION = -0.12
PLAYER_GRAV = 0.8

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = ((255, 255, 153))

I have a few questions:我有几个问题:

Question n.问题 n。 1: 1:

Why doesn't the jump method work?为什么跳转方法不起作用? And why is it using velocity -20 to jump?为什么它使用速度-20来跳跃?

Question n.问题 n。 2: I'm not quite sure if I understand the vector coordinates, can someone please try to explain it to me? 2:我不太确定我是否理解矢量坐标,有人可以尝试向我解释一下吗? I think that this is exactly why I have problems understanding the code.我认为这正是我在理解代码时遇到问题的原因。

In pygame, the screen coordinates start at the top left (0,0).在 pygame 中,屏幕坐标从左上角 (0,0) 开始。 Positive Y goes down.正 Y 下降。 Jump is -20 because the player is going up (toward zero).跳跃是-20,因为玩家正在上升(朝向零)。 It looks like the player bounces up when it hits an object.看起来玩家在碰到 object 时会弹起来。

Pygame 屏幕

Concerning the jump issue, the game does not start for (Platform not defined) so I can't help there.关于跳跃问题,游戏没有开始(平台未定义)所以我无能为力。

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

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