the second game
立即下载
资源介绍:
使用pygame做的小游戏,跟茶叶蛋差不多!仅供参考!!!对了,此作品是我的原创.........................................................................................................................................................................................................................................................................................................................................................................................................................................................................
import pygame
from pygame import *
from random import choice,randint
import time
from sys import exit
class Player(sprite.Sprite):
def __init__(self):
sprite.Sprite.__init__(self)
self.image=Surface((50,50))
self.image.fill((255,255,255))
self.rect=self.image.get_rect()
self.rect.x=50
self.rect.y=550
self.jumped=False
self.velY=0
def move(self):
xMove=0
yMove=0
keys=key.get_pressed()
if keys[K_a]:
self.rect.x-=3
if keys[K_d]:
self.rect.x+=3
if keys[K_w] and self.jumped==False:
self.velY=-15
self.jumped=True
if not keys[K_w]:
self.jumped=False
if self.rect.left<0:
self.rect.left=0
if self.rect.right>1250:
self.rect.right=1250
self.velY+=1
if self.velY>10:
self.velY=10
yMove+=self.velY
self.rect.y+=yMove
if self.rect.y>550:
self.rect.y=550
if self.rect.y<0:
self.rect.y=0
def update(self):
self.move()
class Door(sprite.Sprite):
def __init__(self):
sprite.Sprite.__init__(self)
self.image=Surface((50,100))
self.image.fill((255,255,0))
self.rect=self.image.get_rect()
self.rect.x=1200
self.rect.y=500
def update(self):
global run
global doorkey
if sprite.collide_rect(player,door) and doorkey==True:
print("你赢了!")
run=False
class DoorKey(sprite.Sprite):
def __init__(self):
sprite.Sprite.__init__(self)
self.image=Surface((25,25))
self.image.fill((255,100,0))
self.rect=self.image.get_rect()
self.rect.x=600
self.rect.y=500
def update(self):
global allSprites
global doorkey
if sprite.collide_rect(player,doorKey):
doorkey=True
allSprites.remove(doorKey)
class LavaPool1(sprite.Sprite):
def __init__(self):
sprite.Sprite.__init__(self)
self.image=Surface((950,50))
self.image.fill((200,50,0))
self.rect=self.image.get_rect()
self.rect.x=150
self.rect.y=550
def update(self):
global allSprites
global run
if sprite.collide_rect(player,lavaPool1):
print("你输了!")
allSprites.remove(player)
run=False
class LavaPool2(sprite.Sprite):
def __init__(self):
sprite.Sprite.__init__(self)
self.image=Surface((950,50))
self.image.fill((200,50,0))
self.rect=self.image.get_rect()
self.rect.x=150
self.rect.y=0
def update(self):
global allSprites
global run
if sprite.collide_rect(player,lavaPool2):
print("你输了!")
allSprites.remove(player)
run=False
class Laser(sprite.Sprite):
def __init__(self):
sprite.Sprite.__init__(self)
self.image=Surface((5,600))
self.image.fill((255,0,0))
self.rect=self.image.get_rect()
self.rect.x=0
self.rect.y=0
def move(self):
global allSprites
self.rect.x+=1
if self.rect.left>1250:
self.rect.left=1250
allSprites.remove(laser)
def coll(self):
global run
global allSprites
if sprite.collide_rect(laser,player):
print("你输了!")
allSprites.remove(player)
run=False
if sprite.collide_rect(laser,doorKey):
allSprites.remove(doorKey)
def update(self):
self.move()
self.coll()
init()
screen=display.set_mode((1250,600))
display.set_caption("the second game 第13关---第8关 pro max")
screen.fill((0,0,0))
display.update()
allSprites=sprite.Group()
player=Player()
door=Door()
doorKey=DoorKey()
lavaPool1=LavaPool1()
lavaPool2=LavaPool2()
laser=Laser()
allSprites.add(door)
allSprites.add(doorKey)
allSprites.add(lavaPool1)
allSprites.add(lavaPool2)
allSprites.add(laser)
allSprites.add(player)
fc=pygame.time.Clock()
run=True
doorkey=False
while run:
screen.fill((0,0,0))
for event in pygame.event.get():
if event.type==QUIT:
run=False
if event.type==KEYDOWN:
if event.key==K_ESCAPE:
run=False
allSprites.update()
allSprites.draw(screen)
fc.tick(25)
display.update()
quit()
exit()