python pygame做成的第一個真的意義上的遊戲!

來源:互聯網
上載者:User
#-*- coding:utf-8 -*-import  pygame , sysfrom 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 = locationpygame.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=20while 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()

這個是一個接物遊戲!接到之後速度不斷變快,直到接不住為止

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.