Pygame实战练习之一百层游戏
程序员文章站
2022-06-24 08:04:54
导语哈喽哈喽!大家好!我是木木子,又到了每日游戏更新环节!8月30日,对暑假还意犹未尽的孩子们收到了一份“开学大礼”:通知要求,严格限制向未成年人提供网络游戏服务的时间!啊这~禁了网游,...
导语
哈喽哈喽!大家好!我是木木子,又到了每日游戏更新环节!
8月30日,对暑假还意犹未尽的孩子们收到了一份“开学大礼”:
通知要求,严格限制向未成年人提供网络游戏服务的时间!啊这~禁了网游,那这波单机游戏的就是一波收割大胜利。
安排!家里的孩子没游戏玩了怎么办?当然是由我提供新鲜热乎的游戏代码!
正文
给整了一个一百层的闯关游戏!能完美通关的话孩子得玩很长一段时间,那我就解放了!!
主要分为二部分:
import pygame from pygame.locals import * from sys import exit four_neigh = {"left": (0, -1), "right": (0, 1), "up": (-1, 0), "down": (1, 0)} eight_neigh = list(four_neigh.values()) + [(1, 1), (1, -1), (-1, 1), (-1, -1)] direction = {pygame.k_up: "up", pygame.k_left: "left", pygame.k_right: "right", pygame.k_down: "down"} def hex2rgb(color): b = color % 256 color = color >> 8 g = color % 256 color = color >> 8 r = color % 256 return (r, g, b) class game(object): def __init__(self, title, size, fps=30): self.size = size pygame.init() self.screen = pygame.display.set_mode(size, 0, 32) pygame.display.set_caption(title) self.keys = {} self.keys_up = {} self.clicks = {} self.timer = pygame.time.clock() self.fps = fps self.score = 0 self.end = false self.fullscreen = false self.last_time = pygame.time.get_ticks() self.is_pause = false self.is_draw = true self.score_font = pygame.font.sysfont("calibri", 130, true) def bind_key(self, key, action): if isinstance(key, list): for k in key: self.keys[k] = action elif isinstance(key, int): self.keys[key] = action def bind_key_up(self, key, action): if isinstance(key, list): for k in key: self.keys_up[k] = action elif isinstance(key, int): self.keys_up[key] = action def bind_click(self, button, action): self.clicks[button] = action def pause(self, key): self.is_pause = not self.is_pause def set_fps(self, fps): self.fps = fps def handle_input(self, event): if event.type == pygame.quit: pygame.quit() exit() if event.type == pygame.keydown: if event.key in self.keys.keys(): self.keys[event.key](event.key) if event.key == pygame.k_f11: # f11全屏 self.fullscreen = not self.fullscreen if self.fullscreen: self.screen = pygame.display.set_mode(self.size, pygame.fullscreen, 32) else: self.screen = pygame.display.set_mode(self.size, 0, 32) if event.type == pygame.keyup: if event.key in self.keys_up.keys(): self.keys_up[event.key](event.key) if event.type == pygame.mousebuttondown: if event.button in self.clicks.keys(): self.clicks[event.button](*event.pos) def run(self): while true: for event in pygame.event.get(): self.handle_input(event) self.timer.tick(self.fps) self.update(pygame.time.get_ticks()) self.draw(pygame.time.get_ticks()) def draw_score(self, color, rect=none): score = self.score_font.render(str(self.score), true, color) if rect is none: r = self.screen.get_rect() rect = score.get_rect(center=r.center) self.screen.blit(score, rect) def is_end(self): return self.end def update(self, current_time): pass def draw(self, current_time): pass class test(game): def __init__(self, title, size, fps=30): super(test, self).__init__(title, size, fps) self.bind_key(pygame.k_return, self.press_enter) def press_enter(self): print("press enter") def draw(self, current_time): pass def press_space(key): print("press space.") def click(x, y): print(x, y) def main(): print(hex2rgb(0xfcf040)) game = test("game", (640, 480)) game.bind_key(pygame.k_space, press_space) game.bind_click(1, click) game.run() 其二: import pygame import game from random import choice, randint score = 0 solid = 1 fragile = 2 deadly = 3 belt_left = 4 belt_right = 5 body = 6 game_row = 40 game_col = 28 obs_width = game_col // 4 side = 13 screen_width = side*game_col screen_height = side*game_row color = {solid: 0x00ffff, fragile: 0xff5500, deadly: 0xff2222, score: 0xcccccc, belt_left: 0xffff44, belt_right: 0xff99ff, body: 0x00ff00} choice = [solid, solid, solid, fragile, fragile, belt_left, belt_right, deadly] class barrier(object): def __init__(self, screen, opt=none): self.screen = screen if opt is none: self.type = choice(choice) else: self.type = opt self.frag_touch = false self.frag_time = 12 self.score = false self.belt_dire = 0 self.belt_dire = pygame.k_left if self.type == belt_left else pygame.k_right left = randint(0, screen_width - 7 * side - 1) top = screen_height - side - 1 self.rect = pygame.rect(left, top, 7*side, side) def rise(self): if self.frag_touch: self.frag_time -= 1 if self.frag_time == 0: return false self.rect.top -= 2 return self.rect.top >= 0 def draw_side(self, x, y): if self.type == solid: rect = pygame.rect(x, y, side, side) self.screen.fill(color[solid], rect) elif self.type == fragile: rect = pygame.rect(x+2, y, side-4, side) self.screen.fill(color[fragile], rect) elif self.type == belt_left or self.type == belt_right: rect = pygame.rect(x, y, side, side) pygame.draw.circle(self.screen, color[self.type], rect.center, side // 2 + 1) elif self.type == deadly: p1 = (x + side//2 + 1, y) p2 = (x, y + side) p3 = (x + side, y + side) points = [p1, p2, p3] pygame.draw.polygon(self.screen, color[deadly], points) def draw(self): for i in range(7): self.draw_side(i*side+self.rect.left, self.rect.top) class hell(game.game): def __init__(self, title, size, fps=60): super(hell, self).__init__(title, size, fps) self.last = 6 * side self.dire = 0 self.barrier = [barrier(self.screen, solid)] self.body = pygame.rect(self.barrier[0].rect.center[0], 200, side, side) self.bind_key([pygame.k_left, pygame.k_right], self.move) self.bind_key_up([pygame.k_left, pygame.k_right], self.unmove) self.bind_key(pygame.k_space, self.pause) def move(self, key): self.dire = key def unmove(self, key): self.dire = 0 def show_end(self): self.draw(0, end=true) self.end = true def move_man(self, dire): if dire == 0: return true rect = self.body.copy() if dire == pygame.k_left: rect.left -= 1 else: rect.left += 1 if rect.left < 0 or rect.left + side >= screen_width: return false for ba in self.barrier: if rect.colliderect(ba.rect): return false self.body = rect return true def get_score(self, ba): if self.body.top > ba.rect.top and not ba.score: self.score += 1 ba.score = true def to_hell(self): self.body.top += 2 for ba in self.barrier: if not self.body.colliderect(ba.rect): self.get_score(ba) continue if ba.type == deadly: self.show_end() return self.body.top = ba.rect.top - side - 2 if ba.type == fragile: ba.frag_touch = true elif ba.type == belt_left or ba.type == belt_right: # self.body.left += ba.belt_dire self.move_man(ba.belt_dire) break top = self.body.top if top < 0 or top+side >= screen_height: self.show_end() def create_barrier(self): solid = list(filter(lambda ba: ba.type == solid, self.barrier)) if len(solid) < 1: self.barrier.append(barrier(self.screen, solid)) else: self.barrier.append(barrier(self.screen)) self.last = randint(3, 5) * side def update(self, current_time): if self.end or self.is_pause: return self.last -= 1 if self.last == 0: self.create_barrier() for ba in self.barrier: if not ba.rise(): if ba.type == fragile and ba.rect.top > 0: self.score += 1 self.barrier.remove(ba) self.move_man(self.dire) self.move_man(self.dire) self.to_hell() def draw(self, current_time, end=false): if self.end or self.is_pause: return self.screen.fill(0x000000) self.draw_score((0x3c, 0x3c, 0x3c)) for ba in self.barrier: ba.draw() if not end: self.screen.fill(color[body], self.body) else: self.screen.fill(color[deadly], self.body) pygame.display.update() if __name__ == '__main__': hell = hell("一百层", (screen_width, screen_height)) hell.run()
效果图如下:
总结
我懂!我懂!我现在话不多说都是直接上代码滴!
记得三连哦~mua 你们的支持是我最大的动力!
到此这篇关于pygame实战练习之一百层游戏的文章就介绍到这了,更多相关pygame 一百层游戏内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!