[Py]一个贪吃蛇小游戏的雏形

# -*- coding: utf-8 -*-
# file : pygame1.py
#----2013年12月11日------

#----2013年12月12日------

import pygame
from pygame.locals import *
from sys import exit
from random import randint

pygame.init()
DISPLAYSURF = pygame.display.set_mode((640,480))	
pygame.display.set_caption('Pygame1')
FPSCLOCK=pygame.time.Clock()
FPS=10

startx=50
starty=50
directA='right'
directB='left'
snackA=[{'x':startx,'y':starty},{'x':startx-1,'y':starty},\
		{'x':startx-2,'y':starty},{'x':startx-3,'y':starty},\
		{'x':startx-4,'y':starty} ]
snackB=[{'x':startx,'y':starty},{'x':startx-1,'y':starty},\
		{'x':startx-2,'y':starty},{'x':startx-3,'y':starty},\
		{'x':startx-4,'y':starty} ]
lengthA=6
lengthB=6

applex=2
appley=2


while True:
    
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            exit()
        if event.type == KEYDOWN:
            if event.key==K_q:
                pygame.quit()
                exit()
            elif event.key==K_UP:
                directA='up'
            elif event.key==K_DOWN:
                directA='down'
            elif event.key==K_LEFT:
                directA='left'
            elif event.key==K_RIGHT:
                directA='right'
            elif event.key==K_w:
                directB='up'
            elif event.key==K_s:
                directB='down'
            elif event.key==K_a:
                directB='left'
            elif event.key==K_d:
                directB='right'
                                
    if directA=='up':
        NewheadA={'x':snackA[0]['x'],'y':snackA[0]['y']-1}
    elif directA=='down':
        NewheadA={'x':snackA[0]['x'],'y':snackA[0]['y']+1}
    elif directA=='left':
        NewheadA={'x':snackA[0]['x']-1,'y':snackA[0]['y']}
    elif directA=='right':
        NewheadA={'x':snackA[0]['x']+1,'y':snackA[0]['y']}

    if directB=='up':
        NewheadB={'x':snackB[0]['x'],'y':snackB[0]['y']-1}
    elif directB=='down':
        NewheadB={'x':snackB[0]['x'],'y':snackB[0]['y']+1}
    elif directB=='left':
        NewheadB={'x':snackB[0]['x']-1,'y':snackB[0]['y']}
    elif directB=='right':
        NewheadB={'x':snackB[0]['x']+1,'y':snackB[0]['y']}

    if snackA[0]['x']==applex and snackA[0]['y']==appley:
        applex=randint(0,50)
        appley=randint(0,50)
        lengthA=lengthA+1;
        
    if snackB[0]['x']==applex and snackB[0]['y']==appley:
        applex=randint(0,50)
        appley=randint(0,50)
        lengthB=lengthB+1;

    snackA.insert(0,NewheadA)
    snackB.insert(0,NewheadB)





    DISPLAYSURF.fill((0,0,0))
       
    

    #draw snack:    
    for point in snackA[0:lengthA]:
        x=point['x']*5
        y=point['y']*5
        Rect = pygame.Rect(x, y, 5, 5)
        pygame.draw.rect(DISPLAYSURF, (0,255,0), Rect)
		
    for point in snackB[0:lengthB]:
        x=point['x']*5
        y=point['y']*5
        Rect = pygame.Rect(x, y, 5, 5)
        pygame.draw.rect(DISPLAYSURF, (255,0,0), Rect) #

        

    #draw apple
    x=applex*5
    y=appley*5
    Rect = pygame.Rect(x,y,5,5)
    pygame.draw.rect(DISPLAYSURF,(255,255,255),Rect) #

    

    pygame.display.update()
    FPSCLOCK.tick(FPS)






。。。。

接下来,应该做成面向对象,再丰富一下细节。。= =

当然可以!以下是一个简单的贪吃蛇小游戏Python代码示例: ```python import pygame import random # 初始化pygame pygame.init() # 游戏窗口尺寸 window_width = 800 window_height = 600 # 蛇头和食物的大小 block_size = 20 # 设置游戏窗口 window = pygame.display.set_mode((window_width, window_height)) pygame.display.set_caption("贪吃蛇") # 定义颜色 white = (255, 255, 255) black = (0, 0, 0) red = (255, 0, 0) # 游戏时钟 clock = pygame.time.Clock() # 设置蛇的初始位置和移动速度 snake_speed = 15 direction = "right" def game_loop(): game_over = False game_close = False # 蛇的初始位置 x1 = window_width / 2 y1 = window_height / 2 # 蛇的移动速度 x1_change = 0 y1_change = 0 # 定义食物初始位置 food_x = round(random.randrange(0, window_width - block_size) / block_size) * block_size food_y = round(random.randrange(0, window_height - block_size) / block_size) * block_size while not game_over: while game_close: # 游戏结束时显示得分和重新开始选项 window.fill(black) font_style = pygame.font.SysFont(None, 50) message = font_style.render("Game Over! Press Q-Quit or C-Play Again", True, white) window.blit(message, [window_width / 6, window_height / 3]) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.QUIT: game_over = True game_close = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_q: game_over = True game_close = False if event.key == pygame.K_c: game_loop() for event in pygame.event.get(): if event.type == pygame.QUIT: game_over = True if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: x1_change = -block_size y1_change = 0 elif event.key == pygame.K_RIGHT: x1_change = block_size y1_change = 0 elif event.key == pygame.K_UP: y1_change = -block_size x1_change = 0 elif event.key == pygame.K_DOWN: y1_change = block_size x1_change = 0 # 判断蛇是否撞到边界 if x1 >= window_width or x1 < 0 or y1 >= window_height or y1 < 0: game_close = True # 更新蛇的位置 x1 += x1_change y1 += y1_change window.fill(black) pygame.draw.rect(window, white, [food_x, food_y, block_size, block_size]) pygame.draw.rect(window, red, [x1, y1, block_size, block_size]) pygame.display.update() # 判断蛇是否吃到食物 if x1 == food_x and y1 == food_y: food_x = round(random.randrange(0, window_width - block_size) / block_size) * block_size food_y = round(random.randrange(0, window_height - block_size) / block_size) * block_size clock.tick(snake_speed) pygame.quit() # 运行游戏 game_loop() ``` 这只是一个简单的贪吃蛇游戏示例,你可以根据自己的需要进行修改和扩展。希望对你有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值