python - Cant get my star sheet to work on he game -
can help me i'm trying space background work on game can't seem it, can spot can't or see going wrong. have merged 2 of codes unsure have gone wrong have tried prepare it, blur.
import pygame, random, time import os,sys,random import pygame pygame.locals import * pygame.init() #set 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 variables done = false life = 3 aliens = pygame.sprite.group() allsprites = pygame.sprite.group() bombs = pygame.sprite.group() greenish = [0, 255, 0] white = [255, 255, 255] def disp(phrase, loc, screen, color): # func display text s = font.render(phrase, true, color) screen.blit(s, loc) def draw_star(star): # drawing star # need alter pixel, utilize 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 loop readability stars = [] in range(200): x = random.randint(0, screen.get_width()) y = random.randint(0, screen.get_height()) stars.append([x,y]) # drawing stars star in stars: draw_star(star) screen.fill((0,0,0)) pygame.display.flip() clock.tick(22) # create timer command how screen updates clock = pygame.time.clock() fps = 100 # loads images utilize in game link in 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() = 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) # 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 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 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) # 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() = random.randrange(200) j = self.rect.centerx if == 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 # 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 create sheet of invaders def invade(): j in range(10, 240, 120): in range(5): aliens.add(pi((i*70)+10, j)) def gameover(): message = msg("game over") message.update() player.kill() shot.kill()
pre-game window invade() message = msg("press key play.") allsprites.add(aliens) key = true while key: event in pygame.event.get(): if event.type == pygame.quit: pygame.quit() if event.type == pygame.keydown: item in (aliens): item.kill() key = false allsprites.update() allsprites.draw(screen) message.update() # set loop 40 cycles per sec clock.tick(fps) # update display pygame.display.flip() # main game starts here score = scoreclass() player = gun() shot = blast() invade() allsprites.add(player, aliens, shot) while done==false: 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 nail = 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() 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 loop "fps" cycles per sec clock.tick(fps) # update display pygame.display.flip() # close pygame pygame.quit()
you erasing screen after drawing stars. change:
# drawing stars star in stars: draw_star(star) screen.fill((0,0,0)) pygame.display.flip()
to:
screen.fill((0,0,0)) # drawing stars star in stars: draw_star(star) pygame.display.flip()
python pygame
No comments:
Post a Comment