简单实现python数独游戏,python编写弹球游戏的实现代码

网上看到一个python写的数独,很好玩,分享给大家。

 使用python和java实现数独游戏,有比较才有收获哦。

 弹球游戏:

import random
import itertools
from copy import deepcopy

def make_board(m = 3):
 numbers = list(range(1, m**2 + 1))
 board = None

 while board is None:
 board = attempt_board(m, numbers)
 return board

def attempt_board(m, numbers):
 n = m**2
 board = [[None for _ in range(n)] for _ in range(n)]
 for i, j in itertools.product(range(n), repeat = 2):
 i0, j0 = i - i % m, j - j % m
 random.shuffle(numbers)
 for x in numbers:
  if(x not in board[i]) and all(row[j] != x for row in board) and all(x not in row[j0:j0+m] for row in board[i0:i]):
  board[i][j] = x
  break
 else:
  return None
 return board

def print_board(board, m = 3):
 numbers = list(range(1, m**2 + 1))
 omit = 5
 challange = deepcopy(board)
 for i, j in itertools.product(range(omit), range(m ** 2)):
 x = random.choice(numbers) - 1
 challange[x][j] = None
 spacer = "++---+---+---++---+---+---++---+---+---++"
 print (spacer.replace('-', '='))
 for i, line in enumerate(challange):
 print("|| {0} | {1} | {2} || {3} | {4} | {5} || {6} | {7} | {8} ||".format(*(cell or ' ' for cell in line)))
 if(i + 1) % 3 == 0:
  print(spacer.replace('-', '='))
 else:
  print(spacer)
 return challange

def print_answer(board):
 spacer = "++---+---+---++---+---+---++---+---+---++"
 print(spacer.replace('-','='))
 for i, line in enumerate(board):
 print("|| {0} | {1} | {2} || {3} | {4} | {5} || {6} | {7} | {8} ||".format(*(cell or ' ' for cell in line)))
 if(i + 1) % 3 == 0:
  print(spacer.replace('-','='))
 else:
  print(spacer)

def is_full(challange, m = 3):
 for i, j in itertools.product(range(m**2), repeat = 2):
 if challange[i][j] is None:
  return False
 return True

def cal_candidate(challange, x, y, m = 3):
 candidate = range(1, m ** 2 + 1)
 for i in range(m ** 2):
 if challange[x][i] in candidate:
  candidate.remove(challange[x][i])
 if challange[i][y] in candidate:
  candidate.remove(challange[i][y])
 for i, j in itertools.product(range(m), repeat = 2):
 x0, y0 = x - x % m, y - y % m
 if challange[x0 + i][y0 + j] in candidate:
  candidate.remove(challange[x0 + i][y0 + j])
 return candidate

def least_candidate(challange, m = 3):
 least, x, y = m ** 2, -1, -1
 for i, j in itertools.product(range(m ** 2), repeat = 2):
 if not challange[i][j]:
  num = len(cal_candidate(challange, i, j))
  if num < least:
  least = num
  x, y = i, j
 return x, y

def solving_soduku(challange, m = 3):
 if is_full(challange):
 return challange
 x, y = least_candidate(challange)
 id = x * (m ** 2) + y
 result = try_candidate(challange, id)
 return result

def try_candidate(challange, id, m = 3):
 if is_full(challange):
 return challange
 x = id / (m ** 2)
 y = id % (m ** 2)
 while challange[x][y]:
 id = (id + 1) % m ** 4
 x = id / (m ** 2)
 y = id % (m ** 2)
 candidate = cal_candidate(challange, x, y)
 if len(candidate) == 0:
 return False
 for i in range(len(candidate)):
 challange[x][y] = candidate[i]
 result_r = try_candidate(challange, (id + 1) % m ** 4)
 if not result_r:
  pass
 else:
  return challange
 challange[x][y] = None
 return False


#Board = make_board()
#print Board
#challange = print_board(Board)
#print_answer(Board)

#result = solving_soduku(challange) 
#print_answer(result) 


testing = [[8, None, None, None, None, None, None, None, None],
   [None, None, 3, 6, None, None, None, None, None],
   [None, 7, None, None, 9, None, 2, None, None],
   [None,5 , None, None, None, 7, None, None, None ],
   [None, None, None, None, 4, 6, 7, None, None],
   [None, None, None, 1, None, None, None, 3, None],
   [None, None, 1, None, None, None, None, 6, 8],
   [None, None, 8, 5, None, None, None, 1, None],
   [None, 9, None, None, None, None, 4, None, None]]
result = solving_soduku(testing)
print_answer(result)

1、Python版

from tkinter import *       
import time 
import random 
tk=Tk()    #创建一个界面 
tk.title("弹球游戏") 
canvas=Canvas(tk,width=800,height=600,bg="skyblue",bd=0,highlightthickness = 0) 
tk.resizable(0,0) #表示边框不能被拉伸 
canvas.pack() #使部件放在主窗口中 
tk.update()  #刷新界面 
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.move(self.id,240,100)   #初始球的位置 
    stat=[-3,-2,-1,1,2,3]   
    random.shuffle(stat) 
    self.x=stat[0] 
    self.y=-3 
    self.canvas_height=self.canvas.winfo_height() #获取画布的的高度 
    self.canvas_width=self.canvas.winfo_width() 
    self.hit_bottom=False  
  def hit_paddle(self, pos):    #判断输赢 
    paddle_pos = self.canvas.coords(self.paddle.id ) 
    if pos[2]>= paddle_pos[0] and pos[0]<= paddle_pos[2]: 
      if pos[3]>= paddle_pos[1] and pos[3]<= paddle_pos[3]: 
        return True 
    return False 
  def draw(self): #小球移动 
    self.canvas.move(self.id,self.x,self.y) 
    pos=self.canvas.coords(self.id) 
    if pos[1]<=0: #判断小球是否碰到边框,如果碰到回弹 
      self.y=3 
    if pos[3]>=self.canvas_height: #判断球拍是否有接到球 ,如果没接到游戏结束 
      self.hit_bottom=True 
    if self.hit_paddle(pos)==True: #判断求是否碰到了球拍,如果碰到了使小球回弹 
      self.y=-3 
    if pos[0]<=0: #来判断球拍是不是碰到了边框,, 
      self.x=3 
    if pos[2]>=self.canvas_width: 
      self.x=-3 
class Paddle: #球拍的的类 
  def __init__(self,canvans,color): 
    self.canvas=canvas 
    self.id=canvas.create_rectangle(0,0,150,10,fill=color) 
    self.canvas.move(self.id,400,450) 
    self.x=0 
    self.canvas_width=self.canvas.winfo_width() 
    self.canvas.bind_all("<KeyPress-Left>",self.turn_left) #通过按键来使球拍移动 
    self.canvas.bind_all("<KeyPress-Right>", self.turn_right) 
  def turn_left(self,event): #每次按键球拍移动的距离 
    self.x=-5 
  def turn_right(self,event): 
    self.x=5 
  def draw(self): #球拍移动的方法 
    pos=self.canvas.coords(self.id) 
    self.canvas.move(self.id, self.x, 0) 
    if pos[0]<=0: 
      self.x=0 
    if pos[2]>=self.canvas_width: 
      self.x=0 
paddle=Paddle(canvas,"blue") 
ball=Ball(canvas,paddle,"red") 
while True: #用循环 如果球怕没有接到球就推出 
  if ball.hit_bottom==False: 
    ball.draw() 
    paddle.draw() 
  else: 
    break 
  tk.update_idletasks()# 不停的刷新画布 
  tk.update() 
  time.sleep(0.01) 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

#--coding:utf-8--

import random
import itertools
from copy import deepcopy

def make_board(m=3):
 numbers = list(range(1,m**2+1))
 #可能出现的数字为1-9
 board = None
 #board是数度二维列表
 while board is None:
 board = get_board(m,numbers)
 return board

def get_board(m,numbers):
 n = m**2
 board = [[None for _ in range(n)] for _ in range(n)]
 for i,j in itertools.product(range(n),repeat=2):
 i0,j0 = i-i%m,j-j%m

 # i,j分别代表的是我们的行和列
 # i0和j0代表的是board[i][j]所在的区块的起始位置

 random.shuffle(numbers)
 #shuffle() 方法将序列的所有元素随机排序。
 for x in numbers:
  if(x not in board[i] 
  and all(row[j]!=x for row in board) 
  and all(x not in row[j0:j0+m] 
  for row in board[i0:i])):
  board[i][j] = x
  break
 else:#当循环正常结束时会执行else
  return None
 return board

def print_board(board,m=3):
 numbers = list(range(1,m**2+1))

 #每一行随机把5个数字变成None
 omit = 5 #omit变量掌控着每一行被抹去的数字个数
 challange = deepcopy(board)
 for i,j in itertools.product(range(omit),range(m**2)):
 x = random.choice(numbers) - 1
 challange[x][j] = None

 spacer = "++-----+-----+-----++-----+-----+-----++-----+-----+-----++"
 print(spacer.replace('-','='))
 for i,line in enumerate(challange):
 print("|| {} | {} | {} || {} | {} | {} || {} | {} | {} ||"
 .format(*(cell or ' ' for cell in line)))

 #format()函数中的 * 号,则是将所有的 cell 的不同值放入一个元组 tuple 之中,方便format函数调用
 if(i+1)%3==0:
  print(spacer.replace('-','='))
 else:
  print(spacer)
 return challange

Board = make_board()
print_board(Board)

总结

您可能感兴趣的文章:

  • 基于Python实现的扫雷游戏实例代码
  • 点球小游戏python脚本
  • 使用Python写一个小游戏
  • python实现数独游戏
    java简单实现数独游戏
  • Ubuntu下使用Python实现游戏制作中的切分图片功能
  • python编写弹球游戏的实现代码
  • Python实现Pig
    Latin小游戏实例代码
  • Python实现的井字棋(Tic Tac
    Toe)游戏示例
  • python微信跳一跳游戏辅助代码解析
  • 详解Python 实现元胞自动机中的生命游戏(Game of
    life)
  • 用python写扫雷游戏实例代码分享

效果:

以上所述是小编给大家介绍的python编写弹球游戏的实现代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

图片 1

您可能感兴趣的文章:

  • 基于Python实现的扫雷游戏实例代码
  • 点球小游戏python脚本
  • 使用Python写一个小游戏
  • python实现数独游戏
    java简单实现数独游戏
  • 简单实现python数独游戏
  • Ubuntu下使用Python实现游戏制作中的切分图片功能
  • Python实现Pig
    Latin小游戏实例代码
  • Python实现的井字棋(Tic Tac
    Toe)游戏示例
  • python微信跳一跳游戏辅助代码解析
  • 详解Python 实现元胞自动机中的生命游戏(Game of
    life)
  • 用python写扫雷游戏实例代码分享

2、Java版

package com.jimo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class Shudu {

 public String[][] make_board(int m) {
 String[][] board = null;
 int n = m * m;
 List<Integer> numbers = new ArrayList<>();
 for (int i = 1; i <= n; i++) {
  numbers.add(i);
 }
 while (board == null) {
  board = get_board(m, numbers);
 }
 return board;
 }

 private String[][] get_board(int m, List<Integer> numbers) {
 int n = m * m;
 String[][] board = new String[n][n];
 for (int i = 0; i < n; i++) {
  for (int j = 0; j < n; j++) {
  boolean ok = true;
  // 打乱顺序
  Collections.shuffle(numbers);
  for (int x = 0; x < n; x++) {
   if (isOk(numbers.get(x) + "", board, i, j, m)) {
   board[i][j] = numbers.get(x) + "";
   ok = false;
   break;
   }
  }
  if (ok) {
   return null;
  }
  }
 }
 return board;
 }

 public void print_board(String[][] board, int m) {
 int n = m * m;
 int hard = 5;
 String[][] b = new String[n][n];
 System.arraycopy(board, 0, b, 0, board.length);
 Random rd = new Random();
 for (int i = 0; i < hard; i++) {
  for (int j = 0; j < n; j++) {
  int x = rd.nextInt(9);
  b[x][j] = null;
  }
 }
 String spacer = "+-----+-----+-----+-----+-----+-----+-----+-----+-----+";
 System.out.println(spacer);
 for (int i = 0; i < n; i++) {
  for (int j = 0; j < n; j++) {
  if (null == board[i][j]) {
   System.out.print("| |");
  } else {
   System.out.print("| " + b[i][j] + " |");
  }
  }
  System.out.println();
  System.out.println(spacer);
 }
 }

 /**
 * 判断行列和9格里是否重复
 * 
 * @param x
 * @param board
 * @return
 */
 private boolean isOk(String x, String[][] board, int i, int j, int m) {
 int i0 = i - i % m, j0 = j - j % m;
 int n = m * m;
 for (int k = 0; k < n; k++) {
  if (x.equals(board[i][k]) || x.equals(board[k][j])) {
  return false;
  }
 }
 for (int k = i0; k < i; k++) {
  for (int g = j0; g < j0 + m; g++) {
  if (board[k][g].equals(x)) {
   return false;
  }
  }
 }
 return true;
 }

 public static void main(String[] args) {
 Shudu s = new Shudu();
 int m = 3;
 String[][] b = s.make_board(m);
 for (int i = 0; i < 9; i++) {
  for (int j = 0; j < 9; j++) {
  System.out.print(b[i][j] + " ");
  }
  System.out.println();
 }
 s.print_board(b, m);
 }
}

效果:

3 4 7 6 5 2 8 1 9 
5 9 6 8 3 1 4 2 7 
2 1 8 9 7 4 5 3 6 
1 3 4 7 9 5 6 8 2 
7 5 2 1 8 6 9 4 3 
6 8 9 4 2 3 1 7 5 
4 7 5 3 1 9 2 6 8 
8 2 1 5 6 7 3 9 4 
9 6 3 2 4 8 7 5 1 
+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| || || || 6 || 5 || || 8 || 1 || 9 |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| 5 || 9 || 6 || || || || 4 || || 7 |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| 2 || || || 9 || 7 || 4 || 5 || || 6 |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| 1 || 3 || 4 || 7 || 9 || || 6 || || |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| || 5 || 2 || 1 || || || || 4 || |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| || || || || 2 || 3 || || 7 || |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| 4 || 7 || || || 1 || 9 || || 6 || 8 |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| || 2 || 1 || 5 || 6 || 7 || || || 4 |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| 9 || 6 || || || || || || 5 || 1 |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+

可以看出java的代码量是python的两倍。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

  • 基于Python实现的扫雷游戏实例代码
  • 点球小游戏python脚本
  • 使用Python写一个小游戏
  • 简单实现python数独游戏
  • Ubuntu下使用Python实现游戏制作中的切分图片功能
  • python编写弹球游戏的实现代码
  • Python实现Pig
    Latin小游戏实例代码
  • Python实现的井字棋(Tic Tac
    Toe)游戏示例
  • python微信跳一跳游戏辅助代码解析
  • 详解Python 实现元胞自动机中的生命游戏(Game of
    life)
  • 用python写扫雷游戏实例代码分享