python pygame_python – pygame中的重力

创造一个弹跳球有

a tutorial我认为可能对你有所帮助.

现在,为了将重力添加到该模拟中,您只需在每次循环时在y方向上添加一些额外的速度:

speed[1] += gravity

然而,你最终得到的是一种傻瓜,因为图像很快就会下降到窗口底部以下,再也看不到了:)

因此,下一步是剪切球的位置,使其必须保留在窗口中:

import os

import sys, pygame

pygame.init()

size = width, height = 320, 240

speed = [1, 1]

black = 0, 0, 0

gravity = 0.1

screen = pygame.display.set_mode(size)

image_file = os.path.expanduser("~/pybin/pygame_examples/data/ball.png")

ball = pygame.image.load(image_file)

ballrect = ball.get_rect()

def clip(val, minval, maxval):

return min(max(val, minval), maxval)

while 1:

for event in pygame.event.get():

if event.type == pygame.QUIT: sys.exit()

speed[1] += gravity

ballrect = ballrect.move(speed)

if ballrect.left < 0 or ballrect.right > width:

speed[0] = -speed[0]

if ballrect.top < 0 or ballrect.bottom > height:

speed[1] = -speed[1]

# clip the position to remain in the window

ballrect.left = clip(ballrect.left, 0, width)

ballrect.right = clip(ballrect.right, 0, width)

ballrect.top = clip(ballrect.top, 0, height)

ballrect.bottom = clip(ballrect.bottom, 0, height)

screen.fill(black)

screen.blit(ball, ballrect)

pygame.display.flip()

好的,现在您可以将其合并到当前代码中,然后您就可以开始运行了.但是,您可以采取一些措施来使代码更有条理,更少重复.

例如,考虑下面的大量if … then块

for event in pygame.event.get():

您可以将其重写为:

delta = {

pygame.K_LEFT: (-20, 0),

pygame.K_RIGHT: (+20, 0),

pygame.K_UP: (0, -20),

pygame.K_DOWN: (0, +20),

}

for event in pygame.event.get():

if event.type == pygame.KEYDOWN:

deltax, deltay = delta.get(event.key, (0, 0))

ball.speed[0] += deltax

ball.speed[1] += deltay

您还可以将与图像移动相关的所有逻辑放入类中,从而获益:

class Ball(pygame.sprite.Sprite):

def __init__(self):

pygame.sprite.Sprite.__init__(self)

self.image = pygame.image.load(image_file)

self.rect = self.image.get_rect()

self.speed = [0, 0]

area = pygame.display.get_surface().get_rect()

self.width, self.height = area.width, area.height

def update(self):

self.rect = self.rect.move(self.speed)

if self.rect.left < 0 or self.rect.right > self.width:

self.speed[0] = -self.speed[0]

if self.rect.top < 0 or self.rect.bottom > self.height:

self.speed[1] = -self.speed[1]

self.rect.left = clip(self.rect.left, 0, self.width)

self.rect.right = clip(self.rect.right, 0, self.width)

self.rect.top = clip(self.rect.top, 0, self.height)

self.rect.bottom = clip(self.rect.bottom, 0, self.height)

请注意,update方法与本教程提供的代码非常相似.创建Ball类的好处之一是程序的其余部分不需要了解Ball如何移动.所有逻辑都在Ball.update中.而且,它可以很容易地实例化许多球.你可以创建其他类别(飞机,飞鸟,桨等),这些类也会以不同的方式移动,并相对无痛地将它们添加到您的模拟中.

所以,把它们放在一起,你最终会得到这样的东西:

"""

http://stackoverflow.com/a/15459868/190597 (unutbu)

Based on http://www.pygame.org/docs/tut/intro/intro.html

Draws a red ball bouncing around in the window.

Pressing the arrow keys moves the ball

"""

import sys

import pygame

import os

image_file = os.path.expanduser("~/pybin/pygame_examples/data/ball.png")

delta = {

pygame.K_LEFT: (-20, 0),

pygame.K_RIGHT: (+20, 0),

pygame.K_UP: (0, -20),

pygame.K_DOWN: (0, +20),

}

gravity = +1

class Ball(pygame.sprite.Sprite):

def __init__(self):

pygame.sprite.Sprite.__init__(self)

self.image = pygame.image.load(image_file)

self.rect = self.image.get_rect()

self.speed = [0, 0]

area = pygame.display.get_surface().get_rect()

self.width, self.height = area.width, area.height

def update(self):

self.rect = self.rect.move(self.speed)

if self.rect.left < 0 or self.rect.right > self.width:

self.speed[0] = -self.speed[0]

if self.rect.top < 0 or self.rect.bottom > self.height:

self.speed[1] = -self.speed[1]

self.rect.left = clip(self.rect.left, 0, self.width)

self.rect.right = clip(self.rect.right, 0, self.width)

self.rect.top = clip(self.rect.top, 0, self.height)

self.rect.bottom = clip(self.rect.bottom, 0, self.height)

def clip(val, minval, maxval):

return min(max(val, minval), maxval)

class Main(object):

def __init__(self):

self.setup()

def setup(self):

pygame.init()

size = (self.width, self.height) = (640,360)

self.screen = pygame.display.set_mode(size, 0, 32)

self.ball = Ball()

self.setup_background()

def setup_background(self):

self.background = pygame.Surface(self.screen.get_size())

self.background = self.background.convert()

self.background.fill((0, 0, 0))

self.screen.blit(self.background, (0, 0))

pygame.display.flip()

def draw(self):

self.screen.blit(self.background, (0, 0))

self.screen.blit(self.ball.image, self.ball.rect)

pygame.display.flip()

def event_loop(self):

ball = self.ball

friction = 1

while True:

for event in pygame.event.get():

if ((event.type == pygame.QUIT) or

(event.type == pygame.KEYDOWN and

event.key == pygame.K_ESCAPE)):

sys.exit()

elif event.type == pygame.KEYDOWN:

deltax, deltay = delta.get(event.key, (0, 0))

ball.speed[0] += deltax

ball.speed[1] += deltay

friction = 1

elif event.type == pygame.KEYUP:

friction = 0.99

ball.speed = [friction*s for s in ball.speed]

ball.speed[1] += gravity

ball.update()

self.draw()

pygame.time.delay(10)

if __name__ == '__main__':

app = Main()

app.event_loop()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值