# 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```

0 条评论

• ### pygame-KidsCanCode系列jumpy-part16-enemy敌人

接上回继续，这次我们要给游戏加点难度，增加几个随机出现的敌人，玩家碰到敌人后Game Over。

• ### pygame-KidsCanCode系列jumpy-part10-角色动画(上)

上一节学习如何利用spritesheet加载图片，但是player仍然是一张静态的图片，比较枯燥，我们要让它动起来！

• ### pygame-KidsCanCode系列jumpy-part11-角色动画(下)

接上节继续，上节并没有处理向左走、向右走的动画效果，这节补上，看似很简单，但是有一些细节还是要注意：

• ### Python面向对象(成员)(二)

特点: 在声明的时候. 需要给出self, self必须放在第一个位置

• ### 利用Python编写一个行业专用的小计算器

前言：本文讲述的是如何利用python编程制作一个适用于指定行业的计算器，方便计算结果，涵盖的知识点由Python编写GUI界面程序，利用爬虫采集实时的汇率数据...

• ### PyQt 编程入门（六.3）

from PyQt5.QtCore import QTimer from PyQt5.QtWidgets import * import sys

• ### python的tkinter编程（八）Entry组件的详细介绍，以登录界面作为讲解

写一个按钮，绑定一个方法，当点击这个按钮的时候，就会执行这个方法，在这个方法里面 获取到对应的你输入的值，将获取到的值传到数据库里面进行比对，失败给一个返回的...

• ### PyQt5 非模态对话框(live 型)

本篇介绍非模态“实时”（live）对话框。与上一篇讲的”apply型“非模态对话框的区别是，非模态“实时”（live）对话框没有任何按钮，且所做的任何改变会自动...

• ### PyQt5 对话框 数据验证

本篇介绍PyQt5对话框的数据合法性的验证。有两种验证方式：预防式验证（preventative）和 提交后验证 (post-mortem)。预防式验证适合于单...