# pygame-KidsCanCode系列jumpy-part3-重力及碰撞检测

from part_03.settings import *
import pygame as pg

vec = pg.math.Vector2

class Player(pg.sprite.Sprite):
def __init__(self):
pg.sprite.Sprite.__init__(self)
self.image = pg.Surface((30, 30))
self.image.fill(YELLOW)
self.rect = self.image.get_rect()
self.rect.center = WIDTH / 2, HEIGHT / 2
self.pos = self.rect.center
self.vel = vec(0, 0)
self.acc = vec(0, 0)
self.width = self.rect.width
self.height = self.rect.height

def update(self):
# 初始化时，垂直方向加入重力加速度
self.acc = vec(0, PLAYER_GRAVITY)
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

self.acc.x += self.vel.x * PLAYER_FRICTION

self.vel += self.acc
self.pos += self.vel

if self.rect.left > WIDTH:
self.pos.x = 0 - self.width / 2
if self.rect.right < 0:
self.pos.x = WIDTH + self.width / 2
# 碰撞后，方块底部要停在档板上，所以要改成rect.midbottom
self.rect.midbottom = self.pos

# 档板类
class Platform(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


# game options

SIZE = WIDTH, HEIGHT = 360, 480
FPS = 60
DEBUG = True

TITLE = "Jumpy!"

# Player properties
PLAYER_ACC = 0.5
PLAYER_GRAVITY = 0.5  # 重力加速度
PLAYER_FRICTION = -0.06

# define color
BLACK = 0, 0, 0
WHITE = 255, 255, 255
RED = 255, 0, 0
GREEN = 0, 255, 0
BLUE = 0, 0, 255
YELLOW = 255, 255, 0


然后在main.py中使用这个Platform类：

from part_03.sprites import *
from part_03.settings import *

class Game:

def __init__(self):
pg.init()
pg.mixer.init()
self.screen = pg.display.set_mode(SIZE)
pg.display.set_caption(TITLE)
self.clock = pg.time.Clock()
self.running = True
self.playing = False
self.p1 = object

def new(self):
self.all_sprites = pg.sprite.Group()
# 创建一个档板Group
self.platforms = pg.sprite.Group()
self.player = Player()
# 加入2个档板实例
self.p1 = Platform(0, HEIGHT - 40, WIDTH, 40)
p2 = Platform(WIDTH / 2 - 50, HEIGHT / 2 + 100, 100, 20)
g.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
# 碰撞后，将player 垂直方向的速度归0(否则物体还是会继续向下掉)
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

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

def debug(self):
if DEBUG:
font = pg.font.SysFont('Menlo', 25, True)
pos_txt = font.render(
'Pos:(' + str(round(self.player.pos.x, 2)) + "," + str(round(self.player.pos.y, 2)) + ")", 1, GREEN)
vel_txt = font.render(
'Vel:(' + str(round(self.player.vel.x, 2)) + "," + str(round(self.player.vel.y, 2)) + ")", 1, GREEN)
acc_txt = font.render(
'Acc:(' + str(round(self.player.acc.x, 2)) + "," + str(round(self.player.acc.y, 2)) + ")", 1, GREEN)
self.screen.blit(pos_txt, (20, 10))
self.screen.blit(vel_txt, (20, 40))
self.screen.blit(acc_txt, (20, 70))
pg.draw.line(self.screen, WHITE, (0, HEIGHT / 2), (WIDTH, HEIGHT / 2), 1)
pg.draw.line(self.screen, WHITE, (WIDTH / 2, 0), (WIDTH / 2, HEIGHT), 1)

def show_start_screen(self):
pass

def show_go_screen(self):
pass

g = Game()
g.show_start_screen()
while g.running:
g.new()
g.show_go_screen()

pg.quit()


    def update(self):
# 初始化时，垂直方向加入重力加速度
self.acc = vec(0, PLAYER_GRAVITY)
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

self.acc.x += self.vel.x * PLAYER_FRICTION

self.vel += self.acc
self.pos += self.vel

if self.rect.left > WIDTH:
self.pos.x = 0 - self.width / 2
if self.rect.right < 0:
self.pos.x = WIDTH + self.width / 2

# self.rect.midbottom = self.pos
# 校正0.5px上下抖动的问题
if abs(self.rect.bottom - self.pos.y) >= 1:
self.rect.bottom = self.pos.y
self.rect.x = self.pos.x - self.width / 2


posted @ 2019-01-20 21:23  菩提树下的杨过  阅读(...)  评论(...编辑  收藏