我對這個網站真的很陌生,所以請耐心等待。我目前正在爲一個學校項目製作一個類似於塗鴉的遊戲,並且我一直無法讓玩家一直上下跳動。問題是,如果跳躍的命令行執行超過幾秒鐘,它會開始變得不一致(不能在正確的高度着陸,跳躍距離減少......)。我能想到的唯一可能導致這種不一致的是clock.tick()
函數。請注意,您必須按空間才能啓動此過程。任何關於改善遊戲的建議和單元碰撞的提示以及隨着玩家進步而使屏幕「向上移動」也是受歡迎的。我再一次道歉,因爲我是pygame和本站的絕對新手。clock.tick()中的差異?
bif = "Background.jpg"
mif = "Ball.png"
import pygame, sys
import random
import os
from pygame.locals import*
import time
class Ball(object):
def __init__(self):
x = 320
y = 460
self.dx = 0
self.dy = 0
self.g = 0
self.rect = pygame.Rect(x,y,16,16)
def update(self):
self.rect.x += self.dx
self.rect.y += self.dy
self.dx = 0
self.dy = 0
def aground(self):
if self.g <= 500:
return True
elif self.g > 500:
return False
def moveleft(self):
if self.rect.x > 640:
self.rect.x = 0
elif self.rect.x < 0:
self.rect.x = 640
self.dx = (-(speed*seconds))
self.update()
def moveright(self):
if self.rect.x > 640:
self.rect.x = 0
elif self.rect.x < 0:
self.rect.x = 640
self.dx = (speed*seconds)
self.update()
def jump(self):
if self.g == 1000:
self.g = 0
if self.aground() == True:
self.dy = -(speed*seconds)
self.g += 1
self.update()
elif self.aground() == False:
self.dy = (speed*seconds)
self.g += 1
self.update()
pygame.init()
screen = pygame.display.set_mode((640,480),0,32)
background = pygame.image.load(bif).convert()
ball = pygame.image.load(mif).convert_alpha()
x = 0
a = 0
y = 480
player = Ball()
clock = pygame.time.Clock()
speed = 1000
j = 0
start = 0
init = 0
while True:
screen.blit(background,(0,0))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
key = pygame.key.get_pressed()
if key[pygame.K_LEFT]:
player.moveleft()
if key[pygame.K_RIGHT]:
player.moveright()
if key[pygame.K_SPACE]:
init = 1
if init == 1:
player.jump()
milli = clock.tick()
seconds = milli/1000.
if a%10 == 0:
pygame.draw.rect(background,(140,240,130), Rect((x,y),(70,30)))
a +=12
x = random.randint(0,640)
y -= 15
pygame.draw.rect(screen,(255, 200, 0),player.rect)
pygame.display.update()
而不是......無論是用於跳躍的瘋狂系統,你都要實現一個合適的重力系統,例如, 1)如果你在地面上,跳躍的作品,並設置你的向上速度爲Y 2)每一幀,你上升Y,但Y由重力減少,G 3)如果你碰撞一個地板,那麼你現在接地 - 將Y設置爲0並停止更改,等等。 – Patashu 2013-05-02 03:18:33