赞
踩
13-6 :屏幕底端有一个可以左右移动的玩家,屏幕顶端有一个球,水平位置随机,使这个球按固定速度下落,如果角色抓到球或者球碰到屏幕底端,就让球消失,并在顶端重新创建一个球,跟踪玩家没接到球的次数,达到三次结束游戏。
我这里只是将球换为礼物盒的形式,实现的功能未发生变化。
用到的模块如下:
卡通人物图片:
礼物盒图片:
两张图片均放在image文件夹中即可。
首先是一些基本设置,屏幕的长宽、背景颜色、礼物盒下落速度、人物左右移动速度等。
settings.py
class Settings():
def __init__(self):
self.screen_width = 1900
self.screen_height = 1000
self.bg_color = (255,255,255) # 设置背景色,浅灰色
self.drop_speed = 1
self.moving_speed = 2
self.moving_right = False
self.moving_left = False
self.state = 3
人物的一些基本设置,如何获得人物、初始化任务位置、左右移动更新位置、显示人物。
person.py
import pygame class Person(): def __init__(self,scratch_settings,screen): self.scratch_settings = scratch_settings self.screen = screen self.screen_rect = screen.get_rect() self.image = pygame.image.load('C:/Users/Xin/PycharmProjects/alien_invasion/image/person_scratch.bmp') self.rect = self.image.get_rect() self.rect.centerx = self.screen_rect.centerx self.rect.bottom = self.screen_rect.bottom self.x = float(self.rect.centerx) def update(self,scratch_settings): if scratch_settings.moving_right: self.x += scratch_settings.moving_speed if scratch_settings.moving_left: self.x -= scratch_settings.moving_speed self.rect.centerx = self.x def biltme(self): self.screen.blit(self.image, self.rect)
礼物盒的一些基本设置,如何获得礼物盒图片、初始化礼物盒位置、位置更新、显示礼物盒。
gifts.py
import pygame from random import randint class Gift(): def __init__(self, scratch_settings, screen, ): self.scratch_settings = scratch_settings self.screen = screen self.screen_rect = screen.get_rect() self.image = pygame.image.load('C:/Users/Xin/PycharmProjects/alien_invasion/image/gift.bmp') self.rect = self.image.get_rect() self.rect.centerx = randint(int(self.rect.width / 2), scratch_settings.screen_width - int (self.rect.width / 2)) self.rect.y = 0 self.y = float(self.rect.y) def update(self,scratch_settings): self.y += scratch_settings.drop_speed self.rect.y = self.y def biltme(self): self.screen.blit(self.image, self.rect)
用一个State类记录游戏中未接到礼物的次数.
state.py
class State():
def __init__(self,scratch_settings):
self.scratch_settings = scratch_settings
#每次重新运行都需要重置
self.reset_game()
#表示游戏是否可以继续执行
self.game_state = True
def reset_game(self):
self.opportunity = self.scratch_settings.state
用到的主要函数,响应键盘和鼠标的操作,人物是左移还是右移,更新人物的位置,检查礼物盒的位置,是否触碰到卡通人物或者触碰到屏幕底端,游戏是否可以继续执行,显示人物和礼物盒的位置。
game_function.py
import pygame import sys from random import randint def check_events(scratch_settings): #响应鼠标和键盘的操作 for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN: check_keydown_events(event,scratch_settings) elif event.type == pygame.KEYUP: check_keyup_events(event,scratch_settings) def check_keydown_events(event,scratch_settings): #人物是左移还是右移还是退出游戏 if event.key == pygame.K_RIGHT: scratch_settings.moving_right = True elif event.key == pygame.K_LEFT: scratch_settings.moving_left = True elif event.key == pygame.K_q: sys.exit() def check_keyup_events(event,scratch_settings): if event.key == pygame.K_RIGHT: scratch_settings.moving_right = False elif event.key == pygame.K_LEFT: scratch_settings.moving_left = False def new_position_of_gift(gift,scratch_settings): #礼物盒被捕捉或者触碰到底端之后,出现在屏幕顶端,位置不定 gift.rect.centerx = randint(int(gift.rect.width / 2), scratch_settings.screen_width - int(gift.rect.width / 2)) gift.y = 0 def check_gift_scratch_or_edge(gift,person,scratch_settings,gift_state): #判断是否被接到 if gift.rect.right >= person.rect.left and gift.rect.left <= person.rect.right and gift.rect.bottom == person.rect.top: new_position_of_gift(gift, scratch_settings) #判断是否碰触到屏幕底端 elif gift.rect.bottom == gift.screen_rect.bottom: #游戏是否可以继续,未接到的次数是否小于3 if gift_state.opportunity > 1: new_position_of_gift(gift, scratch_settings) gift_state.opportunity -= 1 else: gift_state.game_state = False def update_gift(gift,person,scratch_settings,gift_state): #更新礼物盒的位置 check_gift_scratch_or_edge(gift,person,scratch_settings,gift_state) gift.update(scratch_settings) def update_screen(scratch_settings,screen,gift,person): screen.fill(scratch_settings.bg_color) # 将背景色填满整个屏幕 gift.biltme() person.biltme() # 让最近绘制的屏幕可见 pygame.display.flip()
主文件部分。
start_scratch.py
import pygame from scratch.settings import Settings from scratch.gifts import Gift import scratch.game_function as gf from scratch.person import Person from scratch.state import State def run(): scratch_settings = Settings() screen = pygame.display.set_mode((scratch_settings.screen_width,scratch_settings.screen_height)) gift = Gift(scratch_settings,screen) scratch_person = Person(scratch_settings,screen) gift_state = State(scratch_settings) while True: #不管游戏是否运行,都需要响应鼠标和键盘的操作以及显示屏幕 gf.check_events(scratch_settings) #当游戏运行时 if gift_state.game_state: gf.update_gift(gift,scratch_person,scratch_settings,gift_state) scratch_person.update(scratch_settings) gf.update_screen(scratch_settings,screen,gift,scratch_person) run()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。