pygame贪吃蛇问题
#贪吃蛇.pyimport pygame
import sys
from pygame.locals import *
import 金币
import 蛇身
pygame.init()
pygame.mixer.init()
size = width, height = 600, 400
bg = (0, 0, 255)
YELLOW = (255, 255, 0)
snake = (0, 255, 0)
white = (255, 255, 255)
Sbody = {}
fanxiang = 'right'
left = 300
top = 200
pygame.mouse.set_visible(False)
#pygame.mixer.music.load('Ximalaya-4.0.0_99B_sc100006.exe')
pygame.mixer.music.load('game_music.ogg')
pygame.mixer.music.set_volume(1)
a = pygame.mixer.Sound('upgrade.wav')
screen = pygame.display.set_mode(size)
pygame.display.set_caption('贪吃蛇')
move = USEREVENT
pygame.time.set_timer(move, 1 * 100)
money = USEREVENT + 1
pygame.time.set_timer(money, 5 * 1000)
score = 0
score_font = pygame.font.Font(None, 20)
GameOver_font = pygame.font.Font(None, 100)
Money = pygame.sprite.Group()
snakebody = pygame.sprite.Group()
pygame.mixer.music.play()
running = True
clock = pygame.time.Clock()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == move:
if fanxiang == 'left':
Sbody =
left -= 11
for b in snakebody:
Sbody =
elif fanxiang == 'top':
Sbody =
top -= 11
for b in snakebody:
Sbody =
elif fanxiang == 'right':
Sbody =
left += 11
for b in snakebody:
Sbody =
elif fanxiang == 'bottom':
Sbody =
top += 11
for b in snakebody:
Sbody =
#生成金币
elif event.type == money:
print(left, top)
m = 金币.money()
Money.add(m)
key_pressed = pygame.key.get_pressed()
#控制方向
if key_pressed or key_pressed:
fanxiang = 'top'
elif key_pressed or key_pressed:
fanxiang = 'bottom'
elif key_pressed or key_pressed:
fanxiang = 'right'
elif key_pressed or key_pressed:
fanxiang = 'left'
if left <= 0 or left >= 600 or top <= 0 or top >= 400:
pygame.mixer.music.stop()
#raise OSError('不能碰到身体或边缘!!!')
running = False
GameOver = True
screen.fill(bg)
#蛇身机制
for b in snakebody:
lastnumber = b.number - 1
nextnumber = b.number + 1
#pygame.time.delay(1)
#print(Sbody)
b.width = Sbody
b.height = Sbody
b.rect = pygame.Rect(b.width, b.height, 10, 10)
pygame.draw.rect(screen, snake, b.rect, 0)
if b.rect.colliderect(pygame.Rect(left, top, 10, 10)):
pygame.mixer.music.stop()
#raise OSError('不能碰到身体或边缘!!!')
running = False
GameOver = True
#金币机制
for i in Money:
pygame.draw.rect(screen, YELLOW, i.rect, 0)
if i.rect.colliderect(pygame.Rect(left, top, 10, 10)):
score+=1000
thousand = score / 1000
last = thousand - 1
#增加长度
s = 蛇身.body(int(Sbody), int(Sbody), thousand, size)
snakebody.add(s)
Money.remove(i)
a.play()
#绘制贪吃蛇
pygame.draw.rect(screen, snake, (left, top, 10, 10), 0)
#显示字迹
score_text = score_font.render(f'Score:{score}', True, white)
screen.blit(score_text, (10, 5))
pygame.display.flip()
while GameOver:
#print('已经准备就绪!')
GameOver_text = GameOver_font.render('Game Over', True, white)
screen.blit(GameOver_text, (100, 100))
pygame.time.delay(1000)
clock.tick(1000)
#蛇身.py
import pygame
class body(pygame.sprite.Sprite):
def __init__(self, width, height, number, bg_size):
pygame.sprite.Sprite.__init__(self)
self.width = width
self.height = height
self.rect = pygame.Rect(self.width, self.height, 10, 10)
self.number = number
self.left = bg_size
self.top = bg_size
#金币.py
import pygame
import random
class money(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.rect = pygame.Rect(random.randint(0, 590), random.randint(0, 390), 10, 10)
不知道为什么游戏失败时突然卡住了,求各位帮忙修复,谢谢 @isdkz 在你的代码中,有几个地方可能导致游戏失败时卡住:
1. 在主循环中的`for event in pygame.event.get():`循环中,当检测到"QUIT"事件后应该退出游戏,但你的代码中只是调用了`sys.exit()`,并没有将`running`置为`False`。所以可以修改为 `running = False`,这样就能正确退出主循环。
2. 在主循环中的键盘控制部分,你使用了`or`逻辑运算符,这是错误的。应该使用`and`逻辑运算符来判断多个按键的组合。例如,应该将 `if key_pressed: fanxiang = 'top'` 修改为 `if key_pressed and not key_pressed: fanxiang = 'top'`。
3. 判断是否碰到边缘的条件语句有错误。在 `if left = 600 or top = 400:` 这一行中,应该使用相等比较运算符`==`而不是赋值运算符`=`。 所以应该修改为 `if left == 600 or top == 400:`。
修复以上问题后,你的代码应该能够正确处理游戏失败,并退出主循环。
另外,你的代码中存在一些不规范的写法和缺少部分代码,下面是修改后的代码示例:
# 贪吃蛇.py
import pygame
import sys
from pygame.locals import *
import 金币
import 蛇身
pygame.init()
pygame.mixer.init()
size = width, height = 600, 400
bg = (0, 0, 255)
YELLOW = (255, 255, 0)
snake = (0, 255, 0)
white = (255, 255, 255)
Sbody = 10 # 初始蛇身大小
fanxiang = 'right'
left = 300
top = 200
pygame.mouse.set_visible(False)
pygame.mixer.music.load('game_music.ogg')
pygame.mixer.music.set_volume(1)
a = pygame.mixer.Sound('upgrade.wav')
screen = pygame.display.set_mode(size)
pygame.display.set_caption('贪吃蛇')
move = USEREVENT
pygame.time.set_timer(move, 1 * 100)
money = USEREVENT + 1
pygame.time.set_timer(money, 5 * 1000)
score = 0
score_font = pygame.font.Font(None, 20)
GameOver_font = pygame.font.Font(None, 100)
Money = pygame.sprite.Group()
snakebody = pygame.sprite.Group()
pygame.mixer.music.play()
running = True
clock = pygame.time.Clock()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == move:
if fanxiang == 'left':
left -= 11
elif fanxiang == 'top':
top -= 11
elif fanxiang == 'right':
left += 11
elif fanxiang == 'bottom':
top += 11
# 生成金币
elif event.type == money:
m = 金币.money()
Money.add(m)
key_pressed = pygame.key.get_pressed()
# 控制方向
if key_pressed and not key_pressed:
fanxiang = 'top'
elif key_pressed and not key_pressed:
fanxiang = 'bottom'
elif key_pressed and not key_pressed:
fanxiang = 'right'
elif key_pressed and not key_pressed:
fanxiang = 'left'
if left >= 600 or top >= 400:
pygame.mixer.music.stop()
running = False
screen.fill(bg)
# 蛇身机制
for b in snakebody:
lastnumber = b.number - 1
nextnumber = b.number + 1
b.width = Sbody
b.height = Sbody
b.rect = pygame.Rect(b.width, b.height, 10, 10)
pygame.draw.rect(screen, snake, b.rect, 0)
if b.rect.colliderect(pygame.Rect(left, top, 10, 10)):
pygame.mixer.music.stop()
running = False
# 金币机制
for i in Money:
pygame.draw.rect(screen, YELLOW, i.rect, 0)
if i.rect.colliderect(pygame.Rect(left, top, 10, 10)):
score += 1000
thousand = score // 1000
last = thousand - 1
# 增加长度
s = 蛇身.body(int(Sbody), int(Sbody), thousand, size)
snakebody.add(s)
Money.remove(i)
a.play()
# 绘制贪吃蛇
pygame.draw.rect(screen, snake, (left, top, 10, 10), 0)
# 显示字迹
score_text = score_font.render(f'Score:{score}', True, white)
screen.blit(score_text, (10, 5))
pygame.display.flip()
GameOver = True
while GameOver:
GameOver_text = GameOver_font.render('Game Over', True, white)
screen.blit(GameOver_text, (100, 100))
pygame.time.delay(1000)
clock.tick(1000)
希望这能帮助到你修复问题。如果还有其他问题,请随时问我。
以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。 @不二如是 flyps 发表于 2023-11-26 20:49
@不二如是
抱歉,我之前的回答有错误,我理解错了你的代码问题。真正导致游戏卡住的原因是因为在主循环中没有处理事件监听与更新屏幕的间隔时间。
下面是修复问题的修改代码:
import pygame
import sys
from pygame.locals import *
import random
pygame.init()
size = width, height = 600, 400
bg = (0, 0, 255)
YELLOW = (255, 255, 0)
snake = (0, 255, 0)
white = (255, 255, 255)
Sbody = 10 # 初始蛇身大小
fanxiang = 'right'
left = 300
top = 200
screen = pygame.display.set_mode(size)
pygame.display.set_caption('贪吃蛇')
clock = pygame.time.Clock()
class SnakeBody(pygame.sprite.Sprite):
def __init__(self, width, height):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((10, 10))
self.image.fill(snake)
self.rect = self.image.get_rect()
self.rect.x = width
self.rect.y = height
class Money(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((10, 10))
self.image.fill(YELLOW)
self.rect = self.image.get_rect()
self.rect.x = random.randint(0, 590)
self.rect.y = random.randint(0, 390)
snakebody = pygame.sprite.Group()
moneygroup = pygame.sprite.Group()
score = 0
score_font = pygame.font.Font(None, 20)
GameOver_font = pygame.font.Font(None, 100)
running = True
move_event = pygame.USEREVENT
money_event = pygame.USEREVENT + 1
pygame.time.set_timer(move_event, 100)
pygame.time.set_timer(money_event, 5000)
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == move_event:
if fanxiang == 'left':
left -= 11
elif fanxiang == 'top':
top -= 11
elif fanxiang == 'right':
left += 11
elif fanxiang == 'bottom':
top += 11
elif event.type == money_event:
m = Money()
moneygroup.add(m)
key_pressed = pygame.key.get_pressed()
if key_pressed and not key_pressed:
fanxiang = 'top'
elif key_pressed and not key_pressed:
fanxiang = 'bottom'
elif key_pressed and not key_pressed:
fanxiang = 'right'
elif key_pressed and not key_pressed:
fanxiang = 'left'
snakebody.update()
screen.fill(bg)
snakebody.draw(screen)
moneygroup.draw(screen)
for b in snakebody:
if b.rect.colliderect(pygame.Rect(left, top, 10, 10)):
running = False
for i in moneygroup:
if i.rect.colliderect(pygame.Rect(left, top, 10, 10)):
score += 1000
thousand = score // 1000
last = thousand - 1
s = SnakeBody(int(left), int(top))
snakebody.add(s)
moneygroup.remove(i)
score_text = score_font.render(f'Score:{score}', True, white)
screen.blit(score_text, (10, 5))
pygame.display.flip()
clock.tick(10)
pygame.quit()
sys.exit()
希望这次能解决你的问题。如果还有其他问题,请随时问我。
以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
页:
[1]