100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > Python版基于pygame的玛丽快跑小游戏源代码 玛丽冒险小游戏代码 支持双人模式

Python版基于pygame的玛丽快跑小游戏源代码 玛丽冒险小游戏代码 支持双人模式

时间:2022-12-31 13:43:54

相关推荐

Python版基于pygame的玛丽快跑小游戏源代码 玛丽冒险小游戏代码 支持双人模式

基于pygame的玛丽快跑小游戏源代码,玛丽冒险小游戏代码,支持双人模式

按空格进入单人模式,按‘t’进入双人模式,双人模式下玛丽1采用空格键上跳,玛丽2采用方向上键上跳。

完整代码下载地址:Python版基于pygame的玛丽快跑小游戏源代码

import pygameimport randomfrom pygame.locals import *import sysimport osFPS = 30from itertools import cycleclass MyMap():def __init__(self, x, y,path):self.bg = pygame.image.load(path).convert_alpha()self.x = xself.y = ydef map_rolling(self,point):if self.x < point:self.x = -(point)+1else:self.x -= 3def map_update(self):SCREEN.blit(self.bg, (self.x, self.y))class Stop_Button():is_start = Truedef __init__(self):self.start_img = pygame.image.load('image/stopbutton.png').convert_alpha()#self.stop_img = pygame.image.load('image/stop.png').convert_alpha()def is_select(self):point_x, point_y = pygame.mouse.get_pos()w, h = self.start_img.get_size()in_x = point_x > 60 and point_x < 60 + win_y = point_y > 20 and point_y < 20 + hreturn in_x and in_yclass Music_Button():is_open = Truedef __init__(self):self.open_img = pygame.image.load('image/btn_open.png').convert_alpha()self.close_img = pygame.image.load('image/btn_close.png').convert_alpha()self.bg_music = pygame.mixer.Sound('audio/bg_music.wav')def is_select(self):point_x, point_y = pygame.mouse.get_pos()w, h = self.open_img.get_size()in_x = point_x > 20 and point_x < 20 + win_y = point_y > 20 and point_y < 20 + hreturn in_x and in_yclass Marie():def __init__(self,lowest_y):# 初始化小玛丽矩形self.rect = pygame.Rect(0, 0, 0, 0)self.jumpState = Falseself.jumpHeight = 130self.lowest_y = lowest_yself.jumpValue = 0self.marieIndex = 0self.marieIndexGen = cycle([0, 1, 2])# 加载小玛丽图片self.adventure_img = (pygame.image.load("image/adventure1.png").convert_alpha(),pygame.image.load("image/adventure2.png").convert_alpha(),pygame.image.load("image/adventure3.png").convert_alpha(),)self.jump_audio = pygame.mixer.Sound('audio/jump.wav')self.rect.size = self.adventure_img[0].get_size()self.x = 50self.y = lowest_yself.rect.topleft = (self.x, self.y)self.mrect = self.adventure_img[0].get_rect()def jump(self):self.jumpState = Truedef move(self):if self.jumpState:if self.rect.y >= self.lowest_y:self.jumpValue = -5if self.rect.y <= self.lowest_y - self.jumpHeight:self.jumpValue = 5self.rect.y += self.jumpValueif self.rect.y >= self.lowest_y:self.jumpState = Falsedef draw_marie(self):marieIndex = next(self.marieIndexGen)SCREEN.blit(self.adventure_img[marieIndex],(self.x, self.rect.y))class Marie2():def __init__(self,lowest_y):self.rect = pygame.Rect(0, 0, 0, 0)self.jumpState = Falseself.jumpHeight = 130self.lowest_y = lowest_yself.jumpValue = 0self.marieIndex = 0self.marieIndexGen = cycle([0, 1, 2, 3])self.adventure_img = (pygame.image.load("image/bros1.png").convert_alpha(),pygame.image.load("image/bros3.png").convert_alpha(),pygame.image.load("image/bros3.png").convert_alpha(),pygame.image.load("image/bros4.png").convert_alpha(),)self.jump_audio = pygame.mixer.Sound('audio/jump.wav')self.rect.size = self.adventure_img[0].get_size()self.x = 80;self.y = lowest_y;self.rect.topleft = (self.x, self.y)def jump(self):self.jumpState = Truedef move(self):if self.jumpState:if self.rect.y >= self.lowest_y:self.jumpValue = -5if self.rect.y <= self.lowest_y - self.jumpHeight:self.jumpValue = 5self.rect.y += self.jumpValueif self.rect.y >= self.lowest_y:self.jumpState = Falsedef draw_marie(self):marieIndex = next(self.marieIndexGen)SCREEN.blit(self.adventure_img[marieIndex],(self.x, self.rect.y))class Marie3():def __init__(self,lowest_y):self.rect = pygame.Rect(0, 0, 0, 0)self.jumpState = Falseself.jumpHeight = 130self.lowest_y = lowest_yself.jumpValue = 0self.marieIndex = 0self.marieIndexGen = cycle([0, 1, 2, 3])self.adventure_img = (pygame.image.load("image/p1.png").convert_alpha(),pygame.image.load("image/p2.png").convert_alpha(),pygame.image.load("image/p3.png").convert_alpha(),pygame.image.load("image/p4.png").convert_alpha(),)self.jump_audio = pygame.mixer.Sound('audio/jump.wav')self.rect.size = self.adventure_img[0].get_size()self.x = 80;self.y = lowest_y;self.rect.topleft = (self.x, self.y)def jump(self):self.jumpState = Truedef move(self):if self.jumpState:if self.rect.y >= self.lowest_y:self.jumpValue = -8if self.rect.y <= self.lowest_y - self.jumpHeight:self.jumpValue = 8self.rect.y += self.jumpValueif self.rect.y >= self.lowest_y:self.jumpState = Falsedef draw_marie(self):marieIndex = next(self.marieIndexGen)SCREEN.blit(self.adventure_img[marieIndex],(self.x, self.rect.y))class Marie4():def __init__(self,lowest_y):self.jumpState = Falseself.runState = Falseself.image = (pygame.image.load("image/adventure1.png").convert_alpha(),pygame.image.load("image/adventure2.png").convert_alpha(),pygame.image.load("image/adventure3.png").convert_alpha())self.mrect = self.image[0].get_rect()self.jumpHeight = 130self.mrect.y = lowest_yself.lowest_y = lowest_yself.jumpValue = 0self.jump_audio = pygame.mixer.Sound('audio/jump.wav')self.index = 0self.list = cycle([0,1,2])def jump(self):self.jumpState = Truedef move(self):if self.jumpState:if self.mrect.y >= self.lowest_y:self.jumpValue = -8if self.mrect.y <= self.lowest_y - self.jumpHeight:self.jumpValue = 8self.mrect.y += self.jumpValueif self.mrect.y >= self.lowest_y:self.jumpState = Falsedef draw_marie(self):index = next(self.list)SCREEN.blit(self.image[index],self.mrect)class Bullet():def __init__(self):self.x = 80self.y = 150self.bullet_img = pygame.image.load("image/s.png")self.brect = self.bullet_img.get_rect()def move(self,x,y):self.brect.x = xself.brect.y = ydef draw_Bullet(self):SCREEN.blit(self.bullet_img,self.brect)class Obstacle():score = 1move = 3obstacle_y = 150def __init__(self):self.rect = pygame.Rect(0, 0, 0, 0)self.missile = pygame.image.load("image/missile.png").convert_alpha()self.pipe = pygame.image.load("image/pipe.png").convert_alpha()self.numbers = (pygame.image.load('image/0.png').convert_alpha(),pygame.image.load('image/1.png').convert_alpha(),pygame.image.load('image/2.png').convert_alpha(),pygame.image.load('image/3.png').convert_alpha(),pygame.image.load('image/4.png').convert_alpha(),pygame.image.load('image/5.png').convert_alpha(),pygame.image.load('image/6.png').convert_alpha(),pygame.image.load('image/7.png').convert_alpha(),pygame.image.load('image/8.png').convert_alpha(),pygame.image.load('image/9.png').convert_alpha())self.score_audio = pygame.mixer.Sound('audio/score.wav')r = random.randint(0, 1)if r == 0:self.image = self.missileself.move = 15self.obstacle_y = 100else:self.image = self.pipeself.rect.size = self.image.get_size()self.width, self.height = self.rect.sizeself.x = 800self.y = self.obstacle_yself.rect.center = (self.x, self.y)def obstacle_move(self):self.rect.x -= self.movedef draw_obstacle(self):SCREEN.blit(self.image, (self.rect.x, self.rect.y))def getScore(self):self.scoretmp = self.score;if tmp == 1:self.score_audio.play()self.score = 0;return tmp;def showScore(self, score,SCREENWIDTH,SCREENHEIGHT):self.scoreDigits = [int(x) for x in list(str(score))]totalWidth = 0 # 要显示的所有数字的总宽度for digit in self.scoreDigits:# 获取积分图片的宽度totalWidth += self.numbers[digit].get_width()# 分数横向位置Xoffset = (SCREENWIDTH - (totalWidth+30))for digit in self.scoreDigits:# 绘制分数SCREEN.blit(self.numbers[digit], (Xoffset, SCREENHEIGHT * 0.1))# 随着数字增加改变位置Xoffset += self.numbers[digit].get_width()

完整代码下载地址:Python版基于pygame的玛丽快跑小游戏源代码

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。