(我是菜鳥)學python的我學了下pygame,試著寫了寫貪吃蛇,下面是**,有問題也可以問我
qq2036693779,廢話不多說上**
#貪吃蛇簡單版
import pygame,sys,time,random
from pygame.locals import *
from pil import image
class snake():
def __init__(self,x,y):
self.x=x
self.y=y
def drawpic(screen,self):
pic=pygame.image.load('snake.png')
screen.blit(pic,(self.x,self.y))
def drawmap(screen):
for i in range(25):
pygame.draw.line(screen,(0,0,0),(0,i*20),(500,i*20))
for j in range(25):
pygame.draw.line(screen,(0,0,0),(j*20,0),(j*20,500))
def drawfood(screen):
x=random.randint(0,25)*20
y=random.randint(0,25)*20
pygame.draw.rect(screen,(255,0,0),(x,y,20,20))
return (x+10,y+10)
def drawfood_1(screen,x,y):
pygame.draw.rect(screen,(255,0,0),(x,y,20,20))
def move(screen,manyske):#蛇頭前進方向
direction='right'
head=manyske[0]
newx=head.x
newy=head.y
screen.fill((255,255,255))
fx,fy=drawfood(screen)
while true:
if direction=='left':
newx=head.x-20
elif direction=='right':
newx=head.x+20
elif direction=='up':
newy=head.y-20
elif direction=='down':
newy=head.y+20
screen.fill((255,255,255))
drawmap(screen)
if (fx,fy)==(newx+10,newy+10):
fx,fy=drawfood(screen)
snake=snake(manyske[-1].x,manyske[-1].y)
else:
pygame.draw.rect(screen,(255,0,0),(fx-10,fy-10,20,20))
j=len(manyske)-1
for i in range(len(manyske)):
manyske[j].x=manyske[j-1].x
manyske[j].y=manyske[j-1].y
j-=1;
head.x=newx
head.y=newy
for i in range(len(manyske)):
drawpic(screen,manyske[i])
event=pygame.event.poll()
if event.type==keydown:
if event.key==pygame.k_left and direction!='right':
direction='left'
elif event.key==pygame.k_right and direction!='left':
direction='right'
elif event.key==pygame.k_down and direction!='up':
direction='down'
elif event.key==pygame.k_up and direction!='down':
direction='up'
elif event.type==quit:
pygame.quit()
sys.exit()
pygame.display.update()
time.sleep(0.25)
#全域性變數
pygame.init()
screen=pygame.display.set_mode((500,500))
pygame.display.set_caption('貪吃蛇')
screen.fill((255,255,255))
drawmap(screen)
#食物manyske=
head=snake(0,0)
move(screen,manyske)
用Python和Pygame寫遊戲 「貪吃蛇」
coding utf 8 import pygame,sys,random from pygame.locals import 定義顏色變數 redcolour pygame.color 255,0,0 blackcolour pygame.color 0,0,0 whitecolour pygam...
用pathon寫貪吃蛇(二)
之前已經寫出了牆,現在要要把小蛇寫出來,這裡的小蛇是那種長度不變的,僅會隨著鍵盤敲轉變方向。在根據鍵盤敲下的按鍵轉變方向時,如果是直接掉頭的方式,比如小蛇在向前走,但直接向後方轉,這顯然是無法做到的,所以對於這樣的命令,我們認為它是無效的。後乙個點獲得前乙個點的座標,蛇頭根據按鍵轉變方向 towar...
C語言寫貪吃蛇與智慧型蛇
貪吃蛇 寫手動貪吃蛇的時候我用了比較醜陋的方式 include include include include include include include void getfood char map 25 int main getfood map intm for m 0 m 20 m intl...