與小卡特一起學python 第18章 一種新的輸入-事件
#18章 事件
#18-1 反彈程式,假如動畫精靈和clock.tick()
import pygame,sys
pygame.init()
screen = pygame.display.set_mode([640,480])
backgroup = pygame.Surface(screen.get_size())
backgroup .fill ([255,255,255])
clock = pygame.time.Clock()
class Ball(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):
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
my_ball = Ball('beach_ball.png',[10,0],[20,20])
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
clock.tick(30)
screen.blit(backgroup,(0,0))
my_ball.move()
screen.blit(my_ball.image,my_ball.rect)
pygame.display.flip()
pygame.quit()
#18章 事件
#18-2 響應向上向下箭頭的反彈球
import pygame,sys
pygame.init()
screen = pygame.display.set_mode([640,480])
backgroup = pygame.Surface(screen.get_size())
backgroup .fill ([255,255,255])
clock = pygame.time.Clock()
class Ball(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):
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
my_ball = Ball('beach_ball.png',[10,0],[20,20])
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:
my_ball.rect.top = my_ball.rect.top - 10
elif event.key == pygame.K_DOWN:
my_ball.rect.top = my_ball.rect.top +10
clock.tick(30)
screen.blit(backgroup,(0,0))
my_ball.move()
screen.blit(my_ball.image,my_ball.rect)
pygame.display.flip()
pygame.quit()
#18章 事件
#18-3 使用定時器事件讓球上下箭頭移動
import pygame,sys
pygame.init()
screen = pygame.display.set_mode([640,480])
backgroup = pygame.Surface(screen.get_size())
backgroup .fill ([255,255,255])
clock = pygame.time.Clock()
class Ball(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):
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
my_ball = Ball('beach_ball.png',[10,0],[20,20])
pygame.time.set_timer(pygame.USEREVENT,1000)
direction = 1
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.USEREVENT:
my_ball.rect.centery = my_ball.rect.centery + (30*direction)
if my_ball.rect.top <= 0 or my_ball.rect.bottom >= screen.get_rect().bottom:
direction = -direction
## if event.key == pygame.K_UP:
## my_ball.rect.top = my_ball.rect.top - 10
## elif event.key == pygame.K_DOWN:
## my_ball.rect.top = my_ball.rect.top +10
clock.tick(30)
screen.blit(backgroup,(0,0))
my_ball.move()
screen.blit(my_ball.image,my_ball.rect)
pygame.display.flip()
pygame.quit()
#18章 事件
#18-4pypong的第一個版本
import pygame,sys
from pygame.locals import *
#Ball 類定義
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]
#球拍類定義
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時鐘球和球拍
pygame.init()
screen = pygame.display.set_mode([640,480])
clock = pygame.time.Clock()
ball_speed = [10,5]
myBall = MyballClass('wackyball.bmp',ball_speed,[50,50])
ballGroup = pygame.sprite.Group(myBall)
paddle = MyPaddleClass([270,400])
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()#移動球
screen.blit(myBall.image,myBall.rect)
screen.blit(paddle.image,paddle.rect)
pygame.display.flip()
pygame.quit()
#18-5 最終的PyPong程式碼
import pygame,sys
#Ball 類定義
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):#移動球,在頂邊和左右兩邊反彈
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時鐘球和球拍
pygame.init()
screen = pygame.display.set_mode([640,480])
clock = pygame.time.Clock()
ball_speed = [10,5]
myBall = MyballClass('wackyball.bmp',ball_speed,[50,50])
ballGroup = pygame.sprite.Group(myBall)
paddle = MyPaddleClass([270,400])
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 ==pygame.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 - 40 * 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 final 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,50)
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,200])
pygame.display.flip()
done = True
else:
pygame.time.delay(20000)
myBall.rect.topleft = [50,50]
pygame.quit()
#18-1 反彈程式,假如動畫精靈和clock.tick()
import pygame,sys
pygame.init()
screen = pygame.display.set_mode([640,480])
backgroup = pygame.Surface(screen.get_size())
backgroup .fill ([255,255,255])
clock = pygame.time.Clock()
class Ball(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):
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
my_ball = Ball('beach_ball.png',[10,0],[20,20])
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
clock.tick(30)
screen.blit(backgroup,(0,0))
my_ball.move()
screen.blit(my_ball.image,my_ball.rect)
pygame.display.flip()
pygame.quit()
#18章 事件
#18-2 響應向上向下箭頭的反彈球
import pygame,sys
pygame.init()
screen = pygame.display.set_mode([640,480])
backgroup = pygame.Surface(screen.get_size())
backgroup .fill ([255,255,255])
clock = pygame.time.Clock()
class Ball(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):
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
my_ball = Ball('beach_ball.png',[10,0],[20,20])
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:
my_ball.rect.top = my_ball.rect.top - 10
elif event.key == pygame.K_DOWN:
my_ball.rect.top = my_ball.rect.top +10
clock.tick(30)
screen.blit(backgroup,(0,0))
my_ball.move()
screen.blit(my_ball.image,my_ball.rect)
pygame.display.flip()
pygame.quit()
#18章 事件
#18-3 使用定時器事件讓球上下箭頭移動
import pygame,sys
pygame.init()
screen = pygame.display.set_mode([640,480])
backgroup = pygame.Surface(screen.get_size())
backgroup .fill ([255,255,255])
clock = pygame.time.Clock()
class Ball(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):
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
my_ball = Ball('beach_ball.png',[10,0],[20,20])
pygame.time.set_timer(pygame.USEREVENT,1000)
direction = 1
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.USEREVENT:
my_ball.rect.centery = my_ball.rect.centery + (30*direction)
if my_ball.rect.top <= 0 or my_ball.rect.bottom >= screen.get_rect().bottom:
direction = -direction
## if event.key == pygame.K_UP:
## my_ball.rect.top = my_ball.rect.top - 10
## elif event.key == pygame.K_DOWN:
## my_ball.rect.top = my_ball.rect.top +10
clock.tick(30)
screen.blit(backgroup,(0,0))
my_ball.move()
screen.blit(my_ball.image,my_ball.rect)
pygame.display.flip()
pygame.quit()
#18章 事件
#18-4pypong的第一個版本
import pygame,sys
from pygame.locals import *
#Ball 類定義
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]
#球拍類定義
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時鐘球和球拍
pygame.init()
screen = pygame.display.set_mode([640,480])
clock = pygame.time.Clock()
ball_speed = [10,5]
myBall = MyballClass('wackyball.bmp',ball_speed,[50,50])
ballGroup = pygame.sprite.Group(myBall)
paddle = MyPaddleClass([270,400])
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()#移動球
screen.blit(myBall.image,myBall.rect)
screen.blit(paddle.image,paddle.rect)
pygame.display.flip()
pygame.quit()
#18-5 最終的PyPong程式碼
import pygame,sys
#Ball 類定義
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):#移動球,在頂邊和左右兩邊反彈
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時鐘球和球拍
pygame.init()
screen = pygame.display.set_mode([640,480])
clock = pygame.time.Clock()
ball_speed = [10,5]
myBall = MyballClass('wackyball.bmp',ball_speed,[50,50])
ballGroup = pygame.sprite.Group(myBall)
paddle = MyPaddleClass([270,400])
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 ==pygame.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 - 40 * 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 final 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,50)
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,200])
pygame.display.flip()
done = True
else:
pygame.time.delay(20000)
myBall.rect.topleft = [50,50]
pygame.quit()
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/220205/viewspace-2079304/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 與小卡特一起學python 第22章 檔案輸入與輸出Python
- 與小卡特一起學python 第14章 物件Python物件
- 與小卡特一起學python 第19章 聲音Python
- 與小卡特一起學python 第20章 使用pyqtPythonQT
- 與小卡特一起學python 第5章 輸入 5-1,2,3,4 input()輸入函式Python函式
- 與小卡特一起學python 第5章 輸入 測試題和動手試一試Python
- 與小卡特一起學python 第16章 圖形 Pygame學習PythonGAM
- 與小卡特一起學python 第4章 資料的型別Python型別
- 與小卡特一起學python 第13章 函式-積木Python函式
- 與小卡特一起學python 第3章 基本數學運算Python
- 與小卡特一起學python 第21章 列印格式化與字串Python字串
- 與小卡特一起學python 第14章 物件 動手試一試Python物件
- 與小卡特一起學python 第11章 巢狀與可變迴圈Python巢狀
- 與小卡特一起學Python 第15章 模組 及動手試一試Python
- 與小卡特一起學python 第8章 動手試一試原始碼Python原始碼
- 與小卡特一起學python 第9章 全都為了你-註釋Python
- 與小卡特一起學python 第17章動畫精靈和碰撞檢測Python動畫
- 與小卡特一起學python 第13章 函式-積木 動手試一試Python函式
- 與小卡特一起學python 第10章 遊戲時間到了 程式碼清單Python遊戲
- 與小卡特一起學python 第1章 出發吧 課後練習題Python
- 與小卡特一起學python 第12章 收集起來,列表與字典 動手試一試Python
- 與小卡特一起學python 第1章 出發吧 1-2猜數遊戲Python遊戲
- 與小卡特一起學python 第8章 轉圈圈 FOR迴圈和條件迴圈Python
- 與小卡特一起學python 第11章 巢狀與可變迴圈 動手試一試Python巢狀
- 與小卡特一起學python 第7章 判斷再判斷 7-1-2-3-6-7Python
- 與小卡特一起學python 第2章 記住記憶體和變數 2-1練習Python記憶體變數
- 與小卡特一起學python 第10章 遊戲時間到了 pygame安裝及素材圖片準備Python遊戲GAM
- 與小卡特一起學python 第1章 出發吧 1-1練習我們第一個真正的程式Python
- 與小卡特一起學python 第2章 記住記憶體和變數 課後 動手試一試Python記憶體變數
- 與小卡特一起學python 第6章 gui-圖形使用者介面 測試和動手試一試PythonGUI
- 與小卡特一起學python 第6章 gui-圖形使用者介面 6-1-2-3-4-5 gui使用PythonGUI
- 第10章 對檔案的輸入輸出
- 一起學習React18 新特性React
- input輸入框的各種事件事件
- 第 18 章 CSS 表格與列表CSS
- 跟我一起學Python從入門到精通《第三章》Python
- 第18章_JDK8-17新特性JDK
- 第 8 章 輸入框和導航元件元件