1. 程式人生 > >Python程式設計從入門到實踐練習13-5

Python程式設計從入門到實踐練習13-5

13-5 抓球:建立一個遊戲,在螢幕底端放置一個左右移動的角色。讓一個球出現在螢幕頂端,而且水平位置是隨機的,並讓這個球以固定的速度下落。如果角色與球發生碰撞(表示球抓住了),就讓球消失。每當角色抓住球或球因抵達螢幕底端而消失後,都建立一個新球。

注:因為從頭到尾只有人和球兩個例項,因此本程式沒有建立一個新球,而是直接改變球的座標。

主程式:game

import pygame

from settings import Settings

import func as f

from player import Player

from ball import Ball

from pygame.sprite import Group

def run_game():
	pygame.init()
	settings = Settings()
	screen = pygame.display.set_mode((settings.screen_width, settings.screen_height))
	player = Player(settings, screen)
	ball = Ball(settings, screen)
	count = 0
	while True:
		f.check_events(player)
		check = f.check_cross(screen, player, ball)
		if check:
			count += 1
			print("你已經接住" + str(count) + "個球啦!再接再厲吧!")
		elif check == False:
			print("沒接住,繼續加油吧。")
		f.update_screen(settings, screen, player, ball)
run_game()

Ball類:

import pygame

from random import randint

from pygame.sprite import Sprite

class Ball():
	def __init__(self, settings, screen):
		self.screen = screen
		self.settings = settings
		self.image = pygame.image.load('ball.bmp')
		self.rect = self.image.get_rect()
		self.screen_rect = screen.get_rect()
		
		self.rect.x = randint(self.rect.x, (self.screen_rect.width - self.rect.x))
		
		
	def blitme(self):
		self.screen.blit(self.image, self.rect)
		
	def update(self):
		self.rect.y += self.settings.ball_speed
		

Player類:

import pygame

class Player():
	def __init__(self, settings, screen):
		self.screen = screen
		self.settings = settings
		self.image = pygame.image.load('player.bmp')
		self.rect = self.image.get_rect()
		self.screen_rect = screen.get_rect()
		
		self.rect.centerx = self.screen_rect.centerx
		self.rect.bottom = self.screen_rect.bottom
		
		self.moving_right = False
		self.moving_left = False
		
	def blitme(self):
		self.screen.blit(self.image, self.rect)
	
	def update(self):
		if self.moving_right and self.rect.right < self.screen_rect.right:
			self.rect.x += self.settings.player_speed
		if self.moving_left and self.rect.left > 0:
			self.rect.x -= self.settings.player_speed

Settings類:

import pygame

class Settings():
	def __init__(self):
		self.screen_width = 1200
		self.screen_height = 700
		self.bg_color = (255, 255, 255)
		self.player_speed = 3
		self.ball_speed = 1

函式塊func:

import pygame

import sys

from random import randint

def update_screen(settings, screen, player, ball):
	screen.fill(settings.bg_color)
	create_ball(ball)
	create_player(player)
	pygame.display.flip()
	
def create_player(player):
	player.blitme()
	player.update()

def create_ball(ball):
	ball.blitme()
	ball.update()
	
def check_cross(screen, player, ball):
	if pygame.sprite.collide_mask(player, ball):
		ball.rect.x = randint(0, (screen.get_rect().width - ball.rect.x))
		ball.rect.y = 50
		create_ball(ball)
		
		return True
	elif ball.rect.top >= screen.get_rect().bottom:
		ball.rect.x = randint(0, (screen.get_rect().width - ball.rect.x))
		ball.rect.y = 50
		return False
def check_events(player):
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			sys.exit()
		if event.type == pygame.KEYDOWN:
			check_key_down(event, player)
		elif event.type == pygame.KEYUP:
			check_key_up(event, player)
			
def check_key_down(event, player):
	if event.key == pygame.K_RIGHT:
		player.moving_right = True
	if event.key == pygame.K_LEFT:
		player.moving_left = True
	
def check_key_up(event, player):
	
	if event.key == pygame.K_RIGHT:
		player.moving_right = False
	if event.key == pygame.K_LEFT:
		player.moving_left = False
	

實際效果展示:




總結與疑問:

1.

百度知識點:pygame.sprite.collide_mask()函式

2.關於接住的球數計數問題:

在重新整理球時計數,在主程式執行時,接球數始終為1,無奈之下將計數程式碼寫到了主程式中。