-3

这是小行星游戏的源代码。我使用了本教程http://comp4431.wordpress.com/2010/08/11/tut-wk-3-asteroids/ 尽可能地紧跟一切。除了一些缺失的特征,比如火,它有什么问题?除了星图,它不会显示任何东西。顺便说一句 - python 新手

import pygame, random, math
from pygame.locals import *
from math import *

WINFLAGS=0
SCREENRECT=Rect(0,0,800,600) #screen res
NSTARS=100
NASTEROIDS=10

#colors
BLACK=(0,0,0)
WHITE=(255,255,255)
YELLOW=(255,255,0)
TRANSPARENT=(1,2,3)

clock=pygame.time.Clock()
while True:
        clock.tick(30)

class Player(pygame.sprite.Sprite):

    color=YELLOW
    thrust_value=1.0
    turn_speed=5
    max_speed=10.0

    base_image=pygame.Surface((21,11))
    base_image.fill(TRANSPARENT)
    base_image.set_colorkey(TRANSPARENT)
    pointlist=[(0,0),(0,10),(20,5)]
    pygame.draw.polygon(base_image,color,pointlist)

    def __init__(self):
            pygame.sprite.Sprite.__init__(self)

            self.image=self.base_image.copy()

            self.rect=self.image.get_rect(center=SCREENRECT.center)

            self.vx=0
            self.vy=0
            self.angle=0

    def update(self):
            self.rect.move_ip(self.vx,self.vy)
            self.wrap()

    def wrap(self):
            if (self.rect.center[0]<0):
                    self.rect.move_ip(SCREENRECT.width,0)
            elif (self.rect.center[0]>=SCREENRECT.width):
                    self.rect.move_ip(-SCREENRECT.width,0)
            if (self.rect.center[1]<0):
                    self.rect.move_ip(SCREENRECT.height,0)
            elif (self.rect.center[1]>=SCREENRECT.height):
                    self.rect.move_ip(0,-SCREENRECT.height)

    def thrust(self):
            a=radians(self.angle)
            self.vx+=self.thrust_value*cos(a)
            self.vy+=self.thrust_value*sin(a)

            vel=sqrt(self.vx*self.vx+self.vy*self.vy)
            if (vel>self.max_speed):
                    self.vx=self.vx*self.max_speed/vel
                    self.vy=self.vy*self.max_speed/vel

    def turn(self,direction):
            self.angle+=self.turn_speed*direction
            while (self.angle<0):
                    self.angle+=360
            while (self.angle>=360):
                    self.angle-=360

            self.image=pygame.transform.rotate(self.base_image,-self.angle)
            self.rect=self.image.get_rect(center=self.rect.center)

    def fire(self):
            print 'fire'

class Asteroid(pygame.sprite.Sprite):

    color=YELLOW
    min_speed=2
    max_speed=6

    base_image = pygame.Surface((40,50))
    base_image.fill(TRANSPARENT)
    base_image.set_colorkey(TRANSPARENT)
    pointlist=[(10,0),(30,0),(40,15),(35,25),(20,50),(5,50),(0,25)]
    pygame.draw.polygon(base_image,color,pointlist)

    def __init__(self):
            pygame.sprite.Sprite.__init__(self)

            self.image=self.base_image.copy()

            x=int(SCREENRECT.width*random.random())
            y=int(SCREENRECT.height*random.random())
            self.rect=self.image.get_rect(center=(x,y))

            velocity=random.randint(self.min_speed, self.max_speed)
            angle=2.0*pi*random.random()#radians
            self.vx=cos(angle)*velocity
            self.vy=sin(angle)*velocity

    def update(self):
            self.rect.move_ip(self.vx,self.vy)
            self.wrap()

    def wrap(self):
            if (self.rect.center[0]<0):
                    self.rect.move_ip(SCREENRECT.width,0)
            elif (self.rect.center[0]>=SCREENRECT.width):
                    self.rect.move_ip(-SCREENRECT.width,0)
            if (self.rect.center[1]<0):
                    self.rect.move_ip(0,SCREENRECT.height)
            elif (self.rect.center[1]>=SCREENRECT.height):
                    self.rect.move_ip(0,-SCREENRECT.height)

def main():
    #init python
    pygame.init()

    #create window
    screen=pygame.display.set_mode(SCREENRECT.size,WINFLAGS)

    #starmap
    background=pygame.Surface(SCREENRECT.size)
    background.fill(BLACK)
    for i in range(0,NSTARS):
            x=random.randint(0,SCREENRECT.width-1)
            y=random.randint(0,SCREENRECT.height-1)
            pygame.draw.circle(background,WHITE,(x,y),0)

    #blit to screen
    screen.blit(background,(0,0))

    pygame.display.update()

    player=Player()

    render=pygame.sprite.RenderUpdates()
    render.add(player)

    asteroids=pygame.sprite.Group()
    for i in range(0,NASTEROIDS):
            asteroid=Asteroid()
            render.add(asteroid)
            asteroids.add(asteroid)

    while True:
            for event in pygame.event.get():
                    if event.type == QUIT or \
                    (event.type == KEYDOWN and event.key == K_ESCAPE):
                      return
            keystate=pygame.key.get_pressed()
            if (keystate[K_UP]):
                    player.thrust()
            direction=keystate[K_RIGHT]-keystate[K_LEFT]
            if (direction !=0):
                    player.turn(direction)
            if (keystate[K_SPACE]):
                    player.fire()
            for asteroid in pygame.sprite.spritecollide(player,asteroids,False):
                    asteroid.kill()

    render.update()
    render.clear(screen,background)
    dirty=render.draw(screen)
    pygame.display.update(dirty)

#run main()
if __name__=="__main__": main()
4

1 回答 1

3

这些更新显示的语句应该在主循环内,而不是在它之后。

render.update()
render.clear(screen,background)
dirty=render.draw(screen)
pygame.display.update(dirty)

另外,我很惊讶您的程序在开始附近出现这个无限循环时会做任何事情

while True:
    clock.tick(30)
于 2013-05-09T01:51:34.487 回答