學習總結-《父與子的編程之旅》chapter 18


這一章中將學習一種不同的輸入,叫做事件(event)。

示例與動手試一試:

import  sys,pygame
pygame.init()
screen = pygame.display.set_mode([1024,1024])
background = pygame.Surface(screen.get_size())
background.fill([255,255,255])
clock = pygame.time.Clock()

delay = 500
interval = 50
pygame.key.set_repeat(delay,interval)
class MytagClass(pygame.sprite.Sprite):
    def __init__(self,image_file,location,speed):
        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):
        if self.rect.left<= screen.get_rect().left or \
            self.rect.right >= screen.get_rect().right:
            self.speed[0] = -self.speed[0]
        newpos = self.rect.move(self.speed)
        self.rect = newpos

mytag = MytagClass("ball.jpg",[10,0],[20,20])
pygame.time.set_timer(pygame.USEREVENT,2) #創建定時器
direction = 1
##held_down = False
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
 # elif event.type ==pygame.KEYDOWN:
 # if event.key == pygame.K_UP:
 # mytag.rect.top = mytag.rect.top - 100
 # elif event.key == pygame.K_DOWN:
 # mytag.rect.top = mytag.rect.top +100
 ## held_down = True
 ## elif event.type == pygame.MOUSEBUTTONUP:
 ## held_down = False
 ## elif event.type == pygame.MOUSEMOTION:
 ## if held_down:
 ## mytag.rect.center = event.pos #鼠標位置
        elif event.type == pygame.USEREVENT:
            mytag.rect.centery = mytag.rect.centery+(30 *direction)
            if mytag.rect.top <= 0 or \
                mytag.rect.bottom >= screen.get_rect().bottom:
                direction = -direction

    clock.tick(5)
    screen.blit(background,(0,0))
    mytag.move()
    screen.blit(mytag.image,mytag.rect)
    pygame.display.flip()
pygame.quit()

PyPong 游戲

import  sys,pygame,random
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.right = location
        self.speed = speed

    def move(self):
        global  score,score_surf,score_font
        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]
            score = score+1
            score_surf = score_font.render(str(score),1,(0,0,0))

class MypaddleClass(pygame.sprite.Sprite):
    def __init__(self,location=[0,0]):
        pygame.sprite.Sprite.__init__(self)
        image_surface = pygame.surface.Surface([100,20])
        image_surface.fill([0,0,0])
        self.image = image_surface.convert()
        self.rect = self.image.get_rect()
        self.rect.left,self.rect.top = location

pygame.init()
pygame.mixer.init()

screen= pygame.display.set_mode([1024,1024])
clock = pygame.time.Clock()
a= random.randint(2,25)
b= random.randint(2,25)
ball_speed = [a,b]
myball= MyballClass('ball.jpg',ball_speed,[300,400])
ballGroup = pygame.sprite.Group(myball)
paddle = MypaddleClass([100,700])
lives = 3
score = 0
score_font = pygame.font.Font(None,50)
score_surf = score_font.render(str(score),1,(0,0,0))
score_pos = [10,10]
done = False
running = True
while running:
    clock.tick(30)
    screen.fill([255,255,255])
    for event in pygame.event.get():
        if event.type == QUIT:
            running = False
        elif event.type == pygame.MOUSEMOTION:
            paddle.rect.centerx = event.pos[0]

    if pygame.sprite.spritecollide(paddle,ballGroup,False):
        myball.speed[1] = -myball.speed[1]

    myball.move()
    if not done :
        screen.blit(myball.image,myball.rect)
        screen.blit(paddle.image,paddle.rect)
        screen.blit(score_surf,score_pos)
        for i in range (lives):
            width = screen.get_width()
            screen.blit(myball.image,[width-300*i,20])
        pygame.display.flip()
    if myball.rect.top >= screen.get_rect().bottom:
        lives = lives -1
        if lives ==0:
            final_text1 = 'Game over'
            final_text2 = 'Your score is: '+str(score)
            ft1_font = pygame.font.Font(None,70)
            ft1_surf = ft1_font.render(final_text1,1,(0,0,0))
            ft2_font = pygame.font.Font(None,40)
            ft2_surf = ft2_font.render(final_text2,1,(0,0,0))
            screen.blit(ft1_surf,[screen.get_width()/2-\
                                  ft1_surf.get_width()/2,100])
            screen.blit(ft2_surf,[screen.get_width()/2-\
                                  ft2_surf.get_width()/2,300])
            pygame.display.flip()
            done = True
        else:
            pygame.time.delay(2000)
            myball.rect.topleft = [50,50]

pygame.quit()

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com