基于pygame的玛丽快跑小游戏源代码,玛丽冒险小游戏代码,支持双人模式
按空格进入单人模式,按‘t’进入双人模式,双人模式下玛丽1采用空格键上跳,玛丽2采用方向上键上跳。
import pygame
import random
from pygame.locals import *
import sys
import os
FPS = 30
from itertools import cycle
class MyMap():
def __init__(self, x, y,path):
self.bg = pygame.image.load(path).convert_alpha()
self.x = x
self.y = y
def map_rolling(self,point):
if self.x < point:
self.x = -(point)+1
else:
self.x -= 3
def map_update(self):
SCREEN.blit(self.bg, (self.x, self.y))
class Stop_Button():
is_start = True
def __init__(self):
self.start_img = pygame.image.load('image/stopbutton.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 + w
in_y = point_y > 20 and point_y < 20 + h
return in_x and in_y
class Music_Button():
is_open = True
def __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 + w
in_y = point_y > 20 and point_y < 20 + h
return in_x and in_y
class Marie():
def __init__(self,lowest_y):
self.rect = pygame.Rect(0, 0, 0, 0)
self.jumpState = False
self.jumpHeight = 130
self.lowest_y = lowest_y
self.jumpValue = 0
self.marieIndex = 0
self.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 = 50
self.y = lowest_y
self.rect.topleft = (self.x, self.y)
self.mrect = self.adventure_img[0].get_rect()
def jump(self):
self.jumpState = True
def move(self):
if self.jumpState:
if self.rect.y >= self.lowest_y:
self.jumpValue = -5
if self.rect.y <= self.lowest_y - self.jumpHeight:
self.jumpValue = 5
self.rect.y += self.jumpValue
if self.rect.y >= self.lowest_y:
self.jumpState = False
def 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 = False
self.jumpHeight = 130
self.lowest_y = lowest_y
self.jumpValue = 0
self.marieIndex = 0
self.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 = True
def move(self):
if self.jumpState:
if self.rect.y >= self.lowest_y:
self.jumpValue = -5
if self.rect.y <= self.lowest_y - self.jumpHeight:
self.jumpValue = 5
self.rect.y += self.jumpValue
if self.rect.y >= self.lowest_y:
self.jumpState = False
def 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 = False
self.jumpHeight = 130
self.lowest_y = lowest_y
self.jumpValue = 0
self.marieIndex = 0
self.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 = True
def move(self):
if self.jumpState:
if self.rect.y >= self.lowest_y:
self.jumpValue = -8
if self.rect.y <= self.lowest_y - self.jumpHeight:
self.jumpValue = 8
self.rect.y += self.jumpValue
if self.rect.y >= self.lowest_y:
self.jumpState = False
def 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 = False
self.runState = False
self.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 = 130
self.mrect.y = lowest_y
self.lowest_y = lowest_y
self.jumpValue = 0
self.jump_audio = pygame.mixer.Sound('audio/jump.wav')
self.index = 0
self.list = cycle([0,1,2])
def jump(self):
self.jumpState = True
def move(self):
if self.jumpState:
if self.mrect.y >= self.lowest_y:
self.jumpValue = -8
if self.mrect.y <= self.lowest_y - self.jumpHeight:
self.jumpValue = 8
self.mrect.y += self.jumpValue
if self.mrect.y >= self.lowest_y:
self.jumpState = False
def draw_marie(self):
index = next(self.list)
SCREEN.blit(self.image[index],self.mrect)
class Bullet():
def __init__(self):
self.x = 80
self.y = 150
self.bullet_img = pygame.image.load("image/s.png")
self.brect = self.bullet_img.get_rect()
def move(self,x,y):
self.brect.x = x
self.brect.y = y
def draw_Bullet(self):
SCREEN.blit(self.bullet_img,self.brect)
class Obstacle():
score = 1
move = 3
obstacle_y = 150
def __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.missile
self.move = 15
self.obstacle_y = 100
else:
self.image = self.pipe
self.rect.size = self.image.get_size()
self.width, self.height = self.rect.size
self.x = 800
self.y = self.obstacle_y
self.rect.center = (self.x, self.y)
def obstacle_move(self):
self.rect.x -= self.move
def draw_obstacle(self):
SCREEN.blit(self.image, (self.rect.x, self.rect.y))
def getScore(self):
self.score
tmp = 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()
完整代码下载地址:
https://download.csdn.net/download/weixin_42756970/87257758
Python代码大全,海量代码任你下载