我对这个网站真的很陌生,所以请多多包涵。我目前正在为一个学校项目开发类似涂鸦跳跃的游戏,但我无法让玩家始终如一地上下跳跃。问题是如果跳跃的命令执行超过几秒钟,它就会开始变得不一致(没有降落在正确的高度,跳跃距离减小......)。我能想到的唯一可能导致这种不一致的是clock.tick()
函数。请注意,您必须按空格键才能启动该过程。任何关于改进游戏的建议以及关于单位碰撞和让屏幕随着玩家的进步而“向上移动”的提示也是受欢迎的。再次道歉,因为我是 pygame 和这个网站的绝对新手。
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()