2
我正在嘗試使用livewires和pygame編寫遊戲,在那裏我有一個廚師(只有我有圖像,哈哈),避免從天上墜落的岩石。岩石應該隨機掉落。我希望它是一開始就有一塊岩石落下,然後每當你成功躲開一塊岩石時,另有兩塊岩石會掉落,直到你失去。到目前爲止,我所擁有的廚師和一塊石頭正在墜落。然而,由於某些原因,如果精靈發生碰撞,或者如果岩石碰到屏幕底部,遊戲就會結束,而不會像我告訴過的那樣給遊戲發送消息。我很困惑,看不到我做了什麼。我知道我沒有爲2個岩石部分正確編碼,但我甚至無法輕微運行。幫幫我!這是我現在有:'Dodger'型遊戲
from livewires import games, color
import random
games.init(screen_width = 640, screen_height = 480, fps = 50)
class Chef(games.Sprite):
image = games.load_image("chef.bmp")
def __init__(self):
super(Chef, self).__init__(image = Chef.image,
x = games.mouse.x,
bottom = games.screen.height)
def update(self):
self.x = games.mouse.x
if self.left < 0:
self.left = 0
if self.right > games.screen.width:
self.right = games.screen.width
self.check_catch()
def check_catch(self):
for pizza in self.overlapping_sprites:
if not self.bottom>games.screen.height:
self.end_game()
class Rock(games.Sprite):
def update(self):
if self.bottom > games.screen.height:
new_rock=Rock(x=random.randrange(games.screen.width),
y=10,
dy=1)
games.screen.add(new_rock)
def end_game(self):
end_message = games.Message(value = "Game Over",
size = 90,
color = color.red,
x = games.screen.width/2,
y = games.screen.height/2,
lifetime = 5 * games.screen.fps,
after_death = games.screen.quit)
games.screen.add(end_message)
def main():
wall_image = games.load_image("wall.jpg", transparent = False)
games.screen.background = wall_image
the_chef = Chef()
games.screen.add(the_chef)
rock_image=games.load_image("rock.bmp")
the_rock=Rock(image=rock_image,
x=random.randrange(games.screen.width),
y=10,
dy=1)
games.screen.add(the_rock)
games.mouse.is_visible = False
games.screen.event_grab = True
games.screen.mainloop()
main()
好的,謝謝。我的主要問題是,一旦精靈擊中屏幕的底部,遊戲就結束了,但我沒有告訴它! – Jam 2010-04-20 18:22:59
因爲:def check_catch(self): for self.overlapping_sprites: if not self.bottom> games.screen.height: self.end_game() – ninMonkey 2011-04-27 05:34:28