#-*- 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()
這個是一個接物遊戲!接到之後速度不斷變快,直到接不住為止