PyGobject(一百零二)Cairo系列——贪吃蛇游戏

例子

这里写图片描述
代码:

#!/usr/bin/env python3
# Created by xiaosanyu at 16/7/6
# section 152
TITLE = "Snake game"
DESCRIPTION = """
Snake is an older classic video game. It was first created in late 70s.
Later it was brought to PCs. In this game the player controls a snake.
The objective is to eat as many apples as possible.
Each time the snake eats an apple, its body grows.
The snake must avoid the walls and its own body.
This game is sometimes called Nibbles.
"""

import gi

gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk, GLib

import cairo
import sys
import random
import os

FONT_SIZE = 14
WIDTH = 300
HEIGHT = 270
DOT_SIZE = 10
ALL_DOTS = int(WIDTH * HEIGHT / (DOT_SIZE * DOT_SIZE))
RAND_POS = 26
DELAY = 200
x = [0] * ALL_DOTS
y = [0] * ALL_DOTS


class Board(Gtk.DrawingArea):
    def __init__(self):
        super(Board, self).__init__()
        self.modify_bg(Gtk.StateFlags.NORMAL, Gdk.Color(0, 0, 0))
        self.set_size_request(WIDTH, HEIGHT)

        self.connect("draw", self.draw)
        self.init_game()

    def on_timer(self):
        if self.inGame:
            self.check_apple()
            self.check_collision()
            self.move()
            self.queue_draw()
            return True
        else:
            return False

    def init_game(self):
        self.left = False
        self.right = True
        self.up = False
        self.down = False
        self.inGame = True
        self.dots = 3
        for i in range(self.dots):
            x[i] = 50 - i * 10
            y[i] = 50
        try:
            self.dot = cairo.ImageSurface.create_from_png(os.path.join(os.path.dirname(__file__), "../data/dot.png"))
            self.head = cairo.ImageSurface.create_from_png(os.path.join(os.path.dirname(__file__), "../data/head.png"))
            self.apple = cairo.ImageSurface.create_from_png(
                    os.path.join(os.path.dirname(__file__), "../data/apple.png"))
        except Exception as e:
            print(e)
            sys.exit(1)
        self.locate_apple()
        self.start_game()

    def start_game(self):
        GLib.timeout_add(DELAY, self.on_timer)

    def draw(self, widget, cr):
        if self.inGame:
            cr.set_source_rgb(0.6, 0.6, 0.6)
            cr.paint()
            cr.set_source_surface(self.apple, self.apple_x, self.apple_y)
            cr.paint()
            for z in range(self.dots):
                if z == 0:
                    cr.set_source_surface(self.head, x[z], y[z])
                    cr.paint()
                else:
                    cr.set_source_surface(self.dot, x[z], y[z])
                    cr.paint()
        else:
            self.game_over(widget, cr)

    def game_over(self, widget, cr):
        w = widget.get_allocated_width()
        h = widget.get_allocated_height()
        (x, y, width, height, dx, dy) = cr.text_extents("Game Over")
        cr.set_source_rgb(65535, 0, 0)
        cr.set_font_size(FONT_SIZE)
        cr.move_to((w - width) / 2, h / 2)
        cr.show_text("Game Over")
        self.inGame = False

    def check_apple(self):
        if x[0] == self.apple_x and y[0] == self.apple_y:
            self.dots += 1
            self.locate_apple()

    def move(self):
        z = self.dots

        while z > 0:
            x[z] = x[(z - 1)]
            y[z] = y[(z - 1)]
            z -= 1
        if self.left:
            x[0] -= DOT_SIZE
        if self.right:
            x[0] += DOT_SIZE
        if self.up:
            y[0] -= DOT_SIZE
        if self.down:
            y[0] += DOT_SIZE

    def check_collision(self):
        z = self.dots
        while z > 0:
            if z > 4 and x[0] == x[z] and y[0] == y[z]:
                self.inGame = False
            z -= 1
        if y[0] > HEIGHT - DOT_SIZE:
            self.inGame = False
        if y[0] < 0:
            self.inGame = False
        if x[0] > WIDTH - DOT_SIZE:
            self.inGame = False
        if x[0] < 0:
            self.inGame = False

    def locate_apple(self):
        r = random.randint(0, RAND_POS)
        self.apple_x = r * DOT_SIZE
        r = random.randint(0, RAND_POS)
        self.apple_y = r * DOT_SIZE

    def on_key_down(self, event):
        key = event.keyval
        if key == Gdk.KEY_Left and not self.right:
            self.left = True
            self.up = False
            self.down = False

        if key == Gdk.KEY_Right and not self.left:
            self.right = True
            self.up = False
            self.down = False
        if key == Gdk.KEY_Up and not self.down:
            self.up = True
            self.right = False
            self.left = False
        if key == Gdk.KEY_Down and not self.up:
            self.down = True
            self.right = False
            self.left = False
        if key == Gdk.KEY_space:
            if self.inGame:
                self.inGame = False
            else:
                self.inGame = True
                self.start_game()


class Snake(Gtk.Window):
    def __init__(self):
        super(Snake, self).__init__()
        self.set_title('Snake')
        self.set_size_request(WIDTH, HEIGHT)

        self.set_resizable(False)
        self.move((Gdk.Screen.width() - WIDTH) / 2, (Gdk.Screen.height() - HEIGHT) / 2)
        self.board = Board()
        self.connect("key-press-event", self.on_key_down)
        self.add(self.board)
        self.connect("destroy", Gtk.main_quit)
        self.show_all()

    def on_key_down(self, widget, event):
        key = event.keyval
        self.board.on_key_down(event)


def main():
    Snake()
    Gtk.main()


if __name__ == "__main__":
    main()





代码下载地址:http://download.csdn.net/detail/a87b01c14/9594728

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sanxiaochengyu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值