我一直在绞尽脑汁,试图找到如何:
出于某种原因,我收到了错误消息:
AttributeError:' module'对象没有属性' sleep'
我找到了一个帖子(https://ubuntuforums.org/showthread.php?t=1129861),其中有此错误消息的人有一个名为time.py的文件。这似乎不是问题所在。
似乎睡眠应该在Pygame(animation in pygame)内部起作用。
我在这里尝试实现的并不一定要停止整个程序,而只是在程序运行时显示不同的图像。
这是我的代码。我完全清楚它远非优雅,我也在努力:
===
# This is my first game demonstration in python
import pygame, sys, random, time
from pygame import *
pygame.init()
# variables globales
global width, height
width, height = 500, 400
pygame.display.set_caption("Test Program")
global player, px, py
player = pygame.image.load("image 1.png") # load the player image
player = pygame.transform.scale(player, (125,125))
px,py = (width - 125)/2, (height - 125)/2 # pour centrer l'image
global DISPLAYSURF
DISPLAYSURF = pygame.display.set_mode((500, 400), 0, 32)
def game():
global player, px, py
# set up the colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# draw on the surface object
DISPLAYSURF.fill(BLACK)
# speed of game and other variables initiated
clock = pygame.time.Clock()
gamespeed = 100
movex = movey = 0
score = 0
# this is the score text loading
gamefont = pygame.font.Font(None, 30)
scoretext = gamefont.render("Player Score: " + str(score), 2, [255,0,0])
boxsize = scoretext.get_rect()
scoreXpos = (width - boxsize[2])/2
# running of the game loop
while True:
# image display updates
seconds = clock.tick()/1000.0
playerImage()
scoretext = gamefont.render("Player Score: " + str(score), 2, [255,0,0])
DISPLAYSURF.blit(scoretext, [scoreXpos, 20])
pygame.display.update()
# keyboard and/or mouse movements
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == KEYDOWN:
if event.key == K_RIGHT:
movex = 2
if event.key == K_LEFT:
movex = -2
if event.key == K_UP:
movey = -2
if event.key == K_DOWN:
movey = 2
elif event.type == KEYUP:
if event.key == K_RIGHT:
movex = 0
if event.key == K_LEFT:
movex = 0
if event.key == K_UP:
movey = 0
if event.key == K_DOWN:
movey = 0
px = px + movex
py = py + movey
def playerImage():
global player
global px
global py
player = pygame.image.load("image 2.png") # load the player image
DISPLAYSURF.blit(player, (px, py))
player = pygame.transform.scale(player, (125,125))
px,py = (width - 125)/2, (height - 125)/2 # pour centrer l'image
time.sleep(2)
# python's way of running the main routine
if __name__ == "__main__":
game()
答案 0 :(得分:0)
你正在调用隐式
pygame.time.sleep
没有sleep
方法而不是
time.sleep
导入time
模块以访问其功能。但请注意不要覆盖脚本中的名称。
使用from xxx import *
通常是个坏主意,只需使用import pygame
或import pygame as xxx
。
答案 1 :(得分:0)
从"时间"在任何体面的游戏中都是非常重要的(事件发生在一段时间后,以毫秒,秒,分钟等表示),I've created a way to deal with it natively in my engine。 (维基远非完整,但您应该查看我的" EwApp"类,因为它提供了完全相同的方法。)。
无论如何,这里的要点是:你应该在不使用python的时候解决这个问题。相反,你应该只使用pygame.time (因为使用Python的时间会在给定的时间内暂停你的整个游戏,并且那是非常残酷的)。我已经开发了一个简单的示例,使用" dt"来调整我的方法,绘制一个红色矩形,然后绘制一个绿色矩形并等待1秒钟,然后返回到初始状态。
import pygame
if __name__ == "__main__":
pygame.init()
done = False
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
SURFACE_SIZE = 300
screen = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
clock = pygame.time.Clock()
FPS = 1
time_elapsed = 0
def check_if_time_has_elapsed_in_seconds(seconds):
global time_elapsed
if time_elapsed > seconds*1000:
time_elapsed = 0
return True
else:
return False
def get_center(*args):
if len(args) == 2:
return ((args[0]/2)-(args[1]/2))
surf1 = pygame.Surface((SURFACE_SIZE, SURFACE_SIZE))
pygame.draw.rect(surf1, (0, 255, 0), (0, 0, SURFACE_SIZE, SURFACE_SIZE), 0)
surf2 = pygame.Surface((SURFACE_SIZE, SURFACE_SIZE))
pygame.draw.rect(surf2, (255, 0, 0), (0, 0, SURFACE_SIZE, SURFACE_SIZE), 0)
while not done:
dt = clock.tick(FPS)
time_elapsed += dt
pygame.display.flip()
screen.fill((0, 0, 0))
screen.blit(surf1, (get_center(SCREEN_WIDTH, SURFACE_SIZE), get_center(SCREEN_HEIGHT, SURFACE_SIZE)))
if check_if_time_has_elapsed_in_seconds(1):
screen.blit(surf2, (get_center(SCREEN_WIDTH, SURFACE_SIZE), get_center(SCREEN_HEIGHT, SURFACE_SIZE)))
for e in pygame.event.get():
if e.type == pygame.QUIT:
done = True
pygame.quit()
使用我的引擎,这可以很容易地实现。 My engine is open-source and is on github,但这是主要的精神:增加一个" time_elapsed"在主循环内通过dt(ticks)变量,以便检查是否已经过了一些时间。像我一样创建外部函数,使它变得更容易/更方便。这种技术也被称为"冷却"。对于十万件事物很有用(例如,像大量的像素一样移动特征,但是慢慢地(经过几毫秒的冷却后))。