python pygame做成的第一个真的意义上的游戏!


#-*- coding:utf-8 -*-

import  pygame , sys

from pygame.locals import *



class Myballclass(pygame.sprite.Sprite):

    def __init__(self,image_file,speed,location):

        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.image.load(image_file)

        self.rect = self.image.get_rect()

        self.rect.left, self.rect.top = location

        self.speed = speed



    def move(self):

        self.rect = self.rect.move(self.speed)

        if self.rect.left < 0 or self.rect.right > screen.get_width():

            self.speed[0] = -self.speed[0]

        if self.rect.top <= 0 :

            self.speed[1] = -self.speed[1]

        

    def over(self):

        self.rect=self.rect.move(self.speed)

        if self.rect.bottom>screen.get_height():

            font =pygame.font.SysFont("宋体",40)

            text_surface=font.render(u"GAME OVER",True,(0,0,255))

            screen.blit(text_surface,(320,240))



            return 0

      

        

class Mypaddleclass(pygame.sprite.Sprite):

    def __init__(self, location):

        pygame.sprite.Sprite.__init__(self)

        image_surface = pygame.surface.Surface([100, 20])

        #控制蓝色标签的大小

        image_surface.fill([213,213,123])

        #控制颜色

        self.image = image_surface.convert()



        self.rect = self.image.get_rect()

        self.rect.left, self.rect.top = location



pygame.init()

screen = pygame.display.set_mode([1280,960])

background=pygame.image.load("6.jpg").convert()

clock = pygame.time.Clock()

ball_speed = [4, -4]

score=0

#控制移方向的,参数为正时并且越大的移动越快t

#最后一个参数控制图像初始位置的

myball = Myballclass('5.png', ball_speed, [10,50])

ballgroup = pygame.sprite.Group(myball)

paddle = Mypaddleclass([270,400])

#控制蓝色标签的初始位置

time=20

while True:

    for event in pygame.event.get():

        if event.type == pygame.QUIT:

            sys.exit()

        elif event.type == pygame.MOUSEMOTION:

            paddle.rect.centerx = event.pos[0]

    if pygame.sprite.spritecollide(paddle, ballgroup, 0):

        myball.speed[1] = -myball.speed[1]

        time=time+1

        score=score+10 

    clock.tick(time)



    screen.fill([255,255,255])

    font =pygame.font.SysFont("宋体",40)

    text_surface=font.render(u'分数'+str(score),True,(0,0,255))

    screen.blit(text_surface,(32,24))

    

    #open()参数的用法a+连续写入不覆盖,r+写入最后一个,w+写入的内容全覆盖

    scorefile=open('a.txt','r+')

    for a in scorefile.readline(10):

        text_surface1=font.render(u'最高记录   '+str(a),True,(0,0,255))    

        screen.blit(text_surface1,(320,24))

        print "woqu"

        print a

        print "nima"

        print score

        if (a<score):

            print a

            print a*a

            print score

            scorefile.flush()

            scorefile.writelines(str(score))

    

    scorefile.close()

    myball.move()

    myball.over()

    screen.blit(myball.image, myball.rect)

    screen.blit(paddle.image, paddle.rect)

    pygame.display.flip()

 

python pygame做成的第一个真的意义上的游戏!

这个是一个接物游戏!接到之后速度不断变快,直到接不住为止

 

你可能感兴趣的:(python)