有很多代码,但大部分都是必要的。它被命名为试图清楚地理解。我试图让它跳跃但是当我击中太空时它只会上升一点并保持在那里。我想挖掘空间并让它跳得相当多,然后用合理的物理学回落。如果有人知道如何做到这一点真的很有帮助。我不确定你如何用pygame做到这一点。
import pygame, sys, time, random
from pygame.locals import *
class Entity(pygame.sprite.Sprite):
"""Inherited by any object in the game."""
def __init__(self, x, y, width, height):
pygame.sprite.Sprite.__init__(self)
self.x = x
self.y = y
self.width = width
self.height = height
# This makes a rectangle around the entity, used for anything
# from collision to moving around.
self.rect = pygame.Rect(self.x, self.y, self.width, self.height)
class Duck(Entity):
"""
Player controlled or AI controlled, main interaction with
the game
"""
def __init__(self, x, y, width, height):
super(Duck, self).__init__(x, y, width, height)
self.image = duck
class Player(Duck):
"""The player controlled Duck"""
def __init__(self, x, y, width, height):
super(Player, self).__init__(x, y, width, height)
# How many pixels the Player duck should move on a given frame.
self.x_change = 0
# How many pixels the spaceship should move each frame a key is pressed.
self.x_dist = 5
def MoveKeyDown(self, key):
"""Responds to a key-down event and moves accordingly"""
if (key == pygame.K_LEFT or key == pygame.K_a):
self.x_change += -self.x_dist
elif (key == pygame.K_RIGHT or key == pygame.K_d):
self.x_change += self.x_dist
elif (key == pygame.K_SPACE):
self.jump()
def MoveKeyUp(self, key):
"""Responds to a key-up event and stops movement accordingly"""
if (key == pygame.K_LEFT or key == pygame.K_a):
self.x_change += self.x_dist
elif (key == pygame.K_RIGHT or key == pygame.K_d):
self.x_change += -self.x_dist
elif (key == pygame.K_SPACE):
self.jump()
def jump(self):
self.rect.y -= 3
def update(self):
"""
Moves the duck while ensuring it stays in bounds
"""
# Moves it relative to its current location.
self.rect.move_ip(self.x_change, 0)
# If the duck moves off the screen, put it back on.
if self.rect.x <= 0:
self.rect.x = 0
elif self.rect.x > window_width:
self.rect.x = window_width
pygame.init()
FPS = 60 # frames per second setting
clock = pygame.time.Clock()
window_width = 1000
window_height = 600
# set up the window
screen = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption('Deterred Journey')
duck = pygame.image.load('walkingduck.png')
duck = pygame.transform.scale(duck, (160, 180))
player = Player(0, (window_height)-175, 120, 160) #Making player object(duck)
all_sprites_list = pygame.sprite.Group()
all_sprites_list.add(player)
while True: # the main game loop
for event in pygame.event.get(): #Closes game
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYUP:
player.MoveKeyUp(event.key)
elif event.type == pygame.KEYDOWN:
player.MoveKeyDown(event.key)
all_sprites_list.draw(screen)
pygame.display.flip()
pygame.display.update()
答案 0 :(得分:0)
有一种非常简单的方法可以为您的游戏实施重力。
首先,输入一个变量来控制你的速度。在此示例中,它是y_speed
。
y_speed = 0
现在,而不是
def jump(self):
self.rect.y -= 3
使用此代码
def jump(self):
y_speed = -3
While true:
y_speed += 1
self.rect.y +-= y_speed
这样可以让你在跳跃的时候总是摔倒,就像现实生活一样,你可能会稍快一点。为了确保您不会从屏幕上掉下来,请执行某种碰撞检测,如下所示:
def jump(self):
y_speed = -3
While true:
y_speed += 1
self.rect.y +-= y_speed
if self.rect.colliderect([some sort of object representing the ground].rect):
y_speed = 0
这是一种非常简单的方法,而且我不会非常使用Classes
,但这应该有助于为您提供基本的重力并能够被操纵以使用您的代码