以下是部分代码预览:
"""pygame制作的太空猫捉太空鼠小游戏,用上下左右方向箭头号操作一只猫在太空中去碰撞老鼠的小游戏.""" __author__ = "李兴球" __date__ = "2018年7月" import pygame from pygame.locals import * from random import randint,choice import time class Rat(): def __init__(self,leftImage,rightImage,screen): self.images = [leftImage,rightImage] # 左图与右图 self.imageIndex = randint(0,1) # 图像索引 self.image = self.images[self.imageIndex]# 确定使用哪个图形 self.xspeed = self.imageIndex * 2 -1 # 0左图对应-1往左移动,1右图对应1往右移动 self.yspeed = choice([-2,2]) pass def move(self): if self.status==1: if time.time() - self.moveStartTime > self.moveDelay: self.rect.x = self.rect.x + self.xspeed self.rect.y = self.rect.y + self.yspeed pass def collide(self,cat): return self.rect.colliderect(cat.rect) def draw(self): self.screen.blit(self.image,self.rect) class Cat(): def __init__(self,leftImage,rightImage,screen): self.images = [leftImage,rightImage] # 左图与右图 self.imageIndex = 1 # 开始时面向右的方向 self.image = self.images[self.imageIndex]# 确定使用哪个图形 self.xspeed = 0 # 0左图对应-1往左移动,1右图对应1往右移动 self.yspeed = 0 pass def move(self): self.rect.move_ip(self.xspeed,self.yspeed) def draw(self): self.image = self.images[self.imageIndex] self.screen.blit(self.image,self.rect) def playmusic(): pygame.mixer.music.load("Cave.wav") pygame.mixer.music.play(-1,0) def main(): pygame.init() screen_width,screeh_height=480,360 screen = pygame.display.set_mode((screen_width,screeh_height)) pygame.display.set_caption("pygame太空猫捉太空鼠小游戏_作者:李兴球 www.scratch8.net") 老鼠音 = pygame.mixer.Sound("老鼠音.wav") 背景图 = pygame.image.load("moon.png") 鼠左图像 = pygame.image.load("太空鼠_左.png") 鼠右图像 = pygame.image.load("太空鼠_右.png") rats=[] for i in range(30): rats.append(Rat(鼠左图像,鼠右图像,screen)) 猫左图像 = pygame.image.load("太空猫_左.png") 猫右图像 = pygame.image.load("太空猫_右.png") cat = Cat(猫左图像,猫右图像,screen) clock = pygame.time.Clock() 运行中 = True pass pygame.quit() if __name__ == "__main__": pygame.mixer.init() playmusic() main()
下载完整源代码与素材,请
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)
发表评论