0

任何人都可以帮助我我正在尝试让我的空间背景在游戏中工作,但我似乎无法做到,任何人都可以发现我看不到的东西或看到我哪里出错了。我已经合并了我的两个代码,所以我不确定我哪里出错了,但我尝试修复它,但只是模糊了。

import pygame, random, time
import os,sys,random
import pygame
from pygame.locals import *
pygame.init()

#set up the graphics window
size = [800, 595]
screen = pygame.display.set_mode(size, 0)
screenrect = screen.get_rect()
pygame.display.set_caption("Mathsvaders")
clock = pygame.time.Clock()


# set some variables
done = False
life = 3

aliens = pygame.sprite.Group()
allsprites = pygame.sprite.Group()
bombs = pygame.sprite.Group()
green = [0, 255, 0]
white = [255, 255, 255]

def disp(phrase, loc, screen, color): # func to display text
    s = font.render(phrase, True, color)
    screen.blit(s, loc)

def draw_star(star): # drawing a star
    # you only need to change a pixel, so use set_at, not draw.line
    screen.set_at((star[0], star[1]), (255, 255, 255))
    star[0] -= 1
    if star[0] < 0:
        star[0] = screen.get_width()
        star[1] = random.randint(0, screen.get_height())
# creating list of stars, used multi-line for loop for readability
stars = []
for i in range(200):
    x = random.randint(0, screen.get_width())
    y = random.randint(0, screen.get_height())
    stars.append([x,y])

    # drawing stars
    for star in stars:
        draw_star(star)

    screen.fill((0,0,0))

    pygame.display.flip()
   clock.tick(22)

# create a timer to control how often the screen updates
clock = pygame.time.Clock()
fps = 100

# loads images to use in the game which link in with my classes(further down)
cannon = pygame.image.load("spaceship.png").convert()
cannon.set_colorkey(white)
blast = pygame.image.load("blast.png").convert_alpha()
boom = pygame.image.load("expl.png").convert_alpha()
bomb = pygame.image.load("missile_player.png").convert_alpha()
back = pygame.image.load("rsz_space.png").convert()
enemy = pygame.image.load("sii.png").convert_alpha()
lives2 = pygame.image.load("alien2.png").convert()
lives2.set_colorkey(white)
lives3 = pygame.image.load("alien3.png").convert()
lives3.set_colorkey(white)
lives1 = pygame.image.load("alien1.png").convert()
lives1.set_colorkey(white)


# (Classes)
# the explosion class
class Explosion(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.image = boom
        self.rect = self.image.get_rect()
        self.rect.centerx = x
        self.rect.centery = y
        self.count = 6

    def update(self):
        self.count -= 1
        if self.count < 1:
        self.kill()

class scoreClass:
    def __init__(self):
        self.value = 0
        # set a font, default font size 28
        self.font = pygame.font.Font(None, 28)

    def update(self):
        text = self.font.render("Score: %s" % self.value, True, (green))
        textRect = text.get_rect()
        textRect.centerx = screenrect.centerx
        screen.blit(text, textRect)

class Msg:
        def __init__(self, words):
        # set a font, default font size 28
        self.font = pygame.font.Font(None, 28)
        self.text = self.font.render(words, True, (green))
        self.textRect = self.text.get_rect()

        def update(self):
            self.textRect.centerx = screenrect.centerx
        self.textRect.centery = screenrect.centery
        screen.blit(self.text, self.textRect)

# the invader class
class Pi(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        aliens.add(self)
        self.image = enemy
        self.rect = self.image.get_rect()
        self.rect.left = x
        self.rect.top = y
        self.speed = 1

    def update(self):
            self.rect.right += self.speed
            if self.rect.right >= (screenrect.right -5):
                self.speed = -1
            self.rect.top += self.rect.height
        if self.rect.left <= (screenrect.left +5):
            self.speed = 1
            self.rect.top += self.rect.height
        if self.rect.top > screenrect.bottom:
            self.kill()
        i = random.randrange(200)
        j = self.rect.centerx
        if i == 1:
            laser_bomb = Bomb(j, self.rect.bottom)
            allsprites.add(laser_bomb)
            aliens.add(laser_bomb)

class Gun(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = cannon
        self.rect = self.image.get_rect()
        self.rect.bottom = screenrect.bottom
        self.rect.centerx = screenrect.centerx
        self.speed=0

    def update(self):
        self.rect.centerx += self.speed
        if self.rect.right >= screenrect.right:
            self.rect.centerx = 0
        if self.rect.right <= 0:
            self.rect.right = screenrect.right

# bomb class
class Bomb(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.image = bomb
        self.rect = self.image.get_rect()
        self.rect.centerx = x
        self.rect.centery = y
        bombs.add(self)

    def update(self):
        self.rect.centery +=1

# the laser blast class
class Blast(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = blast
        self.image.set_colorkey(white)
        self.rect = self.image.get_rect()
        self.rect.top = (player.rect.top + 5)
        self.rect.centerx = player.rect.centerx
        self.speed = 0

    def update(self):
        if self.speed == 0:
            self.rect.centerx = player.rect.centerx
        self.rect.top -= self.speed

# function to make a sheet of invaders
def invade():
    for j in range(10, 240, 120):
       for i in range(5):
            aliens.add(Pi((i*70)+10, j))


def gameover():
    message = Msg("Game Over")
    message.update()
    player.kill()
    shot.kill()

赛前窗口

invade()

message = Msg("Press a key to play.")
allsprites.add(aliens)
key = True

while key:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
        if event.type == pygame.KEYDOWN:
            for item in (aliens):
                item.kill()
            key = False

    allsprites.update()
    allsprites.draw(screen)
    message.update()

        # set the loop to 40 cycles per second
    clock.tick(fps)

        #  update the display
    pygame.display.flip()


# Main Game Starts Here

score = scoreClass()
player = Gun()
shot = Blast()
invade()
allsprites.add(player, aliens, shot)


while done==False:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done=True

    if life <= 0:
        gameover()


    else:
        # shoots laser missile
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                if shot.speed == 0:
                    shot.speed = 5
                    #laser.play()

            if event.key == pygame.K_LEFT:
                player.speed = -3
            if event.key == pygame.K_RIGHT:
                player.speed = 3

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                player.speed = 0
            if event.key == pygame.K_RIGHT:
                player.speed = 0

        hit = pygame.sprite.spritecollide(shot, aliens, 1)
        if len(hit) > 0:
            explosion1 = Explosion(shot.rect.centerx, shot.rect.top)
            score.value += 100
            shot.kill()
            #explode.play()
            shot = Blast()
            allsprites.add(shot, explosion1)


        hit2 = pygame.sprite.spritecollide(player, aliens, 1)
        if len(hit2) > 0:
            life -= 1
            #explode.play()
            explosion2 = Explosion(player.rect.centerx, player.rect.centery)
            allsprites.add(explosion2)
            player.kill()
            shot.kill()
            if life > 0:
                ready = Msg("Push Harder !!.")
                ready.update()
                allsprites.update()
                allsprites.draw(screen)
                score.update()
                pygame.display.flip()
                for item in bombs:
                    item.kill()
                while 1:
                    event = pygame.event.wait()
                    if event.type == pygame.KEYDOWN:
                        break
                player = Gun()
                shot = Blast()
                allsprites.add(player, shot)

        if shot.rect.top <= screenrect.top:
            shot.kill()
            shot = Blast()
            allsprites.add(shot)

    if life == 2:
        men = lives2
    if life == 1:
        men = lives1
    if life == 3:
        men = lives3

    if life > 0:
        screen.blit(men, (0,0))

    allsprites.update()
    allsprites.draw(screen)
    score.update()

    # set the loop to "fps" cycles per second
    clock.tick(fps)
    #  update the display
    pygame.display.flip()



# close pygame
pygame.quit()
4

1 回答 1

1

您在绘制星星后正在擦除屏幕。改变:

# drawing stars
for star in stars:
    draw_star(star)

screen.fill((0,0,0))

pygame.display.flip()

到:

screen.fill((0,0,0))

# drawing stars
for star in stars:
    draw_star(star)



pygame.display.flip()
于 2013-02-18T01:08:47.600 回答