與小卡特一起學python 第10章 遊戲時間到了 程式碼清單
# Skier program 遊戲時間到了
import pygame,sys,random
skier_images = ["skier_down.png","skier_right1.png",
"skier_right2.png","skier_left2.png",
"skier_left1.png"]
#建立滑雪者
class SkierClass (pygame.sprite.Sprite):
def __init__(self): #注意,一定注意兩邊是兩個_ 成為__(更長點)
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("skier_down.png")
self.rect = self.image.get_rect()
self.rect.center = [320,100]#注意是中括號,不是小括號
self.angle = 0
#滑雪者轉向
def turn(self,direction):
self.angle = self.angle + direction
if self.angle < -2: self.angle = -2
if self.angle > 2: self.angle = 2
center = self.rect.center
self.image = pygame.image.load (skier_images[self.angle])
self.rect = self.image.get_rect()
self.rect.center = center
speed = [self.angle, 6 - abs(self.angle)*2]
return speed
#滑雪者左右移動
def move(self,speed):
self.rect.centerx = self.rect.centerx + speed[0]
if self.rect.centerx < 20: self.rect.centerx = 20
if self.rect.centerx > 620: self.rect.centerx = 620
#建立樹和小旗
class ObstacleClass(pygame.sprite.Sprite):
def __init__(self,image_file,location,type):
pygame.sprite.Sprite.__init__(self)
self.image_file = image_file
self.image = pygame.image.load(image_file)
self.rect = self.image.get_rect()
self.rect.center = location
self.type = type
self.passed = False
#讓場景上滾
def update(self):
global speed
self.rect.centery -= speed[1]
#刪除螢幕上方滾下的障礙物
if self.rect.centery < -32:
self.kill()
#建立一個視窗,包含隨隨機的數和小旗
def create_map():
global obstacles
locations = []
for i in range(10):
row = random.randint(0,9)
col = random.randint(0,9)
location = [col * 64 +32, row * 64 + 32 +640]#書中程式碼是 +20 ,而下載的程式碼是+32
if not (location in locations):
locations.append(location)
type = random.choice(["tree","flag"])
if type == "tree": img = "skier_tree.png"
elif type == "flag": img = "skier_flag.png"#注意:
obstacle = ObstacleClass(img,location,type)
obstacles.add(obstacle)
#重新繪製螢幕
def animate():
screen.fill([255,255,255])
obstacles.draw(screen)
screen.blit(skier.image,skier.rect)
screen.blit(score_text,[10,10])
pygame.display.flip()
#做好準備
pygame.init()
screen = pygame.display.set_mode([640,640])
clock = pygame.time.Clock()
speed = [0,6]
obstacles = pygame.sprite.Group()
skier = SkierClass()
map_position = 0
points = 0
create_map()
font = pygame.font.Font(None,50)
#開始迴圈
running = True
#檢查按鈕或者視窗是否關閉
while running:
clock.tick(30) #每秒更新30次圖形
for event in pygame.event.get():
if event.type == pygame.QUIT: running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
speed = skier.turn(-1)
elif event.key == pygame.K_RIGHT:
speed = skier.turn(1)
skier.move(speed) #移動滑雪者
map_position += speed[1] #滾動場景
#建立一個新視窗,包含場景
if map_position >=640:
create_map()
map_position = 0
hit = pygame.sprite.spritecollide(skier,obstacles,False)
if hit:
#檢查是否碰到數或者得到旗
if hit[0].type == "tree" and not hit[0].passed:
points = points - 100
skier.imagge = pygame.image.load("skier_crash.png")
animate()
pygame.time.delay(1000)
skier.image = pygame.image.load("skier_down.png")
skier.angle = 0
speed = [0,6]
hit[0].passed = True
elif hit[0].type == "flag" and not hit[0].passed:
points += 10
hit[0].kill()
obstacles.update()
score_text = font.render("Score:" +str(points),1,(0,0,0)) #顯示得分
animate()
pygame.quit()
import pygame,sys,random
skier_images = ["skier_down.png","skier_right1.png",
"skier_right2.png","skier_left2.png",
"skier_left1.png"]
#建立滑雪者
class SkierClass (pygame.sprite.Sprite):
def __init__(self): #注意,一定注意兩邊是兩個_ 成為__(更長點)
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("skier_down.png")
self.rect = self.image.get_rect()
self.rect.center = [320,100]#注意是中括號,不是小括號
self.angle = 0
#滑雪者轉向
def turn(self,direction):
self.angle = self.angle + direction
if self.angle < -2: self.angle = -2
if self.angle > 2: self.angle = 2
center = self.rect.center
self.image = pygame.image.load (skier_images[self.angle])
self.rect = self.image.get_rect()
self.rect.center = center
speed = [self.angle, 6 - abs(self.angle)*2]
return speed
#滑雪者左右移動
def move(self,speed):
self.rect.centerx = self.rect.centerx + speed[0]
if self.rect.centerx < 20: self.rect.centerx = 20
if self.rect.centerx > 620: self.rect.centerx = 620
#建立樹和小旗
class ObstacleClass(pygame.sprite.Sprite):
def __init__(self,image_file,location,type):
pygame.sprite.Sprite.__init__(self)
self.image_file = image_file
self.image = pygame.image.load(image_file)
self.rect = self.image.get_rect()
self.rect.center = location
self.type = type
self.passed = False
#讓場景上滾
def update(self):
global speed
self.rect.centery -= speed[1]
#刪除螢幕上方滾下的障礙物
if self.rect.centery < -32:
self.kill()
#建立一個視窗,包含隨隨機的數和小旗
def create_map():
global obstacles
locations = []
for i in range(10):
row = random.randint(0,9)
col = random.randint(0,9)
location = [col * 64 +32, row * 64 + 32 +640]#書中程式碼是 +20 ,而下載的程式碼是+32
if not (location in locations):
locations.append(location)
type = random.choice(["tree","flag"])
if type == "tree": img = "skier_tree.png"
elif type == "flag": img = "skier_flag.png"#注意:
obstacle = ObstacleClass(img,location,type)
obstacles.add(obstacle)
#重新繪製螢幕
def animate():
screen.fill([255,255,255])
obstacles.draw(screen)
screen.blit(skier.image,skier.rect)
screen.blit(score_text,[10,10])
pygame.display.flip()
#做好準備
pygame.init()
screen = pygame.display.set_mode([640,640])
clock = pygame.time.Clock()
speed = [0,6]
obstacles = pygame.sprite.Group()
skier = SkierClass()
map_position = 0
points = 0
create_map()
font = pygame.font.Font(None,50)
#開始迴圈
running = True
#檢查按鈕或者視窗是否關閉
while running:
clock.tick(30) #每秒更新30次圖形
for event in pygame.event.get():
if event.type == pygame.QUIT: running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
speed = skier.turn(-1)
elif event.key == pygame.K_RIGHT:
speed = skier.turn(1)
skier.move(speed) #移動滑雪者
map_position += speed[1] #滾動場景
#建立一個新視窗,包含場景
if map_position >=640:
create_map()
map_position = 0
hit = pygame.sprite.spritecollide(skier,obstacles,False)
if hit:
#檢查是否碰到數或者得到旗
if hit[0].type == "tree" and not hit[0].passed:
points = points - 100
skier.imagge = pygame.image.load("skier_crash.png")
animate()
pygame.time.delay(1000)
skier.image = pygame.image.load("skier_down.png")
skier.angle = 0
speed = [0,6]
hit[0].passed = True
elif hit[0].type == "flag" and not hit[0].passed:
points += 10
hit[0].kill()
obstacles.update()
score_text = font.render("Score:" +str(points),1,(0,0,0)) #顯示得分
animate()
pygame.quit()
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/220205/viewspace-2074434/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 與小卡特一起學python 第10章 遊戲時間到了 pygame安裝及素材圖片準備Python遊戲GAM
- 與小卡特一起學python 第14章 物件Python物件
- 與小卡特一起學python 第19章 聲音Python
- 與小卡特一起學python 第20章 使用pyqtPythonQT
- 與小卡特一起學python 第16章 圖形 Pygame學習PythonGAM
- 與小卡特一起學python 第13章 函式-積木Python函式
- 與小卡特一起學python 第3章 基本數學運算Python
- 與小卡特一起學python 第8章 動手試一試原始碼Python原始碼
- 與小卡特一起學python 第21章 列印格式化與字串Python字串
- 與小卡特一起學python 第4章 資料的型別Python型別
- 與小卡特一起學python 第1章 出發吧 1-2猜數遊戲Python遊戲
- 與小卡特一起學python 第11章 巢狀與可變迴圈Python巢狀
- 與小卡特一起學python 第22章 檔案輸入與輸出Python
- 與小卡特一起學python 第14章 物件 動手試一試Python物件
- 與小卡特一起學python 第9章 全都為了你-註釋Python
- 與小卡特一起學Python 第15章 模組 及動手試一試Python
- 與小卡特一起學python 第17章動畫精靈和碰撞檢測Python動畫
- 與小卡特一起學python 第18章 一種新的輸入-事件Python事件
- 與小卡特一起學python 第1章 出發吧 課後練習題Python
- 與小卡特一起學python 第13章 函式-積木 動手試一試Python函式
- 與小卡特一起學python 第8章 轉圈圈 FOR迴圈和條件迴圈Python
- 與小卡特一起學python 第12章 收集起來,列表與字典 動手試一試Python
- 與小卡特一起學python 第11章 巢狀與可變迴圈 動手試一試Python巢狀
- 與小卡特一起學python 第5章 輸入 測試題和動手試一試Python
- 與小卡特一起學python 第7章 判斷再判斷 7-1-2-3-6-7Python
- 與小卡特一起學python 第1章 出發吧 1-1練習我們第一個真正的程式Python
- 與小卡特一起學python 第2章 記住記憶體和變數 2-1練習Python記憶體變數
- 與小卡特一起學python 第5章 輸入 5-1,2,3,4 input()輸入函式Python函式
- 與小卡特一起學python 第2章 記住記憶體和變數 課後 動手試一試Python記憶體變數
- 與小卡特一起學python 第6章 gui-圖形使用者介面 測試和動手試一試PythonGUI
- 與小卡特一起學python 第6章 gui-圖形使用者介面 6-1-2-3-4-5 gui使用PythonGUI
- 《HTML52D遊戲程式設計核心技術》——第3章,第3.10節小結HTML遊戲程式設計
- 微信小程式填坑清單微信小程式
- 第 10 章 表單元素[上]
- 第 10 章 表單元素[中]
- 第 10 章 表單元素[下]
- 我從1萬小時程式設計中學到了什麼程式設計
- 《小程式開發原理與實戰》資源連結清單