python游戏开发keydown_pygame.KEYDOWN移动对象

作者,持牌照消费金融模型经理,发明国家算法专利,国内大型医药数据中心担任过数据库负责人。和中科院,清华大学,百度,腾讯,爱奇艺等平台保持长期项目合作。擅长python 机器学习,应用于游戏,医疗,金融领域。

曾经我也沉迷游戏,连续玩十几个小时竞技游戏可以不吃饭。游戏只是别人写好程序,与其沉迷于他人设计好程序,为何不自己设计游戏。于是便有了录制这部pygame菜鸟游戏编程教程动力。不要沉迷于游戏,我们人生还有许多事情要做,旅游,找到另一个伴侣,成立一个家庭,照顾父母。。。。

编程正在逐步改变世界,程序员不是搬砖的。但传统计算教育程面临枯燥乏味课程,让学生感到乏味。

兴趣是学习最好老师!此课程目的是激发大家对编程兴趣,给广大Python入门初学者带来无穷乐趣。

游戏涉及童年玩过经典小游戏,包括贪吃蛇,消消乐,俄罗斯方块,植物大战僵尸,扫雷等等。视频的参考资料可下载脚本。脚本已经编译好,可直接运行!

感谢妻子对家庭支持,让我周末和节假日有时间录制教学视频,我会继续加油!

Have Fun!

下图展示视频中几个经典童年游戏,植物大战僵尸

《植物大战僵尸》是由PopCap

Games开发的一款益智策略类单机游戏,于2009年5月5日发售。玩家通过武装多种植物切换不同的功能,快速有效地把僵尸阻挡在入侵的道路上。不同的敌人,不同的玩法构成五种不同的游戏模式,加之黑夜、浓雾以及泳池之类的障碍增加了游戏挑战性。

《植物大战僵尸》是一款极富策略性的小游戏。可怕的僵尸即将入侵,每种僵尸都有不同的特点,例如铁桶僵尸拥有极强的抗击打能力,矿工僵尸可以挖地道绕过种植在土壤表面的植物等。玩家防御僵尸的方式就是栽种植物。49种植物每种都有不同的功能,例如樱桃炸弹可以和周围一定范围内的所有僵尸同归于尽,而食人花可以吃掉最靠近自己的一只僵尸。玩家可以针对不同僵尸的弱点来合理地种植植物,这也是胜利的诀窍。游戏根据玩法不同分为五种游戏模式:冒险、生存、花瓶破碎者、小游戏、花园。加之黑夜、屋顶、浓雾以及泳池之类的障碍增加了其挑战性该游戏近乎永无止境。

import random import pygame from objects import Background, Player, Enemy, Bullet, Explosion, Fuel, \ Powerup, Button, Message, BlinkingText pygame.init() SCREEN = WIDTH, HEIGHT = 288, 512 info = pygame.display.Info() width = info.current_w height = info.current_h if width >= height: win = pygame.display.set_mode(SCREEN, pygame.NOFRAME) else: win = pygame.display.set_mode(SCREEN, pygame.NOFRAME | pygame.SCALED | pygame.FULLSCREEN) clock = pygame.time.Clock() FPS = 60 # COLORS ********************************************************************** WHITE = (255, 255, 255) BLUE = (30, 144,255) RED = (255, 0, 0) GREEN = (0, 255, 0) BLACK = (0, 0, 20) # IMAGES ********************************************************************** plane_img = pygame.image.load('Assets/plane.png') logo_img = pygame.image.load('Assets/logo.png') fighter_img = pygame.image.load('Assets/fighter.png') clouds_img = pygame.image.load('Assets/clouds.png') clouds_img = pygame.transform.scale(clouds_img, (WIDTH, 350)) home_img = pygame.image.load('Assets/Buttons/homeBtn.png') replay_img = pygame.image.load('Assets/Buttons/replay.png') sound_off_img = pygame.image.load("Assets/Buttons/soundOffBtn.png") sound_on_img = pygame.image.load("Assets/Buttons/soundOnBtn.png") # BUTTONS ********************************************************************* home_btn = Button(home_img, (24, 24), WIDTH // 4 - 18, HEIGHT//2 + 120) replay_btn = Button(replay_img, (36,36), WIDTH // 2 - 18, HEIGHT//2 + 115) sound_btn = Button(sound_on_img, (24, 24), WIDTH - WIDTH // 4 - 18, HEIGHT//2 + 120) # FONTS *********************************************************************** game_over_font = 'Fonts/ghostclan.ttf' tap_to_play_font = 'Fonts/BubblegumSans-Regular.ttf' score_font = 'Fonts/DalelandsUncialBold-82zA.ttf' final_score_font = 'Fonts/DroneflyRegular-K78LA.ttf' game_over_msg = Message(WIDTH//2, 230, 30, 'Game Over', game_over_font, WHITE, win) score_msg = Message(WIDTH-50, 28, 30, '0', final_score_font, RED, win) final_score_msg = Message(WIDTH//2, 280, 30, '0', final_score_font, RED, win) tap_to_play_msg = tap_to_play = BlinkingText(WIDTH//2, HEIGHT-60, 25, "Tap To Play", tap_to_play_font, WHITE, win) # SOUNDS ********************************************************************** player_bullet_fx = pygame.mixer.Sound('Sounds/gunshot.wav') click_fx = pygame.mixer.Sound('Sounds/click.mp3') collision_fx = pygame.mixer.Sound('Sounds/mini_exp.mp3') blast_fx = pygame.mixer.Sound('Sounds/blast.wav') fuel_fx = pygame.mixer.Sound('Sounds/fuel.wav') pygame.mixer.music.load('Sounds/Defrini - Spookie.mp3') pygame.mixer.music.play(loops=-1) pygame.mixer.music.set_volume(0.1) # GROUPS & OBJECTS ************************************************************ bg = Background(win) p = Player(144, HEIGHT - 100) enemy_group = pygame.sprite.Group() player_bullet_group = pygame.sprite.Group() enemy_bullet_group = pygame.sprite.Group() explosion_group = pygame.sprite.Group() fuel_group = pygame.sprite.Group() powerup_group = pygame.sprite.Group() # FUNCTIONS ******************************************************************* def shoot_bullet(): x, y = p.rect.center[0], p.rect.y if p.powerup > 0: for dx in range(-3, 4): b = Bullet(x, y, 4, dx) player_bullet_group.add(b) p.powerup -= 1 else: b = Bullet(x-30, y, 6) player_bullet_group.add(b) b = Bullet(x+30, y, 6) player_bullet_group.add(b) player_bullet_fx.play() def reset(): enemy_group.empty() player_bullet_group.empty() enemy_bullet_group.empty() explosion_group.empty() fuel_group.empty() powerup_group.empty() p.reset(p.x, p.y) # VARIABLES ******************************************************************* level = 1 plane_destroy_count = 0 plane_frequency = 5000 start_time = pygame.time.get_ticks() moving_left = False moving_right = False home_page = True game_page = False score_page = False score = 0 sound_on = True running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE or event.key == pygame.K_q: running = False if event.type == pygame.KEYDOWN and game_page: if event.key == pygame.K_LEFT: moving_left = True if event.key == pygame.K_RIGHT: moving_right = True if event.key == pygame.K_SPACE: shoot_bullet() if event.type == pygame.MOUSEBUTTONDOWN: if home_page: home_page = False game_page = True click_fx.play() elif game_page: x, y = event.pos if p.rect.collidepoint((x,y)): shoot_bullet() elif x <= WIDTH // 2: moving_left = True elif x > WIDTH // 2: moving_right = True if event.type == pygame.KEYUP: moving_left = False moving_right = False if event.type == pygame.MOUSEBUTTONUP: moving_left = False moving_right = False if home_page: win.fill(BLACK) win.blit(logo_img, (30, 80)) win.blit(fighter_img, (WIDTH//2 - 50, HEIGHT//2)) pygame.draw.circle(win, WHITE, (WIDTH//2, HEIGHT//2 + 50), 50, 4) tap_to_play_msg.update() if score_page: win.fill(BLACK) win.blit(logo_img, (30, 50)) game_over_msg.update() final_score_msg.update(score) if home_btn.draw(win): home_page = True game_page = False score_page = False reset() click_fx.play() plane_destroy_count = 0 level = 1 score = 0 if replay_btn.draw(win): score_page = False game_page = True reset() click_fx.play() plane_destroy_count = 0 score = 0 if sound_btn.draw(win): sound_on = not sound_on if sound_on: sound_btn.update_image(sound_on_img) pygame.mixer.music.play(loops=-1) else: sound_btn.update_image(sound_off_img) pygame.mixer.music.stop() if game_page: current_time = pygame.time.get_ticks() delta_time = current_time - start_time if delta_time >= plane_frequency: if level == 1: type = 1 elif level == 2: type = 2 elif level == 3: type = 3 elif level == 4: type = random.randint(4, 5) elif level == 5: type = random.randint(1, 5) x = random.randint(10, WIDTH - 100) e = Enemy(x, -150, type) enemy_group.add(e) start_time = current_time if plane_destroy_count: if plane_destroy_count % 5 == 0 and level < 5: level += 1 plane_destroy_count = 0 p.fuel -= 0.05 bg.update(1) win.blit(clouds_img, (0, 70)) p.update(moving_left, moving_right, explosion_group) p.draw(win) player_bullet_group.update() player_bullet_group.draw(win) enemy_bullet_group.update() enemy_bullet_group.draw(win) explosion_group.update() explosion_group.draw(win) fuel_group.update() fuel_group.draw(win) powerup_group.update() powerup_group.draw(win) enemy_group.update(enemy_bullet_group, explosion_group) enemy_group.draw(win) if p.alive: player_hit = pygame.sprite.spritecollide(p, enemy_bullet_group, False) for bullet in player_hit: p.health -= bullet.damage x, y = bullet.rect.center explosion = Explosion(x, y, 1) explosion_group.add(explosion) bullet.kill() collision_fx.play() for bullet in player_bullet_group: planes_hit = pygame.sprite.spritecollide(bullet, enemy_group, False) for plane in planes_hit: plane.health -= bullet.damage if plane.health <= 0: x, y = plane.rect.center rand = random.random() if rand >= 0.9: power = Powerup(x, y) powerup_group.add(power) elif rand >= 0.3: fuel = Fuel(x, y) fuel_group.add(fuel) plane_destroy_count += 1 blast_fx.play() x, y = bullet.rect.center explosion = Explosion(x, y, 1) explosion_group.add(explosion) bullet.kill() collision_fx.play() player_collide = pygame.sprite.spritecollide(p, enemy_group, True) if player_collide: x, y = p.rect.center explosion = Explosion(x, y, 2) explosion_group.add(explosion) x, y = player_collide[0].rect.center explosion = Explosion(x, y, 2) explosion_group.add(explosion) p.health = 0 p.alive = False if pygame.sprite.spritecollide(p, fuel_group, True): p.fuel += 25 if p.fuel >= 100: p.fuel = 100 fuel_fx.play() if pygame.sprite.spritecollide(p, powerup_group, True): p.powerup += 2 fuel_fx.play() if not p.alive or p.fuel <= -10: if len(explosion_group) == 0: game_page = False score_page = True reset() score += 1 score_msg.update(score) fuel_color = RED if p.fuel <= 40 else GREEN pygame.draw.rect(win, fuel_color, (30, 20, p.fuel, 10), border_radius=4) pygame.draw.rect(win, WHITE, (30, 20, 100, 10), 2, border_radius=4) pygame.draw.rect(win, BLUE, (30, 32, p.health, 10), border_radius=4) pygame.draw.rect(win, WHITE, (30, 32, 100, 10), 2, border_radius=4) win.blit(plane_img, (10, 15)) pygame.draw.rect(win, WHITE, (0,0, WIDTH, HEIGHT), 5, border_radius=4) clock.tick(FPS) pygame.display.update() pygame.quit() 转c++
最新发布
05-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值