中途岛海是本人编写的一个飞机大战游戏。这是向下滚动的背景类,以供给有需要的朋友。
""" 向下滚动的背景 """ __author__ = '李兴球' __date__ = '2019/9/16' import pygame from pygame.locals import * class ScrollBackground: """滚动的背景""" def __init__(self,image,screen): """image:背景图,screen:屏幕""" self.screen = screen self.sh = screen.get_height() self.image0 = image self.image1 = image.copy() print(id(self.image0),id(self.image1)) self.rect0 = self.image0.get_rect(topleft=(0,0)) self.rect1 = self.image1.get_rect(bottomleft=(0,0)) self.dy = 10 def update(self): """更新坐标""" self.rect0.move_ip(0,self.dy) if self.rect0.top >= self.sh : self.rect0.bottom = 0 self.rect1.move_ip(0,self.dy) if self.rect1.top >= self.sh : self.rect1.bottom = 0 def draw(self): self.screen.blit(self.image0,self.rect0) self.screen.blit(self.image1,self.rect1) main(): """主要函数""" bg = 'images/海洋.png' width,height = size = 960,720 screen = pygame.display.set_mode(size) pygame.display.set_caption("中途岛海战向下滚动背景测试程序by李兴球") bg_image = pygame.image.load(bg) scrbg = ScrollBackground(bg_image,screen) clock = pygame.time.Clock() running = True while running: for event in pygame.event.get(): if event.type == QUIT:running = False scrbg.update() screen.fill((0,120,172)) scrbg.draw() pygame.display.update() clock.tick(60) pygame.quit() if __name__ == "__main__": main()
如需要查看完整源代码,请
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)