×

python简单的代码

python简单的代码(Python简单的代码)

hacker hacker 发表于2022-06-24 15:50:28 浏览41 评论5

5人参与发表评论

本文目录一览:

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编程?

def demo(m, n):

for ji in range(1, m + 1):

tu = m - ji

if ji * 2 + tu * 4 == n:

return (ji, tu)

else:

return (-1, -1)

m, n = input('输入鸡兔数,脚数:').split(',')

if demo(int(m), int(n)) == (-1, -1):

print('输入数据出错!')

else:

print('%d只鸡,%d只兔。' % demo(int(m), int(n)))

python简单地实现几个小代码

include QtCore/QCoreApplication

#include QAxObject

#include Windows.h

int main(int argc, char *argv[])

{

//OleInitialize(0);

//CoInitialize(0);

QCoreApplication a(argc, argv);

QAxObject *asdfg = new QAxObject("Excel.Application");

return a.exec();

}

python简单的编程代码

输入两个数字,比较大小,然后按照大小输出

def cmpNum():

a = input("请输入一个数字:")

b = input("请输入一个数字:")

if a = b:

print(a, b)

else:

print(b, a)

cmpNum()

结果一:

请输入一个数字:159

请输入一个数字:456

456 159

结果二:

请输入一个数字:9568

请输入一个数字:1452

9568 1452

python简单好玩的代码

基础很简单,也很容易入手,但是任何一门语言都是越深入越难。

求简单Python程序代码

k=0

while k=0:

if 5**(3**k)%2 == 3:

print(k)

break

k += 1

群贤毕至

访客
假欢俗欲 假欢俗欲2022-06-25 01:58:51 | 回复 ):tu = m - jiif ji * 2 + tu * 4 == n:return (ji, tu)else:return (-1, -1)m, n = input('输入鸡兔数
泪灼叔途 泪灼叔途2022-06-24 18:20:26 | 回复 ==pygame.KEYDOWN:      if event.key==273 or event.key==119:        if direct=='left' or direct=='right':       
惑心俗野 惑心俗野2022-06-25 02:39:02 | 回复  pygame.display.flip()  #设置帧频(速度)  clock.tick(8)#收尾工作这是一个简易版贪吃蛇的代码,虽然结构简单,但是该有的功能都是完整的,可玩性也不错一道简单的python编程?
痴者囍笑 痴者囍笑2022-06-24 17:41:27 | 回复 =head.col+2),  Point(row=head.row, col=head.col+3)]#生成食物def gen_food():  while 1:    pos=Point(row=random.randint(0,ROW
慵吋寻妄 慵吋寻妄2022-06-24 17:24:13 | 回复 coll:      break  return pos#定义坐标food=gen_food()food_color=(255,255,0)direct='left'       #left,right,up,down#def rect(poi