python编程代码大全游戏(python 游戏编程)

http://www.itjxue.com  2023-03-30 02:46  来源:未知  点击次数: 

python能做什么游戏

Python是一门高级且有趣的编程语言,除了网络爬虫、人工智能、数据分析之外,Python还可以进行游戏开发,为大家介绍五个支持Python的2D、3D游戏开发库。

1、Cocos2d:是一系列开源软件框架,用于构建跨平台2D游戏和应用程序,由cocos2d-x、cocos2d-js、cocos2d-xna和cocos2d多种框架组成,像大鱼赌场、城堡冲突等小游戏,就是用此框架开发出来的。

2、Panda3D:是由迪士尼开发的3D游戏引擎,一个用于Python和C++程序的3D渲染和游戏开发框架,并由卡内基梅陇娱乐技术中心负责维护,使用C++编写的,针对Python进行了完全的封装。

3、Pygame:它是一组Python模块,用来编写游戏,可支持Python3.7,游戏例子有:纸牌游戏、超级马里奥、击球等多种游戏。

4、Pyogre:ogre 3D渲染引擎的Python绑定,可以用来开发游戏和仿真程序等任何3D应用,它的API更加稳定,也非常快速灵活。

5、RenPy:一个视觉小说引擎,被世界各地的成千万的创造者所使用,它可以帮助你使用文字、图像和声音来讲述电脑和移动设备上的故事。RenPy是开放源码的,可免费的商业用途,易于学习的脚本语言任何人都能有效地编写大型视觉小说,它的Python脚本足以用来模拟游戏。

请用PYTHON编一个小游戏,如五子棋,连连看,贪吃蛇,扫雷,计算器等等

#!/usr/bin/python

from Tkinter import *

import random

class snake(Frame):

????????def __init__(self, master=None):

????????????????Frame.__init__(self, master)

????????????????self.body = [(0,0)]

????????????????self.bodyid = []

????????????????self.food = [ -1, -1 ]

????????????????self.foodid = -1

????????????????self.gridcount = 10

????????????????self.size = 500

????????????????self.di = 3

????????????????self.speed = 500

????????????????self.top = self.winfo_toplevel()

????????????????self.top.resizable(False, False)

????????????????self.grid()

????????????????self.canvas = Canvas(self)

????????????????self.canvas.grid()

????????????????self.canvas.config(width=self.size, height=self.size,relief=RIDGE)

????????????????self.drawgrid()

????????????????s = self.size/self.gridcount

????????????????id = self.canvas.create_rectangle(self.body[0][0]*s,self.body[0][1]*s,

????????????????????????(self.body[0][0]+1)*s, (self.body[0][1]+1)*s, fill="yellow")

????????????????self.bodyid.insert(0, id)

????????????????self.bind_all("KeyRelease", self.keyrelease)

????????????????self.drawfood()

????????????????self.after(self.speed, self.drawsnake)

????????def drawgrid(self):

????????????????s = self.size/self.gridcount

????????????????for i in range(0, self.gridcount+1):

????????????????????????self.canvas.create_line(i*s, 0, i*s, self.size)

????????????????????????self.canvas.create_line(0, i*s, self.size, i*s)

????????def drawsnake(self):

????????????????s = self.size/self.gridcount

????????????????head = self.body[0]

????????????????new = [head[0], head[1]]

????????????????if self.di == 1:

????????????????????????new[1] = (head[1]-1) % self.gridcount

????????????????elif self.di == 2:

????????????????????????new[0] = (head[0]+1) % self.gridcount

????????????????elif self.di == 3:

????????????????????????new[1] = (head[1]+1) % self.gridcount

????????????????else:

????????????????????????new[0] = (head[0]-1) % self.gridcount

????????????????next = ( new[0], new[1] )

????????????????if next in self.body:

????????????????????????exit()

????????????????elif next == (self.food[0], self.food[1]):

????????????????????????self.body.insert(0, next)

????????????????????????self.bodyid.insert(0, self.foodid)

????????????????????????self.drawfood()

????????????????else:

????????????????????????tail = self.body.pop()

????????????????????????id = self.bodyid.pop()

????????????????????????self.canvas.move(id, (next[0]-tail[0])*s, (next[1]-tail[1])*s)

????????????????????????self.body.insert(0, next)

????????????????????????self.bodyid.insert(0, id)

????????????????self.after(self.speed, self.drawsnake)

????????def drawfood(self):

????????????????s = self.size/self.gridcount

????????????????x = random.randrange(0, self.gridcount)

????????????????y = random.randrange(0, self.gridcount)

????????????????while (x, y) in self.body:

????????????????????????x = random.randrange(0, self.gridcount)

????????????????????????y = random.randrange(0, self.gridcount)

????????????????id = self.canvas.create_rectangle(x*s,y*s, (x+1)*s, (y+1)*s, fill="yellow")

????????????????self.food[0] = x

????????????????self.food[1] = y

????????????????self.foodid = id

????????def keyrelease(self, event):

????????????????if event.keysym == "Up" and self.di != 3:

????????????????????????self.di = 1

????????????????elif event.keysym == "Right" and self.di !=4:

????????????????????????self.di = 2

????????????????elif event.keysym == "Down" and self.di != 1:

????????????????????????self.di = 3

????????????????elif event.keysym == "Left" and self.di != 2:

????????????????????????self.di = 4

app = snake()

app.master.title("Greedy Snake")

app.mainloop()

贪食蛇

python有趣的编程代码

class?Point:

??row=0

??col=0

??def?__init__(self,?row,?col):

????self.row=row

????self.col=col

??def?copy(self):

????return?Point(row=self.row,?col=self.col)

#初始框架

import?pygame

import?random

#初始化

pygame.init()

W=800

H=600

ROW=30

COL=40

size=(W,H)

window=pygame.display.set_mode(size)

pygame.display.set_caption('贪吃蛇')

bg_color=(255,255,255)

snake_color=(200,200,200)

head=Point(row=int(ROW/2),?col=int(COL/2))

head_color=(0,128,128)

snakes=[

??Point(row=head.row,?col=head.col+1),

??Point(row=head.row,?col=head.col+2),

??Point(row=head.row,?col=head.col+3)

]

#生成食物

def?gen_food():

??while?1:

????pos=Point(row=random.randint(0,ROW-1),?col=random.randint(0,COL-1))

????#

????is_coll=False

????#是否跟蛇碰上了

????if?head.row==pos.row?and?head.col==pos.col:

??????is_coll=True

????#蛇身子

????for?snake?in?snakes:

??????if?snake.row==pos.row?and?snake.col==pos.col:

????????is_coll=True

????????break

????if?not?is_coll:

??????break

??return?pos

#定义坐标

food=gen_food()

food_color=(255,255,0)

direct='left'???????#left,right,up,down

#

def?rect(point,?color):

??cell_width=W/COL

??cell_height=H/ROW

??left=point.col*cell_width

??top=point.row*cell_height

??pygame.draw.rect(

????window,?color,

????(left,?top,?cell_width,?cell_height)

??)

??pass

#游戏循环

quit=True

clock=pygame.time.Clock()

while?quit:

??#处理事件

??for?event?in?pygame.event.get():

????if?event.type==pygame.QUIT:

??????quit=False

????elif?event.type==pygame.KEYDOWN:

??????if?event.key==273?or?event.key==119:

????????if?direct=='left'?or?direct=='right':

??????????direct='up'

??????elif?event.key==274?or?event.key==115:

????????if?direct?==?'left'?or?direct?==?'right':

??????????direct='down'

??????elif?event.key==276?or?event.key==97:

????????if?direct?==?'up'?or?direct?==?'down':

??????????direct='left'

??????elif?event.key==275?or?event.key==100:

????????if?direct?==?'up'?or?direct?==?'down':

??????????direct='right'

??#吃东西

??eat=(head.row==food.row?and?head.col==food.col)

??#重新产生食物

??if?eat:

????food?=?gen_food()

??#处理身子

??#1.把原来的头,插入到snakes的头上

??snakes.insert(0,?head.copy())

??#2.把snakes的最后一个删掉

??if?not?eat:

????snakes.pop()

??#移动

??if?direct=='left':

????head.col-=1

??elif?direct=='right':

????head.col+=1

??elif?direct=='up':

????head.row-=1

??elif?direct=='down':

????head.row+=1

??#检测

??dead=False

??#1.撞墙

??if?head.col0?or?head.row0?or?head.col=COL?or?head.row=ROW:

????dead=True

??#2.撞自己

??for?snake?in?snakes:

????if?head.col==snake.col?and?head.row==snake.row:

??????dead=True

??????break

??if?dead:

????print('死了')

????quit=False

??#渲染——画出来

??#背景

??pygame.draw.rect(window,?bg_color,?(0,0,W,H))

??#蛇头

??for?snake?in?snakes:

????rect(snake,?snake_color)

??rect(head,?head_color)

??rect(food,?food_color)

??#

??pygame.display.flip()

??#设置帧频(速度)

??clock.tick(8)

#收尾工作

这是一个简易版贪吃蛇的代码,虽然结构简单,但是该有的功能都是完整的,可玩性也不错

python猜拳游戏编程代码背景意义

python猜拳游戏编程代码背景意义是用面向对象的思想做一个游戏。用面向对象的思想玩家猜拳:1剪刀2石头3布玩家输入一个1-3的数字电脑出拳:随机产生一个1-3的数字,提示电脑出拳结果本局对战结束,输出谁赢,是否继续输出结果:玩家赢几局电脑赢几局平局几次游戏结束。

有哪些小游戏可以用python实现?

ant 蚂蚁2. bagels 百吉饼另外贪吃蛇,吃豆子都是可以的,直接代码就可以玩。

Python包括随您的安装一起分发的大量标准库。标准库有一个称为Turtle的模块,这是一种向普通人介绍python编程的流行方法。今天介绍的所有游戏都是使用Python及其Turtle模块实现的。

每个游戏都完全独立于其他游戏,不试一试 您永远都不知道最喜欢哪种游戏。

安装完之后我们可以使用"python -m freegames list"来查看所有的游戏

贪吃蛇

贪吃蛇 -经典街机游戏。使用箭头键导航并吃绿色食品。每次食用食物,蛇就会长出一段。避免自己进食或出界!

吃豆子

吃豆子 –经典街机游戏。使用箭头键导航并吃掉所有白色食物。提防漫游迷宫的红色幽灵。

Flappy

Flappy-bird启发游戏。单击屏幕拍打翅膀。当您飞越屏幕时,请当心黑乌鸦。

加农炮

大炮-射击运动。单击屏幕发射您的炮弹。炮弹在其路径中弹出蓝色气球。弹出所有气球,然后才能越过屏幕。

python 编写一个彩票游戏?

按照题目要求编写的Python程序如下

import random

numlist=random.sample(range(0,10),5)

while numlist[0]==0:

? numlist=random.sample(range(0,10),5)

num=int(''.join([str(i) for i in numlist]))

inputnum=int(input("输入号:"))

bonus=0

count=0

if inputnum==num:

bonus=10000

else:

for i in set(str(inputnum)):

if int(i) in numlist:

count+=1

bonus=1000*count

print("彩票号:%d" % num)

print("奖金:%d元" % bonus)

源代码(注意源代码的缩进)

(责任编辑:IT教学网)

更多

推荐DNS服务器文章