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

2021-08-19 15:42:55 字數 4485 閱讀 8515

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.2.關於接住的球數計數問題:

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

Python 程式設計 從入門到實踐

1.官網安裝 3.環境配置 務必選中核取方塊add python to path 4.檢視 啟動python版本的命令 python 執行 print hello python world 5.終端執行x.py檔案 python x.py 7.檢視當前目錄中的所有檔案的命令 dir windows系...

Python程式設計從入門到實踐 基礎入門

python程式設計從入門到實踐 基礎入門 1 python中的變數 2 python首字母大寫使用title 方法,全部大寫upper 方法,全部小寫lower 方法 3 python中字串拼接使用 號 4 python中刪除字串的空格 刪除末尾空格的rstrip 刪除開頭空格的lstrip 刪除...

Python程式設計 從入門到實踐 1

內容總結自 python程式設計 從入門到實踐 安裝python3 安裝文字編輯器sublime text並配置python3環境 安裝sublime text tools new build system 將 untitled.sublime build 文件中的所有內容刪除,輸入以下內容 注意,...