不可预测的敌人速度变化
Unpredictable enemy speed changes
我做了一个游戏,敌人追逐玩家,而玩家通过地图收集硬币。当我 运行 它处于空闲状态时它工作正常但是当我 运行 它作为一个应用程序时(使用 auto-py-to-exe)敌人的运动已经 运行domly 改变了..现在他们正在以非常高的速度移动。我不确定错误可能出在哪里。我发现更改屏幕大小或添加打印语句也会以某种方式影响这一点。你能提出一个解释吗?...我是 pygame 的新手,大部分游戏都是通过文档
逐步完成的
这是我的完整代码
import sys, pygame,time
import numpy as np
import math
pygame.init()
size = width, height = 410,310
screen = pygame.display.set_mode(size)
screen.set_colorkey((255,255,255))
pygame.display.set_caption('Game')
player = pygame.Surface((68,72))
player.set_colorkey((0,0,0))
bg=pygame.image.load("vf.jpg").convert_alpha()
bg.set_colorkey((255,255,255))
bgrect = bg.get_rect()
ash=pygame.image.load("ash.png").convert_alpha()
ashrect = ash.get_rect()
ashrect.x,ashrect.y=205,155
ashrect.w,ashrect.h=10,10
en1=pygame.image.load("charmander.png").convert_alpha()
en1rect = en1.get_rect()
en1rect.x,en1rect.y=500,200
en1rect.w,en1rect.h=10,10
en2=pygame.image.load("squirtle.png").convert_alpha()
en2rect = en2.get_rect()
en2rect.x,en2rect.y=500,700
en2rect.w,en2rect.h=10,10
en3=pygame.image.load("bulbasaur.png").convert_alpha()
en3rect = ash.get_rect()
en3rect.x,en3rect.y=100,500
en3rect.w,en3rect.h=10,10
poke=pygame.image.load("poke.png").convert_alpha()
pokerect = poke.get_rect()
pokerect.x,pokerect.y=100,100
score=0
x,y=0,0
def enSpeed(enx,eny,asx,asy,bs):
if asx==enx and asy==eny:
return [0,0]
else:
dirvect=pygame.math.Vector2(asx-enx,asy-eny)
if dirvect.magnitude()<=200 or not bgrect.collidepoint((enx,eny)):
dirvect.normalize_ip()
dirvect.scale_to_length(5)
spd=pygame.math.Vector2(dirvect[0]-bs[0],dirvect[1]-bs[1])
spd.scale_to_length(5)
return spd
else:
print("test print")
return [-bs[0],-bs[1]]
bgspd=[0,0]
while 1:
keys=pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.mixer.music.stop()
if bgrect.colliderect(ashrect):
if keys[pygame.K_d]:
bgspd=[10,0]
bgrect.move_ip([-10,0])
pokerect.move_ip([-10,0])
player.fill((0,0,0))
x,y=-1,-2
if keys[pygame.K_a]:
bgspd=[-10,0]
bgrect.move_ip([10,0])
pokerect.move_ip([10,0])
player.fill((0,0,0))
x,y=-1,-1
if keys[pygame.K_s]:
bgspd=[0,10]
bgrect.move_ip([0,-10])
pokerect.move_ip([0,-10])
player.fill((0,0,0))
x,y=-1,0
if keys[pygame.K_w]:
bgspd=[0,-10]
bgrect.move_ip([0,10])
pokerect.move_ip([0,10])
player.fill((0,0,0))
x,y=-3,-3
if event.type==pygame.KEYUP:
x+=1
bgspd=[0,0]
if pokerect.colliderect(ashrect):
pokerect.x=np.random.randint(bgrect.x,bgrect.x+900)
pokerect.y=np.random.randint(bgrect.y,bgrect.y+1100)
score+=1
if en1rect.colliderect(ashrect) or en2rect.colliderect(ashrect)or en3rect.colliderect(ashrect) :
gameOver()
else:
gameOver()
if en1rect.colliderect(en2rect):
en1rect.move_ip([np.random.randint(-100,100),np.random.randint(-100,100)])
en2rect.move_ip([np.random.randint(-100,100),np.random.randint(-100,100)])
if en2rect.colliderect(en3rect):
en3rect.move_ip([np.random.randint(-100,100),np.random.randint(-100,100)])
en2rect.move_ip([np.random.randint(-100,100),np.random.randint(-100,100)])
if en3rect.colliderect(en1rect):
en3rect.move_ip([np.random.randint(-100,100),np.random.randint(-100,100)])
en1rect.move_ip([np.random.randint(-100,100),np.random.randint(-100,100)])
en1rect.move_ip(enSpeed(en1rect.x,en1rect.y,ashrect.x,ashrect.y,bgspd))
en2rect.move_ip(enSpeed(en2rect.x,en2rect.y,ashrect.x,ashrect.y,bgspd))
en3rect.move_ip(enSpeed(en3rect.x,en3rect.y,ashrect.x,ashrect.y,bgspd))
screen.blit(bg,bgrect)
player.blit(ash,(68*x,72*y))
screen.blit(player,(205,155))
screen.blit(poke,pokerect)
screen.blit(en1,en1rect)
screen.blit(en2,en2rect)
screen.blit(en3,en3rect)
screen.blit(pygame.font.Font.render(ft,"SCORE: "+str(score),True,(255,255,255)),dest=(200,0))
pygame.display.flip()
对预期游戏的粗略描述是玩家可以在整个地图上向任何方向移动并收集 pokeballs(硬币)以赚取 score.If 玩家处于敌人 运行ge敌人将开始追逐玩家,否则敌人会继续存在 stationary.The 当玩家被抓住或玩家离开背景地图时游戏结束
您的代码目前 运行 已经尽可能快了。您必须限制游戏速度 (FPS)。游戏中的 FPS(每秒帧数)通常设置为 60。幸运的是,这在 pygame.
中非常简单。
import pygame
from sys import exit
FPS = 60
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
clock.tick(FPS)
我做了一个游戏,敌人追逐玩家,而玩家通过地图收集硬币。当我 运行 它处于空闲状态时它工作正常但是当我 运行 它作为一个应用程序时(使用 auto-py-to-exe)敌人的运动已经 运行domly 改变了..现在他们正在以非常高的速度移动。我不确定错误可能出在哪里。我发现更改屏幕大小或添加打印语句也会以某种方式影响这一点。你能提出一个解释吗?...我是 pygame 的新手,大部分游戏都是通过文档
逐步完成的这是我的完整代码
import sys, pygame,time
import numpy as np
import math
pygame.init()
size = width, height = 410,310
screen = pygame.display.set_mode(size)
screen.set_colorkey((255,255,255))
pygame.display.set_caption('Game')
player = pygame.Surface((68,72))
player.set_colorkey((0,0,0))
bg=pygame.image.load("vf.jpg").convert_alpha()
bg.set_colorkey((255,255,255))
bgrect = bg.get_rect()
ash=pygame.image.load("ash.png").convert_alpha()
ashrect = ash.get_rect()
ashrect.x,ashrect.y=205,155
ashrect.w,ashrect.h=10,10
en1=pygame.image.load("charmander.png").convert_alpha()
en1rect = en1.get_rect()
en1rect.x,en1rect.y=500,200
en1rect.w,en1rect.h=10,10
en2=pygame.image.load("squirtle.png").convert_alpha()
en2rect = en2.get_rect()
en2rect.x,en2rect.y=500,700
en2rect.w,en2rect.h=10,10
en3=pygame.image.load("bulbasaur.png").convert_alpha()
en3rect = ash.get_rect()
en3rect.x,en3rect.y=100,500
en3rect.w,en3rect.h=10,10
poke=pygame.image.load("poke.png").convert_alpha()
pokerect = poke.get_rect()
pokerect.x,pokerect.y=100,100
score=0
x,y=0,0
def enSpeed(enx,eny,asx,asy,bs):
if asx==enx and asy==eny:
return [0,0]
else:
dirvect=pygame.math.Vector2(asx-enx,asy-eny)
if dirvect.magnitude()<=200 or not bgrect.collidepoint((enx,eny)):
dirvect.normalize_ip()
dirvect.scale_to_length(5)
spd=pygame.math.Vector2(dirvect[0]-bs[0],dirvect[1]-bs[1])
spd.scale_to_length(5)
return spd
else:
print("test print")
return [-bs[0],-bs[1]]
bgspd=[0,0]
while 1:
keys=pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.mixer.music.stop()
if bgrect.colliderect(ashrect):
if keys[pygame.K_d]:
bgspd=[10,0]
bgrect.move_ip([-10,0])
pokerect.move_ip([-10,0])
player.fill((0,0,0))
x,y=-1,-2
if keys[pygame.K_a]:
bgspd=[-10,0]
bgrect.move_ip([10,0])
pokerect.move_ip([10,0])
player.fill((0,0,0))
x,y=-1,-1
if keys[pygame.K_s]:
bgspd=[0,10]
bgrect.move_ip([0,-10])
pokerect.move_ip([0,-10])
player.fill((0,0,0))
x,y=-1,0
if keys[pygame.K_w]:
bgspd=[0,-10]
bgrect.move_ip([0,10])
pokerect.move_ip([0,10])
player.fill((0,0,0))
x,y=-3,-3
if event.type==pygame.KEYUP:
x+=1
bgspd=[0,0]
if pokerect.colliderect(ashrect):
pokerect.x=np.random.randint(bgrect.x,bgrect.x+900)
pokerect.y=np.random.randint(bgrect.y,bgrect.y+1100)
score+=1
if en1rect.colliderect(ashrect) or en2rect.colliderect(ashrect)or en3rect.colliderect(ashrect) :
gameOver()
else:
gameOver()
if en1rect.colliderect(en2rect):
en1rect.move_ip([np.random.randint(-100,100),np.random.randint(-100,100)])
en2rect.move_ip([np.random.randint(-100,100),np.random.randint(-100,100)])
if en2rect.colliderect(en3rect):
en3rect.move_ip([np.random.randint(-100,100),np.random.randint(-100,100)])
en2rect.move_ip([np.random.randint(-100,100),np.random.randint(-100,100)])
if en3rect.colliderect(en1rect):
en3rect.move_ip([np.random.randint(-100,100),np.random.randint(-100,100)])
en1rect.move_ip([np.random.randint(-100,100),np.random.randint(-100,100)])
en1rect.move_ip(enSpeed(en1rect.x,en1rect.y,ashrect.x,ashrect.y,bgspd))
en2rect.move_ip(enSpeed(en2rect.x,en2rect.y,ashrect.x,ashrect.y,bgspd))
en3rect.move_ip(enSpeed(en3rect.x,en3rect.y,ashrect.x,ashrect.y,bgspd))
screen.blit(bg,bgrect)
player.blit(ash,(68*x,72*y))
screen.blit(player,(205,155))
screen.blit(poke,pokerect)
screen.blit(en1,en1rect)
screen.blit(en2,en2rect)
screen.blit(en3,en3rect)
screen.blit(pygame.font.Font.render(ft,"SCORE: "+str(score),True,(255,255,255)),dest=(200,0))
pygame.display.flip()
对预期游戏的粗略描述是玩家可以在整个地图上向任何方向移动并收集 pokeballs(硬币)以赚取 score.If 玩家处于敌人 运行ge敌人将开始追逐玩家,否则敌人会继续存在 stationary.The 当玩家被抓住或玩家离开背景地图时游戏结束
您的代码目前 运行 已经尽可能快了。您必须限制游戏速度 (FPS)。游戏中的 FPS(每秒帧数)通常设置为 60。幸运的是,这在 pygame.
中非常简单。import pygame
from sys import exit
FPS = 60
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
clock.tick(FPS)