Python制作游戏代码(python游戏程序代码)

http://www.itjxue.com  2023-02-12 01:15  来源:未知  点击次数: 

Python 9个同学一起做游戏,每个人报一个[1,20]区间上的正整数,请写出编程?

1、如单纯取区间报数,循环列表取值即可。

2、如要实现区间随机报数,用random获取随机数(真随机数先用seed设置种子)。

3、如要实现区间随机不重复报数,用list或set存储随机值做比较。

下面代码按第三种情况写:

import random

import time

n=1

st=set()

random.seed(time.time())

while True:

? num=random.randint(1,20)

? if num not in st:

? ? ? print("第%d位报数%d\n"%(n,num))

? ? ? st.add(num)

? ? ? n+=1

? if n9:

? ? ? break

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写一个猜词小游戏的代码?

key = input('请输入一个单词:')

description = input('输入单词描述:')

chance = 5

mark = 5

print('现在开始游戏')

print(description + ' '+'\t 这是单词的描述,请你输入这个单词: ')

for i in range(0, 5):

a = input('请你输入单词:')

if a == key:

print('恭喜你答对了,您的分数%d', mark)

else:

print('对不起,你打错了,你还有 %d 次机会,你的分数%d' % (chance-1, mark-1))

if chance == 0:

print('很抱歉,你已经没有机会了,最后得分%d' % mark)

chance -= 1

mark -= 1

(责任编辑:IT教学网)

更多