求大神编一个python 200-300行的小游戏(比如贪食蛇 俄罗斯方块 剪刀石头布一类的)求速度 真的 就4天 请用PYTHON编一个小游戏,如五子棋,连连看,贪吃蛇,扫雷...

\u7528java\u53ef\u4ee5\u7f16\u5199\u5c0f\u6e38\u620f\uff0c\u6bd4\u5982\u8fde\u8fde\u770b\u3001\u4e94\u6307\u68cb\u3001\u626b\u96f7\u3001\u526a\u5200\u77f3\u5934\u5e03\u3001\u8d2a\u98df\u86c7\u3001\u4fc4\u7f57\u65af\u65b9\u5757\u7b49\uff0c\u8fd8\u6709\u54ea\u4e9b\uff1f

\u6b22\u4e50\u6597\u5730\u4e3b\u3001\u7eb8\u724c

#!/usr/bin/pythonfrom Tkinter import *import randomclass 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("", 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 = 4app = snake()app.master.title("Greedy Snake")app.mainloop()\u8d2a\u98df\u86c7

skier_images = ["skier_down.png", "skier_right1.png", "skier_right2.png",
"skier_left2.png", "skier_left1.png"]
# class for the skier sprite
class SkierClass(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("skier_down.png")
self.rect = self.image.get_rect()
self.rect.center = [320, 100]
self.angle = 0

def turn(self, direction):
# load new image and change speed when the skier turns
self.angle = self.angle + direction
if self.angle < -2: self.angle = -2
if self.angle > 2: self.angle = 2
center = self.rect.center
self.image = pygame.image.load(skier_images[self.angle])
self.rect = self.image.get_rect()
self.rect.center = center
speed = [self.angle, 6 - abs(self.angle) * 2]
return speed

def move(self, speed):
# move the skier right and left
self.rect.centerx = self.rect.centerx + speed[0]
if self.rect.centerx < 20: self.rect.centerx = 20
if self.rect.centerx > 620: self.rect.centerx = 620

# class for obstacle sprites (trees and flags)
class ObstacleClass(pygame.sprite.Sprite):
def __init__(self, image_file, location, type):
pygame.sprite.Sprite.__init__(self) #call Sprite initializer
self.image_file = image_file
self.image = pygame.image.load(image_file)
self.location = location
self.rect = self.image.get_rect()
self.rect.center = location
self.type = type
self.passed = False

def scroll(self, terrainPos):
self.rect.centery = self.location[1] - terrainPos
# create one "screen" of terrain: 640 x 640
# use "blocks" of 64 x 64 pixels, so objects aren't too close together
def create_map(start, end):
obstacles = pygame.sprite.Group()
locations = []
gates = pygame.sprite.Group()
for i in range(10): # 10 obstacles per screen
row = random.randint(start, end)
col = random.randint(0, 9)
location = [col * 64 + 20, row * 64 + 20] #center x, y for obstacle
if not (location in locations): # prevent 2 obstacles in the same place
locations.append(location)
type = random.choice(["tree", "flag"])
if type == "tree": img = "skier_tree.png"
elif type == "flag": img = "skier_flag.png"
obstacle = ObstacleClass(img, location, type)
obstacles.add(obstacle)
return obstacles
# redraw the screen, including all sprites
def animate():
screen.fill([255, 255, 255])
pygame.display.update(obstacles.draw(screen))
screen.blit(skier.image, skier.rect)
screen.blit(score_text, [10, 10])
pygame.display.flip()
def updateObstacleGroup(map0, map1):
obstacles = pygame.sprite.Group()
for ob in map0: obstacles.add(ob)
for ob in map1: obstacles.add(ob)
return obstacles
# initialize everything
pygame.init()
screen = pygame.display.set_mode([640,640])
clock = pygame.time.Clock()
skier = SkierClass()
speed = [0, 6]
map_position = 0
points = 0
map0 = create_map(20, 29)
map1 = create_map(10, 19)
activeMap = 0
# group for all obstacles to do collision detection
obstacles = updateObstacleGroup(map0, map1)
# font object for score
font = pygame.font.Font(None, 50)
# main Pygame event loop
while True:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
if event.type == pygame.KEYDOWN: # check for key presses
if event.key == pygame.K_LEFT: # left arrow turns left
speed = skier.turn(-1)
elif event.key == pygame.K_RIGHT: #right arrow turns right
speed = skier.turn(1)
skier.move(speed)
map_position += speed[1] # scroll the terrain

# manage terrain maps, switch between them,
# and create new terrain at the bottom
if map_position >=640 and activeMap == 0:
activeMap = 1
map0 = create_map(20, 29)
obstacles = updateObstacleGroup(map0, map1)
if map_position >=1280 and activeMap == 1:
activeMap = 0
for ob in map0:
ob.location[1] = ob.location[1] - 1280 # wrap around to top
map_position = map_position - 1280 #
map1 = create_map(10, 19)
obstacles = updateObstacleGroup(map0, map1)

for obstacle in obstacles:
obstacle.scroll(map_position)

# check for hitting trees or getting flags
hit = pygame.sprite.spritecollide(skier, obstacles, False)
if hit:
if hit[0].type == "tree" and not hit[0].passed: #crashed into tree
points = points - 100
skier.image = pygame.image.load("skier_crash.png") # crash image
animate()
pygame.time.delay(1000)
skier.image = pygame.image.load("skier_down.png") # resume skiing
skier.angle = 0
speed = [0, 6]
hit[0].passed = True
elif hit[0].type == "flag" and not hit[0].passed: # got a flag
points += 10
obstacles.remove(hit[0]) # remove the flag

score_text = font.render("Score: " +str(points), 1, (0, 0, 0))
animate()

图片自己找,滑雪人

好好学习,不抄作业

以发,请查收

扩展阅读:python手机版下载安装 ... python代码自动生成器 ... javascript 在线 ... python入门 ... python网站 ... python基础代码大全 ... python求1+2+3+n的和 ... python 强制退出 ... python编程电子书免费 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网