小白一个,今天打书上的一段代码,运行提示出错,但是不知错在哪里,那个大神给指点一二
代码:
import pygame
from pygame.locals import *
from random import randint
class Star(object):
def __init__(self,x,y,speed):
self.x=x
self.y=y
self.speed=speed
def run():
pygame.init()
screen=pygame.display.set_mode((800,600),FULLSCREEN)
stars=[]
for n in range(200):
x=float(randint(0,799))
y=float(randint(0,599))
speed=float(randint(10,300))
stars.append(Star(x,y,speed))
clock=pygame.time.Clock()
White=(255,255,255)
while True:
for event in pygame.event.get():
if event.type==QUIT:
return
if event.type==KEYDOWN:
return
y=float(randint(0,599))
speed=float(randint(10,300))
star=Star(800.,y,speed)
stars.appedn(star)
time_passed=clock.tick()
time_passed_seconds=time_passed/1000.0
screen.fill((0,0,0))
for star in stars:
new_x=star.x-time_passed_seconds*star.speed
pygame.draw.aaline(screen,White,(new_x,star.y),(star.x+1.,star.y))
star.x=new_x
def on_screen(star):
return star.x>0
stars=filter(on_screen,stars)
pygame.display.update()
if __name__=='__main__':
run()
代码:
import pygame
from pygame.locals import *
from random import randint
class Star(object):
def __init__(self,x,y,speed):
self.x=x
self.y=y
self.speed=speed
def run():
pygame.init()
screen=pygame.display.set_mode((800,600),FULLSCREEN)
stars=[]
for n in range(200):
x=float(randint(0,799))
y=float(randint(0,599))
speed=float(randint(10,300))
stars.append(Star(x,y,speed))
clock=pygame.time.Clock()
White=(255,255,255)
while True:
for event in pygame.event.get():
if event.type==QUIT:
return
if event.type==KEYDOWN:
return
y=float(randint(0,599))
speed=float(randint(10,300))
star=Star(800.,y,speed)
stars.appedn(star)
time_passed=clock.tick()
time_passed_seconds=time_passed/1000.0
screen.fill((0,0,0))
for star in stars:
new_x=star.x-time_passed_seconds*star.speed
pygame.draw.aaline(screen,White,(new_x,star.y),(star.x+1.,star.y))
star.x=new_x
def on_screen(star):
return star.x>0
stars=filter(on_screen,stars)
pygame.display.update()
if __name__=='__main__':
run()