用python实现弹球小游戏

目录
  • 一、弹球游戏代码
  • 二、程序结果
  • 总结

一、弹球游戏代码

下文是tkinter的应用实例,实现弹球游戏,通过<--和-->件移动平板接球。

from tkinter import *
import random
import time

# Creating the window:
window = Tk()
window.title("Bounce")
window.geometry('600x600')
window.resizable(False, False)

# Creating the canvas containing the game:
canvas = Canvas(window, width = 450, height = 450, bg = "black")
canvas.pack(padx = 50, pady= 50)
score = canvas.create_text(10, 20, fill = "white")

window.update()

class Ball:
    def __init__(self, canvas1, paddle1, color):
        self.canvas = canvas1
        self.paddle = paddle1
        self.id = canvas1.create_oval(10, 10, 25, 25, fill = color)  # The starting point of the ball
        self.canvas.move(self.id, 190, 160)
        starting_direction = [-3, -2, -1, 0, 1, 2, 3]
        random.shuffle(starting_direction)
        self.x = starting_direction[0]
        self.y = -3
        self.canvas_height = self.canvas.winfo_height()
        self.canvas_width = self.canvas.winfo_width()

    # Detecting the collision between the ball and the paddle:
    def hit_paddle(self, ballcoords):
        paddle_pos = self.canvas.coords(self.paddle.id)
        if ballcoords[0] <= paddle_pos[2] and ballcoords[2] >= paddle_pos[0]:
            if paddle_pos[3] >= ballcoords[3] >= paddle_pos[1]:
                return True
        return False

    # Detecting the collision between the the ball and the canvas sides:
    def draw(self):
        self.canvas.move(self.id, self.x, self.y)
        ballcoords = self.canvas.coords(self.id)
        if ballcoords[1] <= 0:
            self.y = 3
        if ballcoords[3] >= self.canvas_height:
            self.y = 0
            self.x = 0
            self.canvas.create_text(225, 150, text = "Game Over!", font = ("Arial", 16), fill = "white")
        if ballcoords[0] <= 0:
            self.x = 3
        if ballcoords[2] >= self.canvas_width:
            self.x = -3
        if self.hit_paddle(ballcoords):
            self.y = -3

class Paddle:
    def __init__(self, canvas1, color):
        self.canvas1 = canvas
        self.id = canvas.create_rectangle(0, 0, 100, 10, fill = color)
        self.canvas1.move(self.id, 180, 350)
        self.x = 0
        self.y = 0
        self.canvas1_width = canvas1.winfo_width()
        self.canvas1.bind_all("<Left>", self.left)
        self.canvas1.bind_all("<Right>", self.right)

    def draw(self):
        self.canvas1.move(self.id, self.x, 0)
        paddlecoords = self.canvas1.coords(self.id)
        if paddlecoords[0] <= 0:
            self.x = 0
        if paddlecoords[2] >= self.canvas1_width:
            self.x = 0

    def right(self, event):
        self.x = 3

    def left(self, event):
        self.x = -3

paddle = Paddle(canvas, color = "white")
ball = Ball(canvas, paddle, color = "red")

# New code after here
def handler():
    global run
    run = False

window.protocol("WM_DELETE_WINDOW", handler)
run = True

while run:
    # New code before here
    ball.draw()
    paddle.draw()
    window.update_idletasks()
    window.update()
    time.sleep(0.01)

window.destroy()    # should always destroy window before exit

二、程序结果

总结

到此这篇关于用python实现弹球小游戏的文章就介绍到这了,更多相关python弹球游戏内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • python 实现弹球游戏的示例代码

    运行效果 实现代码 # -*- coding: utf-8 -*- import tkinter as tkinter import tkinter.messagebox as mb import random,time class Ball(): ''' 创建Ball类,初始化对象,即创建对象设置属性, init函数是在对象被创建的同时就设置属性的一种方法,Python会在创建新对象时自动调用这个函数. ''' def __init__(self,canvas,paddle,score,col

  • Python实现弹球小游戏

    本文主要给大家分享一个实战项目,通过python代码写一款我们儿时大多数人玩过的游戏---小弹球游戏.只不过当时,我们是在游戏机上玩,现在我们通过运行代码来玩,看看大家是否有不一样的体验,是否可以重温当年的乐趣呢! 整个游戏实现比较简单,只需在安装python的电脑上即可运行,玩游戏,通过键盘键控制弹球挡板的移动即可.原理不多说,且让我们去看看吧. 1.代码运行后,游戏界面如下所示: 2.游戏过程中,界面如下所示: 3.游戏结束后,界面如下所示: 游戏实现部分源码如下: def main():

  • python实现简单反弹球游戏

    python简单游戏-反弹球,供大家参考,具体内容如下 tkinter实现,直接贴上代码 from tkinter import* import time import random class Ball: def __init__(self,canvas,paddle,color): self.canvas = canvas self.paddle = paddle self.id = canvas.create_oval(10,10,25,25,fill=color) self.canvas

  • 用Python写一个简易版弹球游戏

    我们前面讲了几篇关于类的知识点,为了让大家更好的掌握类的概念,并灵活的运用这些知识,我写了一个有趣又好玩的弹球的游戏,一来可以把类的知识融会一下,二来加深对Python的兴趣.你会发现哎呀Python写小游戏还是蛮方便的,蛮有意思的~~ 先看一下我们的最终效果图 我们分9步来讲解如何写这个小游戏 1.创建游戏的主界面 我们用Python的内置模块Tkinter来完成了,它是Python的标准GUI工具包,可以非常方便在制作GUI小工具,因为是跨平台的,可以方便的在win和linux下运行,我们用

  • 使用python和pygame制作挡板弹球游戏

    python是个很有趣的语言,可以在cmd命令窗口运行,还有很多的功能强大的模块. 学了一天pygame,用python和pygame写一个简单的挡板弹球游戏. 2018年6月21日 00:15:21 GitHub: EasyBaffleBallGame # -*- coding:utf-8 -*- from sys import exit import pygame from pygame.locals import * pygame.init() # 创建窗口 ScreenWidth = 5

  • python pygame实现挡板弹球游戏

    学了一天pygame,用python和pygame写一个简单的挡板弹球游戏 GitHub: EasyBaffleBallGame # -*- coding:utf-8 -*- from sys import exit import pygame from pygame.locals import * pygame.init() # 创建窗口 ScreenWidth = 500 ScreenHright = 720 ScreenSize = (ScreenWidth, ScreenHright)

  • 用python实现弹球小游戏

    目录 一.弹球游戏代码 二.程序结果 总结 一.弹球游戏代码 下文是tkinter的应用实例,实现弹球游戏,通过<--和-->件移动平板接球. from tkinter import * import random import time # Creating the window: window = Tk() window.title("Bounce") window.geometry('600x600') window.resizable(False, False) #

  • Python实现的弹球小游戏示例

    本文实例讲述了Python实现的弹球小游戏.分享给大家供大家参考,具体如下: 弹球 1. Ball 类 draw负责移动Ball 碰撞检测,反弹,Ball检测Paddle 2.Paddle类 draw负责移动Paddle 碰撞检测,确定能不能继续 监听键盘事件 3.主循环 绘制Ball和Paddle update sleep 代码 from Tkinter import * import random import time class Ball: def __init__(self, canv

  • Python基于Tkinter模块实现的弹球小游戏

    本文实例讲述了Python基于Tkinter模块实现的弹球小游戏.分享给大家供大家参考,具体如下: #!usr/bin/python #-*- coding:utf-8 -*- from Tkinter import * import Tkinter import random import time #创建小球的类 class Ball: def __init__(self,canvas,paddle,color): #参数:画布,球拍和颜色 self.canvas = canvas self

  • python运用pygame库实现双人弹球小游戏

    使用python pygame库实现一个双人弹球小游戏,两人分别控制一个左右移动的挡板用来拦截小球,小球会在两板间不停弹跳,拦截失败的一方输掉游戏,规则类似于简化版的乒乓球. 因为是第一次用pygame写python小游戏并且只用了两三个小时,所以有些粗糙,部分方面有些bug,比如板子可以移动出屏幕外,游戏结束后的提示显示不全. 但是关键部分如小球的移动和基本功能等,还算比较完善. 代码如下: 运行环境为python 3.7,需要安装pygame库 import pygame,sys,time,

  • 使用Python写一个小游戏

    引言 最近python语言大火,除了在科学计算领域python有用武之地之外,在游戏.后台等方面,python也大放异彩,本篇博文将按照正规的项目开发流程,手把手教大家写个python小游戏,来感受下其中的有趣之处.本次开发的游戏叫做alien invasion. 安装pygame并创建能左右移动的飞船 安装pygame 本人电脑是windows 10.python3.6,pygame下载地址: 传送门 请自行下载对应python版本的pygame 运行以下命令 $ pip install wh

  • python实现猜拳小游戏

    用python实现猜拳小游戏,供大家参考,具体内容如下 本练习旨在养成良好的编码习惯和练习逻辑思考. 1.使用python版本: 3.7.3: 2.代码内容实现如下 #!/usr/bin/env python # -*- coding: utf-8 -*- """ 简单实现猜拳小游戏,默认每回合 五局 Version: 0.1 Author: smartbabble Date: 2018-03-12 """ from random import

  • python实现五子棋小游戏

    本文实例为大家分享了python实现五子棋小游戏的具体代码,供大家参考,具体内容如下 暑假学了十几天python,然后用pygame模块写了一个五子棋的小游戏,代码跟有缘人分享一下. import numpy as np import pygame import sys import traceback import copy from pygame.locals import * pygame.init() pygame.mixer.init() #颜色 background=(201,202

  • python3实现弹弹球小游戏

    本文实例为大家分享了python3实现弹弹球小游戏的具体代码,供大家参考,具体内容如下 from tkinter import * from tkinter import messagebox import random import time from PIL import Image, ImageTk import sys class Game: def __init__(self): self.tk = Tk() self.times = 0 sw = self.tk.winfo_scre

  • python实现大战外星人小游戏实例代码

    主程序 import pygame from pygame.sprite import Group from settings import Settings from game_stats import gameStats from ship import Ship from button import Button import game_functions as gf def run_game(): #初始化背景设置 pygame.init() #创建一个Settings实例,并将其储存在

随机推荐